tags:

views:

193

answers:

1

Hi,

I have the following code which recording the scroll Y position in my ListView whenever my activity is paused.

protected void onSaveInstanceState (Bundle outState) {
 super.onSaveInstanceState(outState); 
 int scroll = mListView.getScrollY(); 
 System.out.println (" scrollY" + scroll);
} 

But regardless where I scroll my ListView vertically, the value of getScrollY() always return 0. Can anyone please help me?

Thank you.

A: 

ListView.getScrollY() returns 0, because you are not scrolling the ListView itself but the Views inside it. Calling 'getScrollY()' on any of the 'View's that you scroll would return actual values (only to some extent, since they get recycled).

In order to save your position, please try the following:

@Override
protected void onSaveInstanceState (Bundle outState) {
  super.onSaveInstanceState(outState); 
  int scrollPos = mListView.getSelection(); 
  outState.putInt("MyPosition", scrollPos);
} 

@Override
public void onRestoreInstanceState(Bundle inState) {
  super.onRestoreInstanceState(inState);
  int scrollPos = inState.getInt("MyPosition");
  mListView.setSelection(scrollPos);
}

Good Luck!

DanTe