question

njsokalski avatar image
0 Votes"
njsokalski asked njsokalski commented

Current Absolute RecyclerView Scroll Position

I need to know the absolute vertical scroll position of a RecyclerView. The ScrollY property seems to always return 0, and the dy parameter of OnScrolled in the OnScrollListener is relative, which means I would need to manually keep a cumulative total. The ScrollChangeEventArgs parameter of the ScrollChange event for ScrollView & HorizontalScrollView gives a new absolute value, is there any way to do this with RecyclerView?







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 njsokalski commented

Hello,

Generally, we could use the method getAdapterPosition to get the position of items in RecyclerView.

However, in the official documentation getAdapterPosition, we could find that:

This method is deprecated.
This method is confusing when adapters nest other adapters. If you are calling this in the context of an Adapter, you probably want to call getBindingAdapterPosition() or if you want the position as RecyclerView sees it, you should call getAbsoluteAdapterPosition().

Therefore, if you want to get the absolute position in RecyclerView, you could use the method getAbsoluteAdapterPosition.

Here is the documentation link: https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#getAbsoluteAdapterPosition().

If you want to get the scrolling distance, you can refer to computeVerticalScrollOffset to get more details.

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.





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

I think you misunderstood what I am looking for (position may have been the wrong word). I am not looking for the index, I am looking for how far (the distance) the user has scrolled.

0 Votes 0 ·

I've updated my answer.

0 Votes 0 ·
njsokalski avatar image njsokalski YonglunLiu-MSFT ·

Thanks! ComputeVerticalScrollOffset() seems to do what I need!

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 ·