How to prevent reloading of images in listview wit

2019-08-06 16:37发布

问题:

I am using displayImage() method of UniversalImageLoader library in the adapter class, which sets images(from url) in the list items. i want if i reset adapter in my listview, images should not reload if the urls are same as previous urls.

回答1:

There is two solution already posted in the issue section of Universal Image Loader.

Solution #1:

You can use custom displayer:

new FadeInBitmapDisplayer(300) {

  @Override
            public Bitmap display(Bitmap bitmap, ImageView imageView, LoadedFrom loadedFrom) {
                if (loadedFrom != LoadedFrom.MEMORY_CACHE) {
                    return super.display(bitmap, imageView, loadedFrom);
                } else {
                    imageView.setImageBitmap(bitmap);
                    return bitmap;
                }
            }

        }

Solution #2:

 BitmapDisplayer displayer = new FadeInBitmapDisplayer(500) {

        @Override
        public Bitmap display(Bitmap bitmap, ImageView imageView,
                LoadedFrom loadedFrom) {
            if (loadedFrom != LoadedFrom.MEMORY_CACHE) {
                return super.display(bitmap, imageView, loadedFrom);
            } else {
                imageView.setImageBitmap(bitmap);
                return bitmap;
            }
        }

    };
    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true).resetViewBeforeLoading(true)
            .showImageForEmptyUri(R.drawable.thumbnail_no_image)
            .showImageOnFail(R.drawable.thumbnail_no_image)
            .displayer(displayer).build();
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            context).defaultDisplayImageOptions(options)
            .memoryCacheSize(2 * 1024 * 1024).build();
    sLoader.init(config);


回答2:

I faced the same problema and went with a solution as follows

Inside the getview method after declaring your imageview try the following line as first line

myImageView.setImageResource(R.drawable.adefaultimage);

this will first show a defaultimage in the imagview and will avoid duplication of images till imageloader loads the real one



回答3:

To achieve this :

Use android Lru cache in your list adapter. First time its looking very complex but its have more benifits.

Using Lru cache your image store in cache and when it will display then check from cache if it will exist then it will not down load and it will use from stored cache memory. You can also give cache memory size for your application and clear it.

Below have some links:

Tutorials:

  1. http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

  2. http://developer.android.com/reference/android/util/LruCache.html

Example :

  1. http://android-er.blogspot.in/2012/07/caching-bitmaps-with-lrucache.html

  2. http://android-er.blogspot.in/2012/07/apply-lrucache-on-gridview.html



回答4:

I tried above solutions but not find usefull, Finally i solved my problem by saving images from url to device(as bitmap) then get in list view from there.