1

Am having a RecyclerView listing items so my issue is,suppose when i click on position 8 item and it goes to the NextActivity , when i press the back button , I don't go back to postion 8 it goes to position 0.

How can i make my RecyclerView track an Item , so that when I try to go to the NextActivity and i press the back button it comes back to the Item which I clicked not position 0?.

Any one who knows this please help.

2 Answers2

1

In your RecyclerView Activity's onPause method store the completely visible item position in your list. Then in onResume method write statement to scroll to that particular position.

Links below will help you https://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager.html#findLastCompletelyVisibleItemPosition() https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#scrollToPosition(int)

Febi M Felix
  • 2,799
  • 1
  • 10
  • 13
  • can you please show me a simple code sample Implementation instead of the docs thamks –  Dec 01 '16 at 11:11
  • See the first answer in this link http://stackoverflow.com/questions/36568168/how-to-save-scroll-position-of-recyclerview-in-android – Febi M Felix Dec 01 '16 at 11:12
  • I have inseted this But when i click on the RecyclerView the App crashes `@Override protected void onPause() { super.onPause(); long currentVisiblePosition = 0; currentVisiblePosition = ((LinearLayoutManager)mRecyclerview.getLayoutManager()).findFirstCompletelyVisibleItemPosition(); } @Override protected void onResume() { int currentVisiblePosition = 0; super.onResume(); ((LinearLayoutManager) mRecyclerview.getLayoutManager()).scrollToPosition(currentVisiblePosition); currentVisiblePosition = 0; }` –  Dec 01 '16 at 12:27
0

This is so simple , the Problem is you are using OnStart() to load your data items ,try to use OnCreate() to load your data . Then there you will be able to get rid of that problem.

What OnStart() does whenever you come back to the Activity which has OnStart(), it loads data again , that's why the RecyclerView goes back to item 0.

Lutaaya Huzaifah Idris
  • 3,596
  • 8
  • 38
  • 77