Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView - bind the view holders again

TL,DR : How can I force the recycler view to call the onBindViewHolder method again, at least for the visible items?

Calling notifyDataSetChanged() will make the list lag for some milliseconds, is there a better way? Thanks.

I have a layout with an ImageView. When ever bind is called for the imageview, I send a request to a server in order to get an image. When the image is loaded, I save the bitmap in the ViewHolder , T variable. And in the bind method I check if variable.getBitmap() is null or not, and if it is I will set the imageview. Now If I scroll my list the images are going to be loaded, but If not, the imageviews are still blank, because onBindViewHolder wasn't called again.

Thanks.

like image 828
Boldijar Paul Avatar asked Feb 10 '23 12:02

Boldijar Paul


1 Answers

notifyDataSetChanged() is definitely the right way. Maybe notifyItemChanged() is even better because it only binds the selected item. If I get your question right, it seems that you do some things on the Main Thread which shouldn't be done there. Remember: Never do potential lengthy actions on the Main Thread, but always use something like AsyncTask.

I think it is a better approach to download the images asynchronously and then cache it, so you don't have to download it every time your Views are recycled. There are libraries for it.

While the images are loading you could show a ProgressBar or something else which signalizes the user, that the image is being loaded.

like image 75
rubengees Avatar answered Feb 13 '23 00:02

rubengees