Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ListView: get Overscroll distance

Is there anyway to determine the exact number of pixels that a user has overscrolled by in an Android ListView? When the user tries to scroll up past the first list item, and the overscroll/bounce animation kicks in, I need to detect the overscroll distance. Basically I'm going to do something in that space, like a fold in animation, similar to the Clear app on iPhone.

I've tried calling getScrollY() and getTop() on my ListView, and its items (using getChildAt(0).getTop), but everything returns 0. For reference my list items are all the same height.

I've also tried adding an onScrollListener to the listview and just calculating the difference between the original Y pos on ACTION_DOWN, and the Y pos on ACTION_MOVE, but it seems that the listview scrolls at a slower velocity.

Any help would be great.

like image 892
Crwydryn Avatar asked Sep 15 '13 18:09

Crwydryn


2 Answers

What about overriding this method?

protected boolean overScrollBy (int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent)

The scrollY will stay 0, but you could accumulate deltaY when isTouchEvent is true to get how far the user overscrolled.

like image 200
Bo. Avatar answered Sep 28 '22 09:09

Bo.


After much digging, and even more frustration, the answer seems to be that you can't. What's even more annoying is that I took a look at the Android source code and can see that the code I need is all nicely hidden away in the AbsListView's inner gubbings. But all is not lost. Seems that you can build your own view using the AbsListView as a guide, and do what you need inside it. It's more longwinded than I wanted, but so be it. I also found a nice tutorial on how to build your own view, in which you can do whatever you want.

Edit

I finally have a demo up and running on Github. If you're interested you can find it here. When you pull down from the top a new view will rotate in, if you let go or scroll up the view will rotate out.

like image 30
Crwydryn Avatar answered Sep 28 '22 09:09

Crwydryn