I need to do some animations in a listview after it is flinged and is about to stop.I have a listview which is going to be of a fixed height(well dont ask me why), and whenever the scroll stops , it should have three elements visible. What i do now is detect when the list reaches SCROLL_STATE_IDLE
and if i have two elements visible at that time, i use smoothScrollToPosition
and reach a state of 3 items visible and it works fine, but what i would like to do is detect when the scroll is about to stop and stop the scroll programatically when there are three items visible. Is that even possible... Any code snippets, pseudo code, algo would help me.
Asked
Active
Viewed 284 times
5

Rasmus
- 8,248
- 12
- 48
- 72
1 Answers
2
You can set an OnScrollListener, and then store the value from absListView.getScrollY() each sample and compare it the previous sample to compute the velocity of the scroll. Once that drops below a threshold you define, you can take over scrolling.

Jens Zalzala
- 2,546
- 1
- 22
- 28
-
getScrollY on a list view will always return 0. please see this:-http://stackoverflow.com/questions/2132370/android-listview-getscrolly-does-it-work – Rasmus May 06 '14 at 22:41
-
Ah, dang it. How about using something like this to compute scroll speed : http://stackoverflow.com/questions/7895504/how-to-get-the-scroll-speed-on-a-listview – Jens Zalzala May 06 '14 at 23:08
-
Am already using it, but the issue is that the final speeds before coming to idle are not consistent. Sometimes it would reach threshold, and sometimes it wont. So for eg:- you decide to take contol of the scroll when the speed reaches 10, you would realise sometimes, the scroll stops when the speed is at 16.It can be argued that you can raise your threshold but then there are lot of other issues that crop up particularly when the user has slowly flinged the list. – Rasmus May 07 '14 at 12:29
-
I think to get this to work you may need to implement your own scrolling. You can compute the velocity using [VelocityTracker](http://developer.android.com/reference/android/view/VelocityTracker.html) and then animate the scroll offset to stop exactly where you want. If you know the exact distance you need to scroll, you could use something like this: private void computeAcceleration() { mAccel = (float) ((2*(mGoalPosition-mStartPosition)-2*mDuration*mVel)/Math.pow(mDuration,2)); } – Jens Zalzala May 07 '14 at 17:52