Android: Detect when ScrollView has finished scrolling and bouncing back?

nebs picture nebs · Sep 17, 2010 · Viewed 10.1k times · Source

I have a ScrollView which has two hidden images, one at the top and one at the bottom. In between there is a bunch of visible content.

What I need to do is make these images hidden by default but when you scroll all the way up or all the way down you could see them as you're scrolling. But then as soon as you stop scrolling it should bounce back to the visible area so that the hidden images aren't showing.

Basically I'm trying to imitate the bounce scrolling feature of the iphone UIScrollView.

I have my ScrollView all setup and I do a scroll at the beginning so as to hide the top hidden image. Now all I need to do is detect when a scrolling has ended, figure out the Y position, and check whether a hidden image is shown. If it is, I would just programmatically scroll the view back so that the hidden image is hidden.

I hope all that made sense.

So anyways, I know how to programmatically scroll a ScrollView. Now what I need is some sort of callback to tell me when a ScrollView ended scrolling and also a way to get the ScrollView's current 'Y' position. Are there any such methods I could use?

I looked through the ScrollView docs but nothing jumped out at me. I'm still not very familiar with the Android naming schemes so maybe I missed something obvious somewhere.

Anyways, any help would be appreciated here. Cheers.

Answer

Joseph Earl picture Joseph Earl · Apr 5, 2011

You can use an OnTouchListener to detect when the user presses/releases the list.

You can also use the onScrollStateChanged method of the OnScrollListener class (most likely in conjunction with a touch listener) to detect changes in the SCROLL_STATE - when the list has stopped scrolling the state will change from a state that is not SCROLL_STATE_IDLE to SCROLL_STATE_IDLE.

Alternatively if you are using 2.3 or above you can use an OverScroller to get the desired effect (see Modifying Android OverScroll for how to change the over scroll effect to an iPhone like one).