RattusRattus RattusRattus - 4 months ago 54
Android Question

Android - How to stop images reloading in Recyclerview when scrolling

I was creating an app for loading images from internet in cards. I am using Recyclerview for listing purpose. For downloading images, I have tried Picasso, universal-image-downloader and other custom methods but everywhere I have this problem:

Whenever I scroll down and up the images in imageview are getting reloaded. I have even kept a condition to check if the imageview contains an image but that also doesn't seem to work or my code may be faulty. But I am not able to find a solution to this problem. Below I have attached the 'onBindViewHolder' function of my code which I think is the problematic part. Here images is a string arraylist containing image urls.

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.default_card, parent, false);

ViewHolder viewHolder;
viewHolder = new ViewHolder(view);

return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String url = images.get(position);

if (holder.imgView.getDrawable() != null) {
//Do Nothing
} else {
ImageLoader.getInstance().displayImage(url, holder.imgView);
}
}

Answer

At first, there is a bug in your code - you have to remove the condition from onBindViewHolder. All ImageView instances has to be updated each time they are about to display.

@Override
public void onBindViewHolder(ViewHolder holder, int position) {            
    String url = images.get(position);               
    ImageLoader.getInstance().displayImage(url, holder.imgView);               
}

The reloading part:

The problem is, that you are probably not using any cache (in-memory, or disk based). I would not elabarote on this a lot, caching of images in collection views is a really common problem and I'm sure a quick StackOverflow search will give you a lot of results.

Just in particular - Universal image does not have caching enabled by default You can turn it on this way.

DisplayImageOptions options = new DisplayImageOptions.Builder()
    ...
    .cacheInMemory(true)
    .cacheOnDisk(true)
    ...
    .build();
ImageLoader.getInstance().displayImage(imageUrl, imageView, options); // Incoming options will be used

Or you can set it globally for your application, just check the docs.