使用Universal Image Loader在Android网格视图中滚动时重复图像

时间:2014-03-29 04:06:20

标签: android image gridview universal-image-loader

我正在尝试使用以下代码在gridview上使用Universal Image Loader从Internet加载图像。

public View getView(int position, View converView, ViewGroup parent) {
        Log.v("Description", "Description is " + position);
        ViewHolder mVHolder;
        if (converView == null) {
            LayoutInflater vi = (LayoutInflater) conted.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            converView = vi.inflate(R.layout.customgrid, null);
            mVHolder = new ViewHolder();
            mVHolder.mImageView = (ImageView) converView
                    .findViewById(R.id.imgview);
            mVHolder.mTextView1 = (TextView) converView
                    .findViewById(R.id.textView1);
            mVHolder.mTextView2 = (TextView) converView
                    .findViewById(R.id.textView2);
            mVHolder.mTextView3 = (TextView) converView
                    .findViewById(R.id.textView3);

            converView.setTag(mVHolder);
        } else {
            mVHolder = (ViewHolder) converView.getTag();
        }
        // mVHolder.mImageView.setImageResource(mThumbIds[position]);
        // mVHolder.mImageView.setImageDrawable(LoadImageFromURL(
        // mThumbIds[position]));
        imageLoader.displayImage(mThumbIds[position],mVHolder.mImageView,options, animationListener);
        Log.v("Names",NAMES[position] + STATES[position] + CONSTITUENCY[position]);
        mVHolder.mTextView1.setText(NAMES[position]);
        mVHolder.mTextView2.setText(STATES[position]);
        mVHolder.mTextView3.setText(CONSTITUENCY[position]);
        return converView;
    }

但是,当我在列表中向下滚动时,第一行的第一个图像似乎在大多数网格中重复出现。它不会一直停留 - 它会在一段时间后变回原始图像。我的问题是,这是关于通用图像加载器的视图通胀或通用图像加载器的图像加载的问题吗?

2 个答案:

答案 0 :(得分:15)

我认为您的问题是您没有为此resetViewBeforeLoading()设置DisplayImageOptions方法: -

     DisplayImageOptions options = new DisplayImageOptions.Builder()
    .showStubImage(R.drawable.stub_image)
    .showImageForEmptyUri(R.drawable.image_for_empty_url)
 ---->   .resetViewBeforeLoading(true)<----very important for recycling views
    .cacheInMemory()
    .cacheOnDisc()
    .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default
    .bitmapConfig(Bitmap.Config.ARGB_8888) // default
    .delayBeforeLoading(1000)
    .displayer(new SimpleBitmapDisplayer()) // default
    .build();

然后应用此DisplayImageOptions。

        imageLoader.displayImage(mThumbIds[position],mVHolder.mImageView,options,                   animationListener);

答案 1 :(得分:1)

另一个解决方案是使用setTag()来识别视图:

class ViewHolder {
    ImageView thumbnail;
    public boolean isSameView(String tag) {
        if (tag == null) {
            return thumbnail.getTag() == null;
        }
        return tag.equals(thumbnail.getTag());
    }
}


protected void imageLoader(ViewHolder holder, String imageUrl) {
    if (!holder.isSameView(imageUrl)) {  // If image is loaded correctly skip else reload and set new image
        holder.thumbnail.setTag(imageUrl);
        imageLoader.displayImage(imageUrl, holder.thumbnail, new SimpleImageLoadingListener() {@
            Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                view.setTag(failReason.getType().name()); // You can handle errors and avoid repeating requests that return error codes
            }
        });
    }
}