The system built-in GestureDetector
has no concept of gesture lifecycle. We enclose gesture callbacks with lifecycle so that you could something in the onActionBegin and do anothing in the onActionEnd callbacks.
Add this into your dependencies block.
implementation 'com.cardinalblue.gesture:collage-gesture-detector:x.x.x'
implementation 'com.cardinalblue.gesture:collage-gesture-detector-rx:x.x.x'
If you cannot find the package, add this to your gradle repository
maven {
url 'https://dl.bintray.com/cblue/android'
}
Instantiate the detector instance
private val mGestureDetector by lazy {
GestureDetector(Looper.getMainLooper(),
ViewConfiguration.get(context),
resources.getDimension(R.dimen.touch_slop),
resources.getDimension(R.dimen.tap_slop),
resources.getDimension(R.dimen.fling_min_vec),
resources.getDimension(R.dimen.fling_max_vec))
}
Register the listener
// Gesture listener.
mGestureDetector.addLifecycleListener(...)
mGestureDetector.addTapGestureListener(...)
mGestureDetector.addDragGestureListener(...)
mGestureDetector.addPinchGestureListener(...)
Add to your view's onTouchEvent()
override fun onTouchEvent(event: MotionEvent): Boolean {
return mGestureDetector.onTouchEvent(event, null, null)
}
If you use flat view hierarchy designed by yourself, the 2nd and 3rd arguements are useful for you. For example:
override fun onTouchEvent(event: MotionEvent): Boolean {
return mGestureDetector.onTouchEvent(event,
someTouchingObject,
payloadOrContext)
}
That's it and so simple!
If you'd prefer the Rx way, try as following, pass the detector to the GestureEventObservable
:
GestureEventObservable(gestureDetector = ${YOUR_COLLAGE_GESTURE_DETECTOR})
.subscrible { event ->
// Handle event...
}
Lifecycle callbacks:
void onActionBegin();
void onActionEnd();
TAP related callbacks:
void onSingleTap();
void onDoubleTap();
void onMoreTap();
void onLongTap();
void onLongPress();
DRAG related callbacks:
boolean onDragBegin();
void onDrag();
void onDragEnd();
boolean onDragFling();
PINCH related callbacks:
boolean onPinchBegin();
void onPinch();
void onPinchEnd();
Checkout the details in the code, interface link.