5

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.

1

2 Answers 2

3

I implemented a solution based on Jourbac comment.

Regards

import android.content.Context; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.util.Log; import android.widget.ScrollView; public class MyScrollView extends ScrollView { private static final int WHAT = 1; class MyHandler extends Handler { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(!hasMessages(WHAT)) { Log.i("TestScroll", "Last msg!. Position from " + msg.arg1 + " to " + msg.arg2); } } } private Handler mHandler = new MyHandler(); public MyScrollView(Context context) { super(context); } public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public MyScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); final Message msg = Message.obtain(); msg.arg1 = oldt; msg.arg2 = t; msg.what = WHAT; mHandler.sendMessageDelayed(msg, 500); } } 
Sign up to request clarification or add additional context in comments.

2 Comments

This does not solve what the question maker asked for - it merely sends delayed messages upon every scroll change.
The log message is printed only when the user has finished the scroll in half second. It solves the question.
1

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.

8 Comments

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.
Inspecting the source for ScrollView seems a nice idea too indeed. I found the donut version there: netmite.com/android/mydroid/donut/frameworks/base/core/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 :-)
Just override onScrollChanged().
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!
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...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.