How do I get the offset for the RecyclerView?
I want to keep the position of my RecyclerView and then reset it later. If the top item is only halfway visible, what do I use to store this offset?
For the position I am using: findFirstCompletelyVisibleItemPosition ()
To reset the position I'm using:
((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(position, offset);
But I'm not sure what to pass as offset?
+3
source to share
1 answer
You should use LinearLayoutManager
like this.
LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
int position = manager.findFirstVisibleItemPosition();
View firstItemView = manager.findViewByPosition(position);
float Offset = firstItemView.getTop();
And get the position and offset of the first element in the current window.
+8
source to share