安卓:检测时,滚动型会停止滚动 [英] Android: Detect when ScrollView stops scrolling

查看:113
本文介绍了安卓:检测时,滚动型会停止滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一个滚动型在Android和那里的可见部分的滚动型是大小相同一个滚动型里面的细胞。每一个细胞是相同的高度。所以,我试图做的是完全到位的滚动型已滚动之后。

I'm using a ScrollView in Android and where the visible portion of the ScrollView is the same size as one of the cells inside the Scrollview. Every "cell" is the same height. So what I am trying to do is snap into position after the ScrollView has been scrolled.

目前我正在检测,当用户触摸了滚动型,当他们已经​​开始滚动和工作它从那里,但它是相当马车。它也需要工作,当用户只需笔触它,它滚动,然后减速。

Currently I am detecting when the user has touched the ScrollView and when they've started scrolling and working it out from there, but it is quite buggy. It also needs to work when the user just flicks it and it scrolls and then decelerates.

在iPhone还有一个功能,那就是像 didDecelerate ,并在那里我可以做任何code我希望在滚动型已完成滚动。有没有与Android这样的事情?或者是有一些code,我可以看看找出这样做的更好的办法?

On iPhone there is a function that is something like didDecelerate and there I can do any code I want when the ScrollView has finished scrolling. Is there such a thing with Android? Or is there some code I could look at to figure out a better way of doing it?

我看过了Android的文档,但没有找到类似的事情。

I've looked over the Android docs and could not find anything like that.

推荐答案

最近,我不得不实施您所描述的功能。我所做的就是有一个Runnable检查出如果滚动型已经停止通过比较)由getScrollY(返回的值当的onTouchEvent首先触发由变量定义的时间后返回的值滚动 newCheck

I recently had to implement the function you described. What i did was to have a Runnable checking out if the ScrollView had stopped scrolling by comparing the value returned by getScrollY() when the onTouchEvent is first triggered with the value returned after a time defined by the variable newCheck.

请参阅下面的code(工作液):

See code below (working solution):

public class MyScrollView extends ScrollView{

private Runnable scrollerTask;
private int initialPosition;

private int newCheck = 100;
private static final String TAG = "MyScrollView";

public interface OnScrollStoppedListener{
    void onScrollStopped();
}

private OnScrollStoppedListener onScrollStoppedListener;

public MyScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);

    scrollerTask = new Runnable() {

        public void run() {

            int newPosition = getScrollY();
            if(initialPosition - newPosition == 0){//has stopped

                if(onScrollStoppedListener!=null){

                    onScrollStoppedListener.onScrollStopped();
                }
            }else{
                initialPosition = getScrollY();
                MyScrollView.this.postDelayed(scrollerTask, newCheck);
            }
        }
    };
}

public void setOnScrollStoppedListener(MyScrollView.OnScrollStoppedListener listener){
    onScrollStoppedListener = listener;
}

public void startScrollerTask(){

    initialPosition = getScrollY();
    MyScrollView.this.postDelayed(scrollerTask, newCheck);
}

}

然后,我有:

Then i have:

scroll.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_UP) {

                scroll.startScrollerTask();
            }

            return false;
        }
});
scroll.setOnScrollStoppedListener(new OnScrollStoppedListener() {

        public void onScrollStopped() {

            Log.i(TAG, "stopped");

        }
});

顺便说一句,我用我的一些想法从其他的答复,为此在我的应用程序。 希望这可以帮助。任何问题随时问。 干杯。

BTW i used i few ideas from other replies to do this in my app. Hope this helps. Any questions feel free to ask. Cheers.

这篇关于安卓:检测时,滚动型会停止滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆