11

我已重写ScrollViewMotionEvents 传递给 aGestureDetector以检测 ScrollView 上的投掷事件。我需要能够检测到滚动何时停止。这与事件不重合,MotionEvent.ACTION_UP因为这通常发生在投掷手势的开始,随后是onScrollChanged()对 ScrollView 的一系列调用。

所以基本上我们在这里处理的是以下事件:

  1. onFling
  2. onScrollChanged, onScrollChanged, onScrollChanged, ... , onScrollChanged

当 onScrollChanged 事件完成触发时没有回调。我正在考虑在 onFling 期间使用 a 将消息发布到事件队列Handler并等待Runnable执行以发出 fling 结束的信号,不幸的是它在第一次 onScrollChanged 调用后触发。

还有其他想法吗?

4

2 回答 2

18

我结合了这里的一些答案来构建一个类似于它的工作监听AbsListView器。它本质上就是您所描述的,并且在我的测试中效果很好。

注意:您可以简单地覆盖ScrollView.fling(int velocityY)而不是使用自己的GestureDetector.

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

public class CustomScrollView extends ScrollView {

    private static final int DELAY_MILLIS = 100;

    public interface OnFlingListener {
        public void onFlingStarted();
        public void onFlingStopped();
    }

    private OnFlingListener mFlingListener;
    private Runnable mScrollChecker;
    private int mPreviousPosition;

    public CustomScrollView(Context context) {
        this(context, null, 0);
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        mScrollChecker = new Runnable() {
            @Override
            public void run() {
                int position = getScrollY();
                if (mPreviousPosition - position == 0) {
                    mFlingListener.onFlingStopped();
                    removeCallbacks(mScrollChecker);
                } else {
                    mPreviousPosition = getScrollY();
                    postDelayed(mScrollChecker, DELAY_MILLIS);
                }
            }
        };
    }

    @Override
    public void fling(int velocityY) {
        super.fling(velocityY);

        if (mFlingListener != null) {
            mFlingListener.onFlingStarted();
            post(mScrollChecker);
        }
    }

    public OnFlingListener getOnFlingListener() {
        return mFlingListener;
    }

    public void setOnFlingListener(OnFlingListener mOnFlingListener) {
        this.mFlingListener = mOnFlingListener;
    }

}
于 2013-10-03T02:42:04.870 回答
0

谢谢@PaulBurke +1

Xamarin 解决方案

using Android.Content;
using Android.Runtime;
using Android.Util;
using Android.Widget;
using System;

public class CustomScrollView : ScrollView
{
    public event EventHandler FlingEnded;
    public event EventHandler FlingStarted;

    private Action ScrollChecker;
    private int PreviousPosition;
    private const int DELAY_MILLIS = 100;

    public CustomScrollView(Context context) : base(context) => Init();
    public CustomScrollView(Context context, IAttributeSet attrs) : base(context, attrs) => Init();
    public CustomScrollView(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr) => Init();
    public CustomScrollView(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes) => Init();
    public CustomScrollView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { }

    private void Init()
    {
        ScrollChecker = () =>
        {
            int position = ScrollY;
            if (PreviousPosition - position == 0)
            {
                FlingEnded?.Invoke(this, new EventArgs());
                RemoveCallbacks(ScrollChecker);
            }
            else
            {
                PreviousPosition = ScrollY;
                PostDelayed(ScrollChecker, DELAY_MILLIS);
            }
        };
    }

    public override void Fling(int velocityY)
    {
        base.Fling(velocityY);

        FlingStarted?.Invoke(this, new EventArgs());
        Post(ScrollChecker);
    }
}

用法:

myCustomScrollView.FlingEnded += myCustomScrollView_FlingEnded;

protected void myCustomScrollView_FlingEnded(object sender, EventArgs e) =>
{
    //Do onFlingEnded code here
};
于 2018-06-05T09:11:54.790 回答