Android ListView – Issue with Lazy Loading Images

androidimagejavalazy-loadinglistview

This is a very common scenario: displaying images in a ListView which have to be downloaded from the internet.

Right now I have a custom subclass of ArrayAdapter which I use for the ListView. In my getView() implementation of the ArrayAdapter, I spawn a separate thread to load an image. After the loading is done, it looks up the appropriate ImageView and sets the image with ImageView.setImageDrawable(). So the solution I used is kind of similar to this one: Lazy load of images in ListView

The problem I'm having is that as soon as I make the call to setImageDrawable() on the ImageView, the ListView somehow refreshes all currently visible rows in the list! This results in kind of an infinite loop:

  1. getView() is called
  2. thread is spawned to load image
  3. image is loaded; setImageDrawable() is called on ImageView
  4. ListView picks it up for some reason and refreshes itself
  5. For the ListView to refresh, getView() is called for each visible row, so we go back to step 1 and the whole thing repeats itself

So as far as I can see, the solution proposed in "Android – How do I do a lazy load of images in ListView" (see link above) simply doesn't work. It might look like it does, but it will run very slow because in the background, it keeps reloading the currently visible rows.

Did anyone encounter this before and/or have a solution for this?

Best Answer

I used the code in following link : another stackoverflow question

i made small changes in order to solve recycling view problem.i set the url of image to Tag of imageview in the adapter. Following code contains my solution that solves recycling problem:

public void fetchDrawableOnThread(final String urlString, final ImageView imageView,Drawable drw) {

    imageView.setImageDrawable(drw);//drw is default image
    if (drawableMap.containsKey(urlString)) {
        if(imageView.getTag().toString().equals(urlString))
        {
            imageView.setImageBitmap(drawableMap.get(urlString));
            imageView.invalidate();
            return;
        }

    }

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            BitmapWrapper wrapper = (BitmapWrapper)message.obj;
            if(wrapper.imageurl.equals(imageView.getTag().toString()))
            {
                imageView.setImageBitmap((Bitmap)wrapper.bitmap);
                imageView.invalidate();
            }

        }
    };

    Thread thread = new Thread() {
        @Override
        public void run() {
            //TODO : set imageView to a "pending" image

            Bitmap drawable = fetchDrawable(urlString);
            BitmapWrapper wrapper = new BitmapWrapper();
            wrapper.bitmap = drawable;
            wrapper.imageurl = urlString;
            Message message = handler.obtainMessage(1, wrapper);
            handler.sendMessage(message);
        }
    };
    thread.start();
}


    public class BitmapWrapper
{
    public Bitmap bitmap;
    public String imageurl;
}
Related Question