Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GestureDetector.SimpleOnGestureListener. How do I detect an ACTION_UP event?

Using this

mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return true;
        }

Only detects for single tap event i.e. quick tap and release. If i hold down and then release, onSingleTapUp is not called.

I am looking for an motion event which is ACTION_UP after holding down.

I looked at onShowPress which is called when the user performs a down action but then I was not sure how to detect a ACTION_UP while in onShowPress.

Note this is for a recycler view to click items. At the moment, I can single tap an item which works but if I hold it down and then release, it is not invoked.

like image 958
Ersen Osman Avatar asked Jun 07 '15 11:06

Ersen Osman


People also ask

What is a Gesture detector?

android.view.GestureDetector. Detects various gestures and events using the supplied MotionEvent s. The OnGestureListener callback will notify users when a particular motion event has occurred. This class should only be used with MotionEvent s reported via touch (don't use for trackball events).

How to detect gestures in Android?

Detect gestures. Android provides the GestureDetector class for detecting common gestures. Some of the gestures it supports include onDown() , onLongPress() , onFling() , and so on. You can use GestureDetector in conjunction with the onTouchEvent() method described above.

What is the difference between onSingleTapUp and onSingleTapConfirmed?

onSingleTapConfirmed. Notified when a single-tap occurs. Unlike OnGestureListener#onSingleTapUp(MotionEvent) , this will only be called after the detector is confident that the user's first tap is not followed by a second tap leading to a double-tap gesture.

What is onFling Android?

onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) Notified of a fling event when it occurs with the initial on down MotionEvent and the matching up MotionEvent . abstract void. onLongPress(MotionEvent e) Notified when a long press occurs with the initial on down MotionEvent that trigged it.


2 Answers

You can subclass your view and override onTouchEvent. That will let you observe the different actions before the gesture detector handles them.

@Override
public boolean onTouchEvent(MotionEvent e) {

    int action = e.getActionMasked();
    if (action == MotionEvent.ACTION_UP) {
        // do something here
    }

    return mGestureDetector.onTouchEvent(e);
}
like image 56
Suragch Avatar answered Dec 10 '22 16:12

Suragch


You may try the following in your onSingleTapUp method:

@Override
public boolean onSingleTapUp(MotionEvent e) {
    if(e.getAction() == MotionEvent.ACTION_UP){

    // Do what you want
    return true;
    }
    return false;
}
like image 23
blueware Avatar answered Dec 10 '22 16:12

blueware