我有一个显示两列 ImageView 的 GridView 。我正在使用异步任务加载这些图像(请参阅这篇文章 Lazy load of images in ListView)
但是当我滚动 gridview 时,位置中的图像正在混合。例如第 14 张图像显示第 1 张图像,我认为 View 试图在异步任务完成之前显示旧图像。
我的代码:
public Content getItem(int position) {
return contents.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public boolean hasStableIds() { return true; }
public View getView(int position, View convertView, ViewGroup parent) {
Thumbnail contentView;
Content current = contents.get(position);
if (convertView == null) {
contentView = new Thumbnail(mContext);
}
else {
contentView = (Thumbnail) convertView;
}
contentView.title = current.getTitle();
contentView.image = current.getImage();
contentView.link = current.getLink();
contentView.init();
return contentView;
}
初始化函数
TextView titleText = (TextView)super.findViewById(R.id.titleText);
titleText.setText(title);
ImageView imageControl = (ImageView)super.findViewById(R.id.thumbImage);
DrawableManager loadImage = new DrawableManager(); loadImage.fetchDrawableOnThread(imgUrl, imageControl);
等待您的帮助
谢谢
最佳答案
它的发生是因为资源重用。你应该做什么:
首先,只需在 getView 方法中为您的 imageView (
contentView.image.setImageResource(DEFAULT_RESOURCE)
) 设置某种默认图片(过渡默认图片总比错误图片好)。为您的图像设置唯一标签,例如要加载的图像的位置或 url (
contentView.image.setTag(url)
)。 当 AsyncTask 完成时,您可以使用一些检查,例如String url=(String)imageView.getTag(); if (url.equals(mUrl)) { //mUrl can be transmitted to AsyncTask instance separately mActivity.runOnUiThread(new Runnable() { @Override public void run() { imageView.setImageDrawable(bmp); }); }); }
它是必需的,因为 Adapter
不会为 N 个项目的所有 N 个 ImageView
分配内存。它只存储可见项目所需的数量和一些保留的 ImageView。因此,无法保证您存储的 ImageView
引用在几秒钟内是真实的,因为它可能变得不可见并被可见的引用重用。
关于Android - Gridview 图像在滚动时混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16298604/