Maintain/Save/Restore scroll position when returning to a ListView

maintain/save/restore scroll position when returning to a recyclerview
android listview: maintain your scroll position when you refresh
android-nestedscrollview save scroll position
listview position is getting changed when scrolling on android
refreshing data in recyclerview and keeping its scroll position
flutter listview keep scroll position
listview get scroll position
flutter listview scroll position

I have a long ListView that the user can scroll around before returning to the previous screen. When the user opens this ListView again, I want the list to be scrolled to the same point that it was previously. Any ideas on how to achieve this?

Try this:

// save index and top position
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : (v.getTop() - mList.getPaddingTop());

// ...

// restore index and position
mList.setSelectionFromTop(index, top);

Explanation: ListView.getFirstVisiblePosition() returns the top visible list item. But this item may be partially scrolled out of view, and if you want to restore the exact scroll position of the list you need to get this offset. So ListView.getChildAt(0) returns the View for the top list item, and then View.getTop() - mList.getPaddingTop() returns its relative offset from the top of the ListView. Then, to restore the ListView's scroll position, we call ListView.setSelectionFromTop() with the index of the item we want and an offset to position its top edge from the top of the ListView.

How to Save and Restore the Scroll Position and , they lose the scroll progress I made on a ListView after returning from a different Some apps at least restore the ListView to the general area, but not Save the ListView state (= includes scroll position) as a Parceble Additionally, if you need to set a new adapter, you can hold on to the scroll position: Maintain/Save/Restore scroll position when returning to a ListView When a user taps on an item in the list, I use SharedPreferences to save the "index" and "top" before the app transitions to an activity.

Parcelable state;

@Override
public void onPause() {    
    // Save ListView state @ onPause
    Log.d(TAG, "saving listview state @ onPause");
    state = listView.onSaveInstanceState();
    super.onPause();
}
...

@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    // Set new items
    listView.setAdapter(adapter);
    ...
    // Restore previous state (including selected item index and scroll position)
    if(state != null) {
        Log.d(TAG, "trying to restore listview state..");
        listView.onRestoreInstanceState(state);
    }
}

How can I save the scroll position for my ListView that uses a , Maintain/Save/Restore scroll position when returning to a ListView. When a user taps on an item in the list, I use SharedPreferences to save the  The problem is that you are creating a new adapter every time you reload the data. That's not how you should use ListView and its adapter. Instead of setting a new adapter (which causes ListView to reset its state), simply update the content of the adapter already set on the ListView. And the selection/scroll position will be saved for you.

I adopted the solution suggested by @(Kirk Woll), and it works for me. I have also seen in the Android source code for the "Contacts" app, that they use a similar technique. I would like to add some more details: On top on my ListActivity-derived class:

private static final String LIST_STATE = "listState";
private Parcelable mListState = null;

Then, some method overrides:

@Override
protected void onRestoreInstanceState(Bundle state) {
    super.onRestoreInstanceState(state);
    mListState = state.getParcelable(LIST_STATE);
}

@Override
protected void onResume() {
    super.onResume();
    loadData();
    if (mListState != null)
        getListView().onRestoreInstanceState(mListState);
    mListState = null;
}

@Override
protected void onSaveInstanceState(Bundle state) {
    super.onSaveInstanceState(state);
    mListState = getListView().onSaveInstanceState();
    state.putParcelable(LIST_STATE, mListState);
}

Of course "loadData" is my function to retrieve data from the DB and put it onto the list.

On my Froyo device, this works both when you change the phone orientation, and when you edit an item and go back to the list.

How to retain Android ListView scroll position when returning to an , Maintain/Save/Restore scroll position when returning to a ListView ListView.​getFirstVisiblePosition() returns the top visible list item. But this  RecyclerView state is saved and restored on rotation of phone. RecyclerView state is saved and restored on returning to activity with RecyclerView (which wasn't destroyed whilst the other activity was showing - which means that onRestoreInstanceState() isn't called !!)

A very simple way:

/** Save the position **/
int currentPosition = listView.getFirstVisiblePosition();

//Here u should save the currentPosition anywhere

/** Restore the previus saved position **/
listView.setSelection(savedPosition);

The method setSelection will reset the list to the supplied item. If not in touch mode the item will actually be selected if in touch mode the item will only be positioned on screen.

A more complicated approach:

listView.setOnScrollListener(this);

//Implements the interface:
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
    mCurrentX = view.getScrollX();
    mCurrentY = view.getScrollY();
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {

}

//Save anywere the x and the y

/** Restore: **/
listView.scrollTo(savedX, savedY);

How to Save and Restore the Scroll Position and State of a , Possible Duplicate: Maintain/Save/Restore scroll position when returning to a ListView The user views a listview of items, they move to an activity that shows  Maintain/Save/Restore scroll position when returning to a ListView ; Example: Communication between Activity and Service using Messaging ; ViewPager and fragments — what's the right way to store fragment's state? How to add dividers and spaces between items in RecyclerView?

I found something interesting about this.

I tried setSelection and scrolltoXY but it did not work at all, the list remained in the same position, after some trial and error I got the following code that does work

final ListView list = (ListView) findViewById(R.id.list);
list.post(new Runnable() {            
    @Override
    public void run() {
        list.setSelection(0);
    }
});

If instead of posting the Runnable you try runOnUiThread it does not work either (at least on some devices)

This is a very strange workaround for something that should be straight forward.

android MaintainSaveRestore scroll position when returning to a , How to Save and Restore the Scroll Position and State of a Android ListView Views save their state automatically, and if you're having issues with your scroll position not to have the spinner (since adapter is null) and launches the loader to return the cached data. Google In-App Update: Keep your users up-to-date. Maintain Scroll Bar position of a div within a gridview after a PostBack. I used the following piece of code in the web.config in order to maintain the scrollbar position after a server postback: All is working fine, but now i have a gridview encapsuled within a div with a scrollbar in the div (internal scrollbar).

Android scroll position restoring done right - Dmytro Saviuk, android MaintainSaveRestore scroll position when returning to a ListView? Parcelable state; @Override public void onPause() { // Save ListView state You can maintain the scroll state after a reload if you save the state before you reload​  Maintain/Save/Restore scroll position when returning to a ListView. I have a long ListView that the user can scroll around before returning to the previous screen. When the user opens this ListView again, I want the list to be scrolled to the same point that it was previously.

How do I save and restore the scroll position of a ListView between , Taking current scroll position and saving it in Activity's onSaveInstanceState RecyclerView/ListView/ScrollView/NestedScrollView sources, you'll see that each of It means that you need to keep it in something, that lives longer than Activity  In that post I looked at using the NavigationCacheMode of the page to persist the scroll position of your ListView. I also briefly mentioned using the ScrollIntoView method of the ListView. In this post we’ll look at a little known helper class that allows you to keep your scroll position without using all the memory that NavigationCacheMode

Maintain/Save/Restore scroll position when returning to a ListView, So I guess it should be saved on disk using something like How do I save and restore the scroll position of a ListView between app restarts? index) { return ListView( shrinkWrap: true, physics: ClampingScrollPhysics(),  Maintain ListView Position in Android Application Published by Michael Carrano on January 23, 2014. A small and simple feature you can immediately add to your Android application is the functionality of maintaining the ListView position. When your user switches to a different Activity/Screen and then back to the ListView, they will not have to scroll through the ListView items again to get back to where they were.

Comments
  • I think the solutions mentioned by Eugene Mymrin / Giorgio Barchiesi are better than the accepted answer
  • The solution given by 'Giorgio Barchiesi' worked for me. The other solutions did not work for me.
  • Not really understanding how this works. I'm using listView.getFirstVisiblePosition() only and understand that, but not sure what's going on here. Any chance of a brief explanation? :)
  • So ListView.getFirstVisiblePosition() returns the top visible list item. But this item may be partially scrolled out of view, and if you want to restore the exact scroll position of the list you need to get this offset. So ListView.getChildAt(0) returns the View for the top list item, and then View.getTop() returns its relative offset from the top of the ListView. Then, to restore the ListView's scroll position, we call ListView.setSelectionFromTop() with the index of the item we want and an offset to position its top edge from the top of the ListView. All clear?
  • This can be made even simpler be using one line to save: int index = mList.getFirstVisiblePosition(); and only one line to restore: mList.setSelectionFromTop(index, 0);. Great answer though (+1)! I have been looking for an elegant solution to this problem.
  • @Phil your simplistic solution doesn't work to restore the exact scrolling position.
  • as @nbarraille said, the code should read more like "v.getTop() - mList.getPaddingTop()." Otherwise you'll spend an hour like I did trying to figure out why it always restores just a hair off...
  • I think this is the best answer as this method restores the exact scroll position and not only the first visible item.
  • Great answer but won't work when dynamically loading content and you want to save state in the onPause() method.
  • great solution. Just one Problem. If the items are big and you scroll, but the first item is still visible the list jumps back to top. if you scroll to the second item or anywhere else everything works
  • @passsy Did you ever find a solution the list jumping back to the top?
  • As aaronvargas said, this couldn't work when ListView.getFirstVisiblePosition() is 0.