Hello,
This works fine when the element is onscreen, but when it gets scrolled offscreen, I end up with problems.
Referring to the documentation RecyclerView.LayoutManager, we could find that:
A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user. By changing the LayoutManager a RecyclerView can be used to implement a standard vertically scrolling list, a uniform grid, staggered grids, horizontally scrolling collections and more. Several stock layout managers are provided for general use.
It means that RecylerView
has only views visible on the screen. All off-screen ViewHolders are detached and stored in RecyclerView.RecycledViewPool until they're on-screen again, and they are not laid out at all.
That's the reason the issue occurs.
If you want to disable recycling, you could use ViewHolder.setIsRecyclable(false);
, however, doing so will cause RecycleView to lose its own characteristics and is not recommended
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.