How to prevent reloading of images in listview with custom adapter when refresh listview

核能气质少年 提交于 2019-12-11 09:07:56

问题


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.



来源:https://stackoverflow.com/questions/30068676/how-to-prevent-reloading-of-images-in-listview-with-custom-adapter-when-refresh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!