給一個girdview設置一個自定義的ArrayAdapter,該適配器同時實現了OnScrollListener接口,但是OnScrollListener中的onScroll() 以及在滑動gridview的時候onScrollStateChanged()都沒有執行,自定義的ArrayAdapter中的geiView()執行正常,這段代碼主要是現實從SD卡讀取圖片並顯示在gridview中,代碼如下:
public class PhotoGalleryFragment extends Fragment {
private static final String TAG = "PhotoGalleryFragment";
private GridView mGridView;
private ArrayList<GalleryItem> mItems;
private PhotoLoader<ImageView> mPhotoLoader;
private int mFirstVisibleItem,mVisibleItemCount;
//用於判斷是否是第一次打開應用,解決第一次進入應用,圖片不加載的問題
private boolean mIsFirstIn = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
new FetchItemsTask().execute();
//new FetchItemsTask().cancel();
mPhotoLoader = new PhotoLoader<>(new Handler(),getActivity());
mPhotoLoader.setmListener(new PhotoLoader.Listener<ImageView>() {
@Override
public void onPhotoLoadered(ImageView imageView, Bitmap bitmap) {
if (isVisible()) imageView.setImageBitmap(bitmap);
}
});
mPhotoLoader.start();
mPhotoLoader.getLooper();
Log.d(TAG, "backgrounp thread started");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_photo_gallery,container,false);
mGridView = (GridView) view.findViewById(R.id.gridView);
setupAdapter();
return view;
}
private class FetchItemsTask extends AsyncTask<Void,Void,ArrayList<GalleryItem>>{
@Override
protected ArrayList<GalleryItem> doInBackground(Void... params) {
Log.d(TAG,"doInBackground");
return new PhotosFetchr().fetchItems();
}
@Override
protected void onPostExecute(ArrayList<GalleryItem> galleryItems) {
mItems = galleryItems;
setupAdapter();
//GalleryItemAdapter adapter = (GalleryItemAdapter)mGridView.getAdapter();
//adapter.notifyDataSetChanged();
}
}
private void setupAdapter(){
//Log.d(TAG,"mItems size = " +mItems.size());
if(getActivity() == null || mGridView == null) return;
if(mItems != null){
mGridView.setAdapter(new GalleryItemAdapter(mItems));
}else {
mGridView.setAdapter(null);
}
}
private class GalleryItemAdapter extends ArrayAdapter<GalleryItem> implements AbsListView.OnScrollListener{
public GalleryItemAdapter(ArrayList<GalleryItem> mItems) {
super(getActivity(), 0, mItems);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d(TAG,"getView" );
if(convertView == null){
convertView = getActivity().getLayoutInflater().inflate(R.layout.gallery_item,parent,false);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.gallery_item_imageView);
imageView.setImageResource(R.mipmap.icon1);
GalleryItem item = getItem(position);
imageView.setTag(item.getPhotoName());
//mPhotoLoader.queueLoader(imageView,item.getPhotoName());
return convertView;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
Log.d(TAG,"onScrollStateChanged scrollState = " + scrollState );
if(scrollState == SCROLL_STATE_IDLE){
loadImage(mFirstVisibleItem,mVisibleItemCount);
}else{
mPhotoLoader.clearQueue();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
Log.d(TAG,"onScroll" +" firstVisibleItem = "+firstVisibleItem+" visibleItemCount = "+visibleItemCount);
mFirstVisibleItem = firstVisibleItem;
mVisibleItemCount = visibleItemCount;
//首次進入時加載圖片
if(mIsFirstIn && visibleItemCount > 0){
loadImage(mFirstVisibleItem,mVisibleItemCount);
mIsFirstIn = false;
}
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
mPhotoLoader.clearQueue();
}
@Override
public void onDestroy(){
super.onDestroy();
mPhotoLoader.quit();
Log.d(TAG, "backgrounp thread destroyed");
}
public void loadImage(int firstVisibleItem,int visibleItemCount){
Log.d(TAG,"loadImage");
for(int i =firstVisibleItem;i<firstVisibleItem + visibleItemCount;i++){
String photoName = mItems.get(i).getPhotoName();
ImageView imageView = (ImageView) mGridView.findViewWithTag(photoName);
mPhotoLoader.queueLoader(imageView,photoName);
}
}
}
你應該給listview設置監聽器,你只說給它設置了適配器。mGridView.setOnScrollListener(new GalleryItemAdapter(mItems));
為什麼用適配器實現監聽接口?