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

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();


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.


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);