Skip to content

cardinalblue/collage-gesture-detector-android

Repository files navigation

Collage Gesture Detector

  • build: CircleCI
  • detector: Download
  • detector-rx: Download

demo

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.

Gradle

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

Usage

Without RxJava

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!

With RxJava

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

Callbacks

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.

The State Diagram

state diagram state diagram2

Revisions

link

About

The gesture detector with gesture lifecycle awareness

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages