Android LRU缓存没有显示正确的位图



我正在制作一个android应用程序,其中有一个缩略图的新闻文章。这些缩略图从网络加载并存储在LruCache中,URL作为键,位图作为值。

private LruCache<String, Bitmap> tCache;

在适配器的getView()方法中,我调用getThumbnail(),它检查缓存(必要时从网络加载),然后显示缩略图。

public void populateList(){
    ...
    new Thread(new Runnable() {
        @Override
        public void run() {
            getThumbnail(story, thumbnail);
        }
    }).start();
}

private Bitmap getThumbnail(Story story, ImageView imageView) {
    String url = story.getThumbnail();
    Bitmap bitmap;
    synchronized (tCache) {
        bitmap = tCache.get(url);
        if (bitmap == null) {
            bitmap = new ImageLoadingUtils(this, imageView).execute(url,
                    Boolean.TRUE).get();
            tCache.put(url, bitmap);
        }
    }
    return bitmap;
}

ImageLoadingUtils从网络加载,并在完成后将结果位图放在ImageView中。

@Override
protected void onPostExecute(Bitmap result) {
    if (imageView != null) {
        imageView.setImageBitmap(result);
        adapter.notifyDataSetChanged();
    }
}

问题是当我向下滚动时缩略图在同一个ListView中重复。

<>之前________| IMAGE1 || IMAGE2 || IMAGE3 |屏幕| IMAGE4 |--------| IMAGE1 || IMAGE2 |幕后________

当我向下滚动然后向上滚动时,文章不再有正确的缩略图。这真是一团糟。

有人能发现这个问题吗?非常感谢。

这个问题是因为Views在listview中被重用。下面是一个关于如何在listview中异步缓存和加载缩略图的好例子。

Lazy Load ListView Android

最新更新