Need help using getScrollY () ListView

I have the following code that writes the scroll position of Y 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 no matter where I view my ListView vertically, the value of getScrollY () always returns 0. Can someone help me?

Thanks.

+4
source share
1 answer

ListView.getScrollY() returns 0 because you are not scrolling the ListView itself, but the View inside it. Calling "getScrollY ()" on any of the "View" you scroll through returns the actual values ​​(only to some extent, since they are being processed).

To maintain your position, 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

+2
source

Source: https://habr.com/ru/post/1308329/


All Articles