How to focus position using Recyclerview?

Asked

Viewed 435 times

0

Hello, I’m having a problem saving the last user view position, e.g.:

The user goes sweeping the list and when clicking on a position the app opens a new Activity and when returning to the list it goes back to the top, in case it would have to go back to the position of the last selection.

I’ve been through it and I was able to solve it but I was using Listview, I used the:

lista = (ListView) findViewById(R.id.lst);    
  Parcelable posicaoFoco;
  posicaoFoco = lista.onSaveInstanceState();    
lista.onRestoreInstanceState(posicaoFoco);

Now I’m using Recyclerview but it does not accept.

  • Edit the code better for easy reading, try to put more complete codes to better illustrate your question

2 answers

1

Recyclerview does not have these methods.

However the Layoutmanager she uses has.

You can access them using

Parcelable recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState()

and

recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState)

Note that most of the time there is no need to use them since internally they are used by Recyclerview to save their state.

0


Store the position of the clicked item and when back you can set a scroll to the position:

recyclerView.scrollToPosition(position);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.