开发者

Need help in using getScrollY() of ListView

开发者 https://www.devze.com 2022-12-28 08:51 出处:网络
I have the following code which recording the scroll Y position in my ListView whenever my activity is paused.

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 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.getSelectedItemPosition(); 
  outState.putInt("MyPosition", scrollPos);
} 

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

Good Luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消