Finding transition end on ScrollView

I overridden ScrollView to pass the MotionEvent to the GestureDetector to detect transition events on the ScrollView. I need to determine when the scroll stops. This is not the MotionEvent.ACTION_UP with the MotionEvent.ACTION_UP event, because it usually happens at the beginning of fling gestures, followed by a flurry of onScrollChanged() calls in ScrollView.

So, basically we are dealing with the following events:

  • onFling
  • onScrollChanged, onScrollChanged, onScrollChanged, ..., onScrollChanged

There is no callback when the onScrollChanged event occurs. I was thinking of posting a message to the event queue using Handler during onFling and waiting for Runnable to complete, indicating that the transition was completed, unfortunately, it fires after the first call to onScrollChanged.

Any other ideas?

+7
android android-scrollview onfling
source share
1 answer

I put together some answers here to create a working listener similar to the AbsListView method. This is essentially what you are describing and works well in my testing.

Note. You can simply override ScrollView.fling(int velocityY) and not 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; } } 
+15
source share

All Articles