How to get the Scrollposition in the Recyclerview/Layoutmanager?

后端 未结 7 1064
旧时难觅i
旧时难觅i 2020-12-23 20:12

How to get the scrollposition in Recyclerview or the Layoutmanager?

I can measure the scrollposition by adding an OnScrollListener

7条回答
  •  青春惊慌失措
    2020-12-23 20:54

    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.

提交回复
热议问题