question

njsokalski avatar image
0 Votes"
njsokalski asked RobCaplan edited

Scrolling Before Finding: Refreshing the RecyclerView

I have a RecyclerView that, like many RecyclerView(s), scrolls items offscreen. If I call FindViewHolderForAdapterPosition for an item that is currently offscreen, I receive a NullReferenceException, which is to be expected. When this is the scenario, I want to scroll the item back into view (which I do using SmoothScrollToPosition), and then call FindViewHolderForAdapterPosition. However, FindViewHolderForAdapterPosition still returns a NullReferenceException until the next round of events (the user presses the button again). How can a make the RecyclerView recognize that the item is now visible (or onscreen or whatever you want to call it)? Is there some kind of refresh method I need to call? The Notify methods are obviously not appropriate because this is not a data updating scenario. Any ideas?

dotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, njsokalski. When the recyclerView's item is scrolled outside of the screen, the item is recycled and viewHolder is null. We cannot get the corresponding item view via FindViewHolderForAdapterPosition method. To keep the item when it's scrolled out side the screen, the only way is to cancel the recycle function. But it's not recommented to do that.

public void onBindViewHolder(final CommentViewHolder viewHolder, final int position) {
    viewHolder.setIsRecyclable(false);
}

If you want to update the itemView's data, you could just get the adapter of the recyclerView to call the Adapter.NotifyItemChanged(index) to perform the work. If you're using the nested recyclerView, please create the adapter instances of the child recyclerView in the Activity class to make the adapters available.

0 Votes 0 ·

0 Answers