Reputation: 5495
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.
Upvotes: 7
Views: 11805
Reputation: 1850
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 View
s 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.
Upvotes: 13