Need help in using getScrollY() of ListView
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.
ListView.getScrollY()
returns 0, because you are not scrolling the ListView
itself but the View
s 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.getSelectedItemPosition();
outState.putInt("MyPosition", scrollPos);
}
@Override
public void onRestoreInstanceState(Bundle inState) {
super.onRestoreInstanceState(inState);
int scrollPos = inState.getInt("MyPosition");
mListView.setSelection(scrollPos);
}
Good Luck!
精彩评论