Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save scroll position of RecyclerView in Android?

Tags:

I have Recycler view which lays inside of SwipeRefreshLayout. Also, have ability to open each item in another activity. After returning back to Recycler I need scroll to chosen item, or to previous Y. How to do that?

Yes, I googled, found articles in StackOverFlow about saving instance of layout manager, like this one: RecyclerView store / restore state between activities. But, it doesn't help me.

UPDATE

Right now I have this kind of resolving problem, but, of course, it also doesn't work.

private int scrollPosition;  ...//onViewCreated - it is fragment recyclerView.setHasFixedSize(true); LinearLayoutManager llm = new LinearLayoutManager(getActivity()); recyclerView.setLayoutManager(llm); data = new ArrayList<>(); adapter.setData(getActivity(), data); recyclerView.setAdapter(adapter); ...  @Override public void onResume() {     super.onResume();     recyclerView.setScrollY(scrollPosition); }  @Override public void onPause() {     super.onPause();     scrollPosition = recyclerView.getScrollY(); } 

Yes, I have tried scrollTo(int, int) - doen't work.

Now I tried just scroll, for example, to Y = 100, but it doesn't scrolling at all.

like image 477
DefaultXYZ Avatar asked Apr 12 '16 08:04

DefaultXYZ


People also ask

How scroll RecyclerView to bottom in android programmatically?

mLinearLayoutManager = new LinearLayoutManager(this); recyclerView. setLayoutManager(mLinearLayoutManager); 3). On your Button onClick , do this to scroll to the bottom of your RecyclerView .

How do I scroll to the top of RecyclerView?

In the above code we have added recycler view to window manger as relative parent layout and add FloatingActionButton. FloatingActionButton supports CoordinatorLayout. So we have used parent layout is CoordinatorLayout. When you click on FloatingActionButton, it will scroll to top position.

How scroll horizontal RecyclerView programmatically in Android?

For these methods to work, the LayoutManager of the RecyclerView needs to have implemented these methods, and LinearLayoutManager does implement these in a basic manner, so you should be good to go. @SoliTawako I guess scrollTo(int x, int y) you can also scroll view, but in a more fine-tuned way.


2 Answers

Save the current state of recycle view position @onPause:

    positionIndex= llManager.findFirstVisibleItemPosition();     View startView = rv.getChildAt(0);     topView = (startView == null) ? 0 : (startView.getTop() - rv.getPaddingTop()); 

Restore the scroll position @onResume:

    if (positionIndex!= -1) {         llManager.scrollToPositionWithOffset(positionIndex, topView);     } 

or another way can be @onPause:

long currentVisiblePosition = 0; currentVisiblePosition = ((LinearLayoutManager)rv.getLayoutManager()).findFirstCompletelyVisibleItemPosition(); 

restore @onResume:

((LinearLayoutManager) rv.getLayoutManager()).scrollToPosition(currentVisiblePosition); currentVisiblePosition = 0; 
like image 160
Madhukar Hebbar Avatar answered Oct 30 '22 20:10

Madhukar Hebbar


A lot of these answers seem to be over complicating it.

The LayoutManager supports onRestoreInstanceState out of the box so there is no need to save scroll positions etc. The built in method already saves pixel perfect positions.

example fragment code (null checking etc removed for clarity):

private Parcelable listState; private RecyclerView list;  @Override public void onCreate(@Nullable Bundle savedInstanceState) {     super.onCreate(savedInstanceState);      listState=savedInstanceState.getParcelable("ListState");  }  @Override public void onSaveInstanceState(Bundle outState) {     super.onSaveInstanceState(outState);      outState.putParcelable("ListState", list.getLayoutManager().onSaveInstanceState());  } 

then just call

list.getLayoutManager().onRestoreInstanceState(listState); 

once your data has been reattached to your RecyclerView

like image 36
Kuffs Avatar answered Oct 30 '22 22:10

Kuffs