Reputation: 1451
I tried doing lazy loading of images from web on a listview by taking hints from this The only problem is that when views are being recycled by the list adapter it results in loading of wrong image for the first row in the list. When i prevented the adapter from recycling views it worked fine but its not the most efficient way of doing a listview. Is there a way where we can do lazy loading of images in a listview with the views for rows being recycled?
Upvotes: 0
Views: 1881
Reputation: 8772
We can avoid wrong image getting loaded by setting URL on the imageview as tag. Every time image is loaded we need to cross check the URL of image being loaded with requested image. Look at the solution below
Activity: where image for image - view are requested
if( referenceImageURL.length()!=0){
//Store the image URL as Tag on the image view
holder.refImageView.setTag(referenceImageURL);
// use the image loader to download and load the image
//pass the image-view and the URL to image loader
imageManager.loadImage(referenceImageURL,holder.refImageView);
}else{
//URL is empty for this list-item
//set null on Tag and load default image here
holder.refImageView.setTag(null);
...
}
ImageManager: Responsible for downloading and caching images
//after image is downloaded, before loading it to the view
//cross check if this image-view is recycled if yes,
//getTag returns different URL than the one which is used to download image
String url = (String) m_imageViewRef.get().getTag();
if(url != null && url.compareTo(m_url) == 0 ){
m_imageViewRef.get().setImageBitmap(result);
Use the same concept before loading cached image to image-view
Upvotes: 0