Current Absolute RecyclerView Scroll Position

Nathan Sokalski 4,111 Reputation points
2022-05-18T23:07:07.747+00:00

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?

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2022-05-19T01:27:51.913+00:00

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.