10

I've overridden ScrollView to pass MotionEvents to a GestureDetector to detect fling events on the ScrollView. I need to be able to detect when the scrolling stops. This doesn't coincide with the MotionEvent.ACTION_UP event because this usually happens at the start of a fling gesture, which is followed by a flurry of onScrollChanged() calls on the ScrollView.

So basically what we are dealing with here is the following events:

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

There's no callback for when the onScrollChanged events are done firing. I was thinking of posting a message to the event queue using a Handler during onFling and waiting for the Runnable to execute to signal the end of the fling, unfortunately it fires after the first onScrollChanged call.

Any other ideas?

Christopher Perry
  • 36,832
  • 42
  • 136
  • 182

2 Answers2

17

I've combined a few of the answers from here to construct a working listener that resembles the way AbsListView does it. It's essentially what you describe, and it works well in my testing.

Note: you can simply override ScrollView.fling(int velocityY) rather than use your own 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;
    }

}
Community
  • 1
  • 1
Paul Burke
  • 24,988
  • 9
  • 63
  • 61
  • I haven't tried this, but the constant messages on the UI thread might be a performance issue no? – Christopher Perry Oct 07 '13 at 19:31
  • 1
    Considering that this is similar to the way its done with the `Scroller` in `AbsListView`, I think you'll be fine. They actually update every 40ms. Check out `AbsListView.FlingRunnable` – Paul Burke Oct 07 '13 at 23:18
  • Just don't get confused with `android.support.v7.widget.RecyclerView.onFlingListener` – Pierre Jun 05 '18 at 08:43
0

Thank you @PaulBurke +1

Xamarin Solution

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

Usage:

myCustomScrollView.FlingEnded += myCustomScrollView_FlingEnded;

protected void myCustomScrollView_FlingEnded(object sender, EventArgs e) =>
{
    //Do onFlingEnded code here
};
Pierre
  • 6,347
  • 4
  • 48
  • 67