问题描述
- OnScrollListener中的方法没有执行
-
给一个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));
为什么用适配器实现监听接口?
解决方案二:
Silverlight 中 OnApplyTemplate方法没有执行
时间: 2024-10-27 05:47:38