question

njsokalski avatar image
0 Votes"
njsokalski asked YonglunLiu-MSFT commented

OnDraw Using Coordinates of Elements Scrolled Offscreen

I have a RecyclerView (which uses a GridLayoutManager) inside a HorizontalScrollView. I do this to allow scrolling in both directions. When the user scrolls (either direction), I need to get the position of the selected element in the RecyclerView (I keep track of the selected element using an ItemClick event). I get the position using the GetLocationOnScreen method. This works fine when the element is onscreen, but when it gets scrolled offscreen, I end up with problems. What I do with the data from GetLocationOnScreen is use it in the OnDraw override of a custom view. However, because the values from GetLocationOnScreen are negative when the element is offscreen, the result does not display as expected. What should I do?



dotnet-xamarin
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.

1 Answer

YonglunLiu-MSFT avatar image
0 Votes"
YonglunLiu-MSFT answered YonglunLiu-MSFT commented

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.




· 3
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.

when to recycle item views that are no longer visible to the user

Is there a way to "disable" recycling? If there is, would that possibly fix the problem? I understand that recycling is sort of like RecyclerView's "claim to fame" and supposedly makes it more efficient, but for my app, that is not significant and the scrolling and knowing the position is much more important. If there was a way to get the scroll positions of the HorizontalScrollView & RecyclerView, I could probably find a way to calculate the position using that.

0 Votes 0 ·

I've updated my answer about how to disable recycling.

0 Votes 0 ·

May I know if you have got any chance to check my answer? I am glad to help if you have any other questions.

0 Votes 0 ·