Android how can I get current positon on recyclerview that user scrolled to item


In my RecyclerView I have some items that user can scroll and see that. Now I want to save this position and scroll that after come back. This below code return 0 always and I can’t save that:

recyclerMarketLists.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
    public void onScrollChanged() {
        recyclerViewCurrentScrolledPosition = recyclerMarketLists.getScrollY();
        Log.e("Y: ", recyclerViewCurrentSceolledPosition + "");

the Logcat:

07-07 18:28:30.919 2124-2124/com.sample.presentationproject E/Y:: 0


You are trying to get the info on the wrong object. It is not the RecyclerView nor the Adapter responsibility but the RecyclerView’s LayoutManager.

Instead of the generic ViewTreeObserver.OnScrollChangedListener() I would recommend to add instead the RecyclerView.OnScrollListener and use the onScrollStateChanged(RecyclerView recyclerView, int newState) callback which gives you the newState, you should use SCROLL_STATE_IDLE to fetch its position. Meaning:


As Rik van Velzen pointed out, you probably need to cast your
LayoutManager to a LinearLayoutManager or GridLayoutManager
(you have to cast to the correct type you are using) to access these
findVisibleXXXX() methods.

On said callback method. Hope I made this clear enough for your, you can find documentation on the classes here:


yigit’s (Google) response on visible positions

Answered By – Joaquim Ley

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published