OnBindViewHolder Not Called When Offscreen ViewHolder(s) Scrolled Back Into View

Nathan Sokalski 4,126 Reputation points
2021-08-19T03:54:06.047+00:00

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.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Nathan Sokalski 4,126 Reputation points
    2021-08-19T18:43:50.64+00:00

    I finally tried the following:

    if (rvScores.FindViewHolderForAdapterPosition(prevplayer) == null)
    { rvScores.GetAdapter().NotifyItemChanged(prevplayer); }
    else
    { (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.

    0 comments No comments

  2. JarvanZhang 23,951 Reputation points
    2021-08-20T06:26:42.06+00:00

    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.

    0 comments No comments