1

How to Save Scroll Position of RecylerView Activity when we go to next Detail Activity by using itemclicklistener from list of items(these items are retrieved from Firebase JSON) in RecyclerView and restore position when we come back to Recylerview Activity and i am Using LinearLayoutManager

Please Help Me Thanks in Advance

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    You can save the position either globally for app or in static variable of the activity. – Parul Oct 08 '18 at 08:18
  • Please check this link 1. https://stackoverflow.com/a/33798938/5343866 2. https://stackoverflow.com/a/51742762/5343866 this may help you. – Abhinav Suman Oct 08 '18 at 08:25
  • Possible duplicate of [How to tell RecyclerView to start at specific item position](https://stackoverflow.com/questions/51499834/how-to-tell-recyclerview-to-start-at-specific-item-position) – Abhinav Suman Oct 08 '18 at 08:25

4 Answers4

1

I am not really clear with your question. I assumed you current activity was destroyed when you go to next detail, thus recreating the activity when you go back. Do not call finish when you go to next detail and just call onBackPressed to go back to your previous activity. This will maintain your recyclerView state and position.

Anyway, you can use Shared Preferences to save the position before go to next detail and get it back on onResume.

Amad Yus
  • 2,856
  • 1
  • 24
  • 32
  • It worked...Fixed it!!! Thanks Sir for your valuable answer :D Actually I'm using this if(adapter!=null) { adapter.startListening(); } I removed it now – Dasi Sridarshan Oct 09 '18 at 09:21
0

Problem statement is unclear. Still I am trying to answer.

Assuming you starting new Activity on click of list. As per the Lifecycle of activity background activity will stay in the same state with scroll. When you finish new activity back activity will on same state.

This might happening due to below reasons

  1. You reloading data on onResume method. You should load data on onCreateView.
  2. You are starting background activity from second activity.
Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
  • Fixed it!!! Thanks Sir for your valuable answer...Actually I'm using this if(adapter!=null) { adapter.startListening(); } I removed it now – Dasi Sridarshan Oct 09 '18 at 09:18
0

save state on Activity on onPause method and restore it on onResume() method See the code

 public class Activity extends AppCompatActivity
 {
    private final String STATE = "recycler_state";
    private RecyclerView mRecyclerView;
    private static Bundle mBundle;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);//set to whatever view id you use
        // don't forget to set your adapter
    }

    @Override
    protected void onPause()
    {
        super.onPause();

        // save RecyclerView state

        mBundle = new Bundle();
        Parcelable listState = mRecyclerView.getLayoutManager().onSaveInstanceState();
        mBundle.putParcelable(STATE, listState);
    }

    @Override
    protected void onResume()
    {
        super.onResume();

        // restore RecyclerView state
        if (mBundle != null) {
            Parcelable listState = mBundle.getParcelable(STATE);
            mRecyclerView.getLayoutManager().onRestoreInstanceState(listState);
        }
    }
}
Rohit Chauhan
  • 1,119
  • 1
  • 12
  • 30
  • Thanks for the Comment...But i didn't use the above solution Actually I'm using this if(adapter!=null) { adapter.startListening(); } I removed it now – Dasi Sridarshan Oct 09 '18 at 09:22
0

You can use this trick:

int firstItemPos = mLayoutManager.findFirstVisibleItemPosition();
View v = mLayoutManager.findViewByPosition(firstItemPos);
int offsetPixel = 0;
if(v != null) offsetPixel = v.getTop();

And then when you restore position:

mLayoutManager.scrollToPositionWithOffset(firstItemPos, offsetPixel)

firstItemPos and offsetPixel you can save in extra bundle of activity. mLayoutManager in my example I use ordinary LinearLayoutManager and set it to my Recycle View:

mLayoutManager = new LinearLayoutManager(getActivity())
dantes_21
  • 427
  • 3
  • 8