views:

217

answers:

2

I'm trying to detect when a ScrollView has finished scrolling so I can slightly modify its position. Before I was using ACTION_UP to detect when the user lifted their finger, but then I realized this wouldn't allow me to use "flinging" as it would modify the scroll before it was finished.

Is there any way to detect when a ScrollView has finished scrolling? Or detect its scroll state like ListView?

Any other ideas on how to implement this?

Thanks.

A: 

I would approach this by creating a child class of ScrollView, say MyScrollView, and using that instead of ScrollView.

That way you can override the methods of ScrollView which are invoked when scrolling; and add a callback of someform in them (not forgetting to call the corresponding method in super)

I can't really tell you which of ScrollView's methods will be invoked when; I assume one of them is called each time a scoll happens, but which one... maybe simply scrollTo().

I suppose it would be a very interesting and learning experience to override them all and just log when they're called. I might want to do that myself, if I do, I'll be sure to come back with a more useful answer, assuming noone else does during that time.

Joubarc
Thanks that helps. Is there any place to find the source for ScrollView so I can see how they are handling the scrolling? I suppose I really don't need to change it, just add something onto the end that will handle my stuff, but it would still help to see theirs.
Matt
Inspecting the source for ScrollView seems a nice idea too indeed. I found the donut version there: http://www.netmite.com/android/mydroid/donut/frameworks/base/core/java/android/widget/ScrollView.java - I see the source for `scrollTo()` has a comment saying that it should be called everytime `scrollBy()` is called - so I would tend to override `scrollTo()` to start with. Good luck with the reading :-)
Joubarc
Just override onScrollChanged().
Romain Guy
Isn't onScrollChanged part of ListView, not ScrollView? I could be mistaken I've been reading about these so much I keep getting the two confused... If I am mistaken, where would I override it?Joubarc, It seems that I will need to override both scrollTo and fling. Both are used when finger scrolling. Now all I need to figure out just what exactly I need to do to get this to work! This site has been really helpful for me as a novice programmer. Thanks guys!
Matt
Following up: looks like if I place the code within the overridden Fling and ScrollTo it keeps being called while it is scrolling or flinging, not just at the end. Looks like I either need to find a way to access isFinished (located in scroller.java) or some other way to check if the scrolling is finished...
Matt
`onScrollChanged()` is a method of View, which is a superclass of both `ScrollView` and `ListView`. Anyway, if Romain says to override that one, just trust him.
Joubarc
Hmmm, I am still having no luck... Maybe it it working and I just don't know it? Right now from the overridden onScrollChanged, the only code in it is the super and the code for my listener: mOnScrollListener.onScrollFinished(this); where onScrollListener is a public interface in my new view's class. Right now the event is triggering a toast in the main activity. When scrolling/flinging this toast keeps popping up and goes away after a period of inactivity. This leads me to think that it is triggering on every scroll update, not after they are all done.
Matt
I guess so - and in a way it makes sense; how can it consider when scrolling is truly finished? What if you're just scrolling again immediately after? So if you really need to know, you'll need to implement a timer yourself. Maybe easier with a `Handler` - each time the scroll event triggers, use `PostDelayed` (with for example a delay of 1 second); the runnable you post can simply check if there are more messages in the queue. If there aren't, then no more scrolling event happened in the last second and you can do something different. I'll probably test this myself too when I get some time.
Joubarc