Possible Duplicate:
Maintain/Save/Restore scroll position when returning to a ListView
How can I maintain the position of my ListView in my activity when I go to another activity (by launching another intent) and then come back (press the back button)?
Thank you.
Declare global variables:
int index = 0;
ListView list;
and make a reference to your ListView
in onCreate()
:
list = (ListView) findViewById(R.id.my_list);
Next, in onResume()
, add this line at the end:
list.setSelectionFromTop(index, 0);
Lastly, in onPause
, add the following line to the end:
index = list.getFirstVisiblePosition();
Do simple....
@Override
protected void onPause()
{
index = listView.getFirstVisiblePosition();
// store index using shared preferences
}
and..
@Override
public void onResume() {
super.onResume();
// get index from shared preferences
if(listView != null){
if(listView.getCount() > index)
listView.setSelectionFromTop(index, 0);
else
listView.setSelectionFromTop(0, 0);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With