主要问题:
在使用 ViewHolder 模式的简单 ListView 适配器中延迟加载远程图像的最有效无错误方法是什么?
我目前有一个实现将首先检查 SoftReference Bitmap HashMap 以获取图像的软缓存版本。如果失败,我会检查我的硬盘缓存以获取图像副本。如果失败,我会从网上获取。我在单独的线程和队列中执行所有这些操作,以消除并发或重复下载。
问题出在通过回调加载时。因为我使用 ViewHolder 模式,所以我的 View 不断被回收,而且我还没有找到一种可靠的方法来消除随机附加到我的 ImageViews 的不同图像。我在每次加载前都默认使用默认图像,但是因为 View 被快速回收,所以“旧”监听器应用到我的 ImageView 上,提供错误的图像,然后用正确的图像替换。
我发现的唯一半可靠解决方案是使用 ViewHolder 本身作为监听器,但这只会使问题不那么明显。它仍然发生在快速滚动中。
如有任何帮助,我们将不胜感激。
更新:
最佳答案
我已经找到了图像切换问题的解决方案,我将在下面提供一个代码块。不过,我不会接受它,因为我不知道这是否是最有效的方法,这是我最初的问题。如果正确实现,它将完美运行。
public void getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
...
String imagePath = insertImageUrlHere();
Object tag = holder.userThumb.getTag();
if (tag != null && tag.equals(imagePath)) {
// Do nothing
} else {
holder.userThumb.setTag(imagePath);
holder.userThumb.setImageResource(R.drawable.default_image);
AsynchronousImageLoadingUtility.load(imagePath, holder);
}
...
return convertView;
}
private static class ViewHolder implements AsynchronousImageLoadingUtilityCallback {
private ImageView userThumb;
@Override
public void onImageLoad(String source, Bitmap image) {
if (image != null && userThumb.getTag().equals(source)) {
userThumb.setImageBitmap(image);
}
}
}
关于android - 具有 ViewHolder 模式的 ListView 中的远程图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7667129/