I'm trying to detect when a list view is scrolled beyond certain fixed threshold in pixels (half way through the first item). Unfortunately listview's getScrollY() seems to always return 0 instad of the scroll position. Is there any way to get the actual scroll location by pixel?
Here's the code I tried to use but as said it only returns 0.
getListView().setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
Log.d("scroll", "scroll: " + getListView().getScrollY());
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == 0)
Log.d("scroll", "scrolling stopped");
}
});
A refactor code of Malachiasz's answer. This function is used for dynamic row's height.
Call onScroll listener
getScrollY function
Maybe it will be useful for someone. Code of previous answer's will not work when the list is scrolled fast. Because in this case firstVisiblePosition may be change irregular. In my code I do not use an array to store positions
Then we can use when a listview is scrolled beyond threshold
isScrollWorking - can be used for animation
There is no notion of Y scroll for a ListView in Android simply because the total height of the content is unknown. Only the height of the displayed content is known.
However it is possible to get the current position/Y scroll of a visible item using the following hack:
You can try implementing
OnTouchListener
and override itsonTouch(View v, MotionEvent event)
and get the x and y usingevent.getX()
andevent.getY()
. I had just created a demo for swipe on ListView row that will enable a delete button for deleting a particular item from the ListView. You can check the source code from my github asListItemSwipe
.