Android: ListView.getScrollY() - does it work?
Asked Answered
I

3

27

I am using it, but it always returns 0, even though I have scrolled till the end of the list.

Imena answered 25/1, 2010 at 12:59 Comment(2)
If what you're looking for is number of pixels that disappears at the top of the list when user scrolls down have a look at my answer hereAppend
The correct solutionVanquish
R
34

getScrollY() is actually a method on View, not ListView. It is referring to the scroll amount of the entire view, so it will almost always be 0.

If you want to know how far the ListView's contents are scrolled, you can use listView.getFirstVisiblePosition();

Regnant answered 25/1, 2010 at 16:47 Comment(2)
Note that getFirstVisiblePosition will return the item not the position, thus it is not something you can use as an alternative to getScrollY except in the circumstance that all list items are precisely the same height.Salomesalomi
Is it getScroolY position of scroolview at top of viewport or at the end?Sandasandakan
G
4

It does work, it returns the top part of the scrolled portion of the view in pixels from the top of the visible view. See the getScrollY() documentation. Basically if your list is taking up the full view then you will always get 0, because the top of the scrolled portion of the list is always at the top of the screen.

What you want to do to see if you are at the end of a list is something like this:

public void onCreate(final Bundle bundle) {
     super.onCreate(bundle);
     setContentView(R.layout.main);
     // The list defined as field elswhere
     this.view = (ListView) findViewById(R.id.searchResults);
     this.view.setOnScrollListener(new OnScrollListener() {
          private int priorFirst = -1;
          @Override
          public void onScroll(final AbsListView view, final int first, final int visible, final int total) {
               // detect if last item is visible
               if (visible < total && (first + visible == total)) {
                    // see if we have more results
                    if (first != priorFirst) {
                         priorFirst = first;
                         //Do stuff here, last item is displayed, end of list reached
                    }
               }
          }
     });
}

The reason for the priorFirst counter is that sometimes scroll events can be generated multiple times, so you only need to react to the first time the end of the list is reached.

If you are trying to do an auto-growing list, I'd suggest this tutorial.

Generalissimo answered 25/1, 2010 at 15:50 Comment(0)
O
1

You need two things to precisely define the scroll position of a listView:

To get current position:

int firstVisiblePosition = listView.getFirstVisiblePosition(); 
int topEdge=listView.getChildAt(0).getTop(); //This gives how much the top view has been scrolled.

To set the position:

listView.setSelectionFromTop(firstVisiblePosition,0);
// Note the '-' sign for scrollTo.. 
listView.scrollTo(0,-topEdge);
Operate answered 13/3, 2020 at 13:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.