3

我正在使用 aGridView来显示来自服务器的一些图像行。对于异步图像加载,我使用新的网络库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;
    }
4

1 回答 1

0

使用一点解决方法 Volley 与我的 Gridview-Adapter 配合得很好。如果用户解锁了徽章,我会通过 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。它对我来说也很好。

于 2013-11-06T10:52:17.227 回答