开发者

Detecting when scrollView has scrolled

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 scro开发者_JS百科lling? Or detect its scroll state like ListView?

Any other ideas on how to implement this?

Thanks.


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);
    }

}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜