Created
August 9, 2018 18:09
-
-
Save WildOrangutan/043807ddbd68978179b7cea3b53c71e8 to your computer and use it in GitHub Desktop.
Extended GestureDetector functionality. Adds onScrollEnd method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.os.Build; | |
import android.support.annotation.RequiresApi; | |
import android.view.GestureDetector; | |
import android.view.MotionEvent; | |
/** | |
* Improved android's {@link android.view.GestureDetector} | |
* | |
* Features: | |
* - adds {@link ExtendedSimpleGestureListener#onScrollEnd(MotionEvent)}, so that end of | |
* the scroll can be detected. | |
*/ | |
public class ExtendedGestureDetector { | |
private ExtendedSimpleGestureListener mExternalListener; | |
/** Listener used for re-routing Android's | |
* {@link android.view.GestureDetector.SimpleOnGestureListener} callbacks to external | |
* {@link ExtendedSimpleGestureListener}*/ | |
private ListenerRouter mListenerRouter; | |
private ModifiedDetector mModifiedDetector; | |
public ExtendedGestureDetector(Context context, ExtendedSimpleGestureListener | |
extendedSimpleGestureListener){ | |
mExternalListener = extendedSimpleGestureListener; | |
mListenerRouter = new ListenerRouter(); | |
mModifiedDetector = new ModifiedDetector(context, mListenerRouter); | |
} | |
public boolean onTouchEvent(MotionEvent motionEvent){ | |
return mModifiedDetector.onTouchEvent(motionEvent); | |
} | |
/** | |
* Class that adds additional functionality to {@link GestureDetector}. | |
* | |
* Needed so we can detect and trigger custom callbacks from | |
* {@link ExtendedSimpleGestureListener}. | |
*/ | |
private class ModifiedDetector extends GestureDetector { | |
ModifiedDetector(Context context, OnGestureListener listener) { | |
super(context, listener); | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent ev) { | |
boolean handled = super.onTouchEvent(ev); | |
switch(ev.getAction()){ | |
case MotionEvent.ACTION_UP: | |
if(mListenerRouter.isScrolling) | |
mListenerRouter.onScrollEnd(ev); | |
break; | |
} | |
return handled; | |
} | |
} | |
/** | |
* Class for adding additional functionality to | |
* {@link android.view.GestureDetector.SimpleOnGestureListener} | |
*/ | |
public static class ExtendedSimpleGestureListener extends GestureDetector | |
.SimpleOnGestureListener { | |
/** | |
* Called when scrolling ended. | |
* @param me {@link MotionEvent#ACTION_UP} motion event | |
*/ | |
public void onScrollEnd(MotionEvent me){ } | |
} | |
/** | |
* Listener re-routing class. Passes detected gesture events from Android's | |
* {@link android.view.GestureDetector.OnGestureListener} and | |
* {@link ExtendedSimpleGestureListener} to {@link #mExternalListener}. | |
*/ | |
private class ListenerRouter extends ExtendedSimpleGestureListener { | |
private boolean isScrolling = false; | |
@Override public boolean onSingleTapUp(MotionEvent e) { | |
return mExternalListener.onSingleTapUp(e); | |
} | |
@Override public void onLongPress(MotionEvent e) { | |
mExternalListener.onLongPress(e); | |
} | |
@Override public boolean onScroll(MotionEvent e1, MotionEvent e2, | |
float distanceX, float distanceY) { | |
isScrolling = true; | |
return mExternalListener.onScroll(e1, e2, distanceX, distanceY); | |
} | |
@Override public void onScrollEnd(MotionEvent me) { | |
isScrolling = false; | |
mExternalListener.onScrollEnd(me); | |
} | |
@Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, | |
float velocityY) { | |
return mExternalListener.onFling(e1, e2, velocityX, velocityY); | |
} | |
@Override public void onShowPress(MotionEvent e) { | |
mExternalListener.onShowPress(e); | |
} | |
@Override public boolean onDown(MotionEvent e) { | |
isScrolling = false; // Just in case | |
return mExternalListener.onDown(e); | |
} | |
@Override public boolean onDoubleTap(MotionEvent e) { | |
return mExternalListener.onDoubleTap(e); | |
} | |
@Override public boolean onDoubleTapEvent(MotionEvent e) { | |
return mExternalListener.onDoubleTapEvent(e); | |
} | |
@Override public boolean onSingleTapConfirmed(MotionEvent e) { | |
return mExternalListener.onSingleTapConfirmed(e); | |
} | |
@RequiresApi(api = Build.VERSION_CODES.M) | |
@Override public boolean onContextClick(MotionEvent e) { | |
return mExternalListener.onContextClick(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment