使用Android Volley Network库的Gridview滚动问题

我使用GridView从服务器显示一些图像行。 对于异步图像加载,我使用新的网络库Volley。 最初图像加载工作正常,但只要我上下滚动图像随机显示在错误的网格单元格内。 但TextView内容始终正确显示。

我的适配器代码:

class BadgeGalleryAdapter extends BaseAdapter {

        private LayoutInflater mInflater;
        private ImageLoader mImageLoader;

        // Constructor
        public BadgeGalleryAdapter(Context c) {
            mInflater = (LayoutInflater) c
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mImageLoader = ImageCacheManager.getInstance().getImageLoader();
        }

        @Override
        public int getCount() {
            return mBadgeList.size();
        }

        @Override
        public Object getItem(int position) {
            return mBadgeList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder viewHolder;

            // Use ViewHolder pattern to only inflate once
            if (convertView == null) {

                convertView = mInflater.inflate(R.layout.user_badge_grid_item,
                        parent, false);

                viewHolder = new ViewHolder();
                viewHolder.badgeImage = (ImageView) convertView
                        .findViewById(R.id.grid_badge_image);
                viewHolder.badgeName = (TextView) convertView
                        .findViewById(R.id.grid_badge_name);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            Badge badge = (Badge) getItem(position);
            if (badge != null) {
                           //using Volley ImageLoader
               mImageLoader.get(badge.imageUrl, ImageLoader
                        .getImageListener(viewHolder.badgeImage, 0, 0));
               viewHolder.badgeName.setText(badge.name);
            }
            return convertView;

        }
    }

    static class ViewHolder {
        ImageView badgeImage;
        TextView badgeName;
    }

使用一点解决方法Volley对我的Gridview适配器工作得很好。 如果用户已经解锁徽章,我通过Volley NetworkImageView加载它。 如果没有,我会显示一个占位符图像。

...
 Badge badge = (Badge) getItem(position);
    if (badge != null) {
        // check if user already unlocked this badge
        if (badge.unlocked && Utils.isUrl(badge.imageUrl)) {
            viewHolder.badgeImage.setImageUrl(badge.imageUrl,
                    ImageCacheManager.getInstance().getImageLoader());
        } else {
            // explicity set placeholder image to avoid messy image recycling issue
            viewHolder.badgeImage.setImageUrl(badge.placeholderUrl,
                    ImageCacheManager.getInstance().getImageLoader());
        }

        viewHolder.badgeName.setText(badge.name);
    }
...

我也在我的ListAdapters中使用Volley的NetworkImageView。 它对我来说也很好。

链接地址: http://www.djcxy.com/p/50599.html

上一篇: Gridview scrolling issue using Android Volley Network library

下一篇: Android adapter (recycler views) with assyncTask image downloader