Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Scrollposition in the Recyclerview/Layoutmanager?

How to get the scrollposition in Recyclerview or the Layoutmanager?

I can measure the scrollposition by adding an OnScrollListener, but when the Orientation changes scrollDy is 0.

    mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            int scrollDy = 0;
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                scrollDy += dy;
            }
        });
like image 356
seb Avatar asked Apr 11 '15 18:04

seb


People also ask

How do I get the current item of the Recycler view?

First, you can listen to the scroll state changes of the Recyclerview using the OnScrollListener . If the current state is SCROLL_STATE_IDLE , then it means the user has stopped scrolling, and the view is in an idle state.

What is LayoutManager in RecyclerView?

This wear-specific implementation of LinearLayoutManager provides basic offsetting logic for updating child layout. 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.

How can show all items in RecyclerView without scrolling?

It's pretty simple, simply set the RecyclerView 's height to wrap_content . That's right.


4 Answers

For future use, If you are switching between Fragments within the same activity and all you want to do is save scroll-position for recyclerview and then restore recyclerview to the same scroll-position, you can do as follows:

In your onStop()/onDestroyView()/onPause() whatever callback is more appropriate, do this:

Parcelable recylerViewState = recyclerView.getLayoutManager().onSaveInstanceState(); 

And In your onStart()/onCreateView()/onResume() whatever callback is more appropriate, do this:

recyclerView.getLayoutManager().onRestoreInstanceState(recylerViewState); 

This way you can successfully keep your recyclerView's state in a parcelable and restore it whenever you want.

like image 144
Shoaib Anwar Avatar answered Sep 19 '22 08:09

Shoaib Anwar


You cannot get it because it does not really exist. LayoutManager only knows about the views on screen, it does not know the views before, what their size is etc.

The number you can count using the scroll listener is not reliable because if data changes, RecyclerView will do a fresh layout calculation and will not try to re-calculate a real offset (you'll receive an onScroll(0, 0) if views moved).

RecyclerView estimates this value for scrollbars, you can use the same methods from View class.

computeHorizontalScrollExtent

computeHorizontalScrollRange

computeHorizontalScrollOffset

These methods have their vertical counterparts as well.

like image 36
yigit Avatar answered Sep 18 '22 08:09

yigit


I came to the question just wanting to get the item position index that is currently scrolled to. For others who want to do the same, you can use the following:

LinearLayoutManager myLayoutManager = myRecyclerView.getLayoutManager();
int scrollPosition = myLayoutManager.findFirstVisibleItemPosition();

You can also get these other positions:

  • findLastVisibleItemPosition()
  • findFirstCompletelyVisibleItemPosition()
  • findLastCompletelyVisibleItemPosition()

Thanks to this answer for help with this. It also shows how to save and restore the scroll position.

like image 45
Suragch Avatar answered Sep 19 '22 08:09

Suragch


recyclerView.computeVerticalScrollOffset() does the trick.

like image 29
Johann Avatar answered Sep 21 '22 08:09

Johann