Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get offset for RecyclerView?

Tags:

java

android

I want to save the position of my RecyclerView, then reset it later. If the top item is only halfway visible, what do I use to save this offset?

For the position I am using: findFirstCompletelyVisibleItemPosition()

To reset the position I am using:

        ((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(position, offset);

But I am not sure what to pass as the offset?

like image 446
teiiluj Avatar asked Mar 28 '17 01:03

teiiluj


People also ask

How to get the scroll offset of a recyclerview?

by override RecyclerView onScrolled (int dx,int dy) , you can get the scroll offset. but when you add or remove the item, the value may be wrong.

What are the methods of a recyclerview?

The basic methods for a successful implementation are: onCreateViewHolder: which deals with the inflation of the card layout as an item for the RecyclerView. onBindViewHolder: which deals with the setting of different data and methods related to clicks on particular items of the RecyclerView.

What is a recyclerview card layout?

The Card Layout: The card layout is an XML layout which will be treated as an item for the list created by the RecyclerView.

What happens when ondraw is called in recyclerview?

Every time onDraw (…) is called, we go through all the Views there are in RecyclerView in a cycle and draw the passed drawable. However, the layout elements on your screen can be more complex than a list of identical elements. A screen can have: A.


1 Answers

You must use LinearLayoutManager as 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 first item in current window.

like image 108
Amit Darji Avatar answered Sep 19 '22 18:09

Amit Darji