I have a listView. When I scroll and stops in a particular place.
How can I get the amount of pixels I scrolled(from top)?
I have tried using get listView.getScrollY(), but it returns 0.
I have a listView. When I scroll and stops in a particular place.
How can I get the amount of pixels I scrolled(from top)?
I have tried using get listView.getScrollY(), but it returns 0.
You cant get pixels from top of list (because then you need to layout all views from top of list - there can be a lot of items). But you can get pixels of first visible item:
int pixels = listView.getChildAt(0).getTop();
it generally will be zero or negative number - shows difference between top of listView and top of first view in listedit: I've improved in this class to avoid some moments that the track was losing due to views being too big and not properly getting a
getTop()
This new solution uses 4 tracking points:
that makes sure we always have a
isSafeToTrack
equals totrue
original answer:
First I want to thank @zsolt-safrany for his answer, that was great stuff, total kudos for him.
But then I want to present my improvement on his answer (still is pretty much his answer, just a few improvements)
Improvements:
It's a separate "gesture detector" type of class that can be added to any class that extends
AbsListView
by calling.setOnScrollListener()
, so it's a more flexible approach.It's using the change in scroll state to pre-allocate the tracked child, so it doesn't "waste" one
onScroll
pass to allocate its position.It re-calculate the tracked child on every
onScroll
pass to avoiding missing random onScroll pass to recalculate child. (this could be make more efficient by caching some heights and only re-calculate after certain amount of scroll).hope it helps
I had the same problem.
I cannot use View.getScrollY() because it always returns 0 and I cannot use OnScrollListener.onScroll(...) because it works with positions not with pixels. I cannot subclass ListView and override onScrollChanged(...) because its parameter values are always 0. Meh.
All I want to know is the amount the children (i.e. content of listview) got scrolled up or down. So I came up with a solution. I track one of the children (or you can say one of the "rows") and follow its vertical position change.
Here is the code:
Couple of notes:
Try to implement OnScrollListener: