I have a RecyclerView that contains ViewHolder(s) whose appearance needs updated at certain points when I call NotifyItemChanged(x). However, if the ViewHolder has been scrolled offscreen so that it is no longer visible, FindViewHolderForAdapterPosition returns null, therefore preventing me from changing it. I would expect OnBindViewHolder to update this previously hidden ViewHolder when it is scrolled back into view. However, this does not seem to be happening (it looks exactly the same as it did when it was scrolled offscreen). This problem does not occur when the ViewHolder is still visible, since FindViewHolderForAdapterPosition does not return null. How can I either force the ViewHolder to be rebound when it comes back into view? Thanks.
Hi, njsokalski. In RecyclerView, the item view will be recycled when an item is out of the screen. The viewHolder of the hidden item is null. What are you want to do with the viewHolder? If you want to get the item data, you could get the info from the data list. If to update the view, just call the notify command and the view will be updated when the view scrolls to the screen.
I don't know if you want to call this updating the view or not, but here goes. The ViewHolder contains another RecyclerView, and I need to update a ViewHolder in that RecyclerView. However, I cannot call NotifyItemChanged on that inner RecyclerView because it is a property of the ViewHolder that would have been returned. Here is the line of code that I attempt to use:
(rvScores.FindViewHolderForAdapterPosition(prevplayer) as ScoresViewHolder)?.rvPoints.GetAdapter().NotifyItemChanged(prevround);
The part that returns null is the following:
rvScores.FindViewHolderForAdapterPosition(prevplayer)
And the inner RecyclerView (the one containing the ViewHolder that needs updated) is:
(rvScores.FindViewHolderForAdapterPosition(prevplayer) as ScoresViewHolder).rvPoints
However, none of the actual data in rvPoints gets changed, but the value of a variable used in OnBindViewHolder does. I don't know if this would cause NotifyItemChanged to be ignored (but since the null propagation prevents NotifyItemChanged anyway, I don't know for sure).
I finally tried the following:
if (rvScores.FindViewHolderForAdapterPosition(prevplayer) == null)
{ rvScores.GetAdapter().NotifyItemChanged(prevplayer); }
{ (rvScores.FindViewHolderForAdapterPosition(prevplayer) as ScoresViewHolder).rvPoints.GetAdapter().NotifyItemChanged(prevround); }
This may be somwhat inefficient since it recreates (or recycles) the entire ViewHolder containing the inner RecyclerView, but since it is offscreen where the user can't see it I guess it isn't a big deal.
Hello,
Welcome to our Microsoft Q&A platform!
When the item is scrolled out of the screen, the item is recycled and viewHolder is null. So it's not a good idea to get the recyclerView in that case and get the adapter via the recyclerView. To avoid this, try creating the adapters of the child recyclerViews in the Activity class. You could get the corresponding adapter to call the Adapter.NotifyItemChanged(index) directly.
Best Regards,
Jarvan Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.