LazyLoad图像到GalleyView - Android

时间:2011-09-24 11:47:27

标签: android load gallery lazy-evaluation

我已经搜索过,并没有遇到任何对我面临的问题有用的东西。

我从远程位置获取图像并在需要时将它们存储在本地缓存中,当下载图像时,我使用延迟加载来更新请求它的ImageView。一切正常,但是当来自Gallery适配器的请求时,一旦下载完成,它似乎不仅仅更新请求它的imageView,而是更新整个GalleryView。这真令人讨厌,好像用户滚动浏览图库时,当其中一个需要的图像准备好显示时,它将跳回到最后的已知位置。

如果我只是将画廊拖到一边并且刚刚进入视野的新图像已经准备好它会跳回到最后一个已知的位置并且我必须再次进行再次拖拽等等,也会发生同样的情况。 ..

那么有没有人知道在库中更新单个imageView而不影响用户滚动的任何解决方法?

1 个答案:

答案 0 :(得分:0)

我有一个例子(我前段时间写过,并在这里抽象):

public class MyAdapter ... {

    // you don't need a weak reference necessarily, in my case it was a more
    // common solution so I had to do that
    private List<WeakReference<ImageView>> mShownImageViews =
        new LinkedList<WeakReference<ImageView>>();

    public View getView(...) {
        if (view == null) {
            // create your view
            mShownImageViews.add(new WeakReference(theImageViewToUpdate));
        }

        // set your data

        // If you set null the image view will never be updated
        // you have to set the image here then
        imageView.setTag(imageAvailable ? null : "someUniqueIdForYourImage");
    }

    public void imageLoaded(String someUniqueIdForYourImage, Bitmap theImage) {
        for (WeakReference<ImageView> r : mShownImageViews) {
            ImageView iv = r.get();

            if (iv != null && iv.getTag() != null
                    && iv.getTag().equals(someUniqueIdForYourImage)) {
                iv.setImageBitmap(theImage);
                iv.setTag(null);
        }
    }
}
相关问题