Detect end of fling on ScrollView
Asked Answered
S

2

11

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?

Sebiferous answered 2/10, 2013 at 19:12 Comment(2)
Yes, as I mentioned in my question that approach doesn't work.Sebiferous
Have you seen/tried my answer?Coracoid
C
18

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

}
Coracoid answered 3/10, 2013 at 2:42 Comment(3)
I haven't tried this, but the constant messages on the UI thread might be a performance issue no?Sebiferous
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.FlingRunnableCoracoid
Just don't get confused with android.support.v7.widget.RecyclerView.onFlingListenerCounterpoise
C
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
};
Counterpoise answered 5/6, 2018 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.