问题
I have a recycler view which has an Imageview in the Viewholder. The Image is downloaded via AsyncTask in the onBindViewHolder in the adapter.
The problem is when I scroll down or up, the onBindViewHolder is called for recycling the view holder and the Images are downloaded again and again. Now I understand that this is how Recyclerview is supposed to work. So can anyone tell me if anything can be done to avoid downloading the Images more than once. Or will it be better to move back to ListView.
回答1:
Use Picasso and cache the image. And I guess Picasso Cache the image by default.
Example
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
回答2:
hold List of Bitmap or Drawable in adapter's field and get image from that list. You can take image's index by getLayoutPosition() method in onBindViewHolder
来源:https://stackoverflow.com/questions/34692872/downloading-image-in-recyclerview