Android detecting double tap without single tap first

梦想的初衷 提交于 2019-11-29 00:06:47

I'm confused on what you're trying to achieve. The double tap method onDoubleTap() isn't called when the single tap occurs. On the other hand, for every double tap there are two associated single tap onSingleTapUp() method calls.

If you want to distinguish them, you can use onSingleTapConfirmed() which is triggered when the gesture detector is sure the tap is single. See reference:

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

You can then use this method call in combination with a boolean flag to essentially detect any type of single/double tap.

Try out as below :

You can use the GestureDetector. See the following code:

public class MyView extends View {

GestureDetector gestureDetector;

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
            // creating new gesture detector
    gestureDetector = new GestureDetector(context, new GestureListener());
}

// skipping measure calculation and drawing

    // delegate the event to the gesture detector
@Override
public boolean onTouchEvent(MotionEvent e) {
    return gestureDetector.onTouchEvent(e);
}


private class GestureListener extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }
    // event when double tap occurs
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        Log.d("Double Tap", "Tapped Occured.");
        return true;
    }
}
}

You can override other methods of the listener to get single taps, flinges and so on.

i think i've figured it out

@Override
            public  boolean     onSingleTapConfirmed(MotionEvent e) {
                Log.d("KitView", "onSingleTapConfirmed");
                return true;
            }
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                Log.d("KitView", "onDoubleTap");
                return true;
            }

onSingleTapConfirmed is not called when i do a double tap, nice and easy

you can refer to blew : override the onTouchEvent() methed

in the first down, we shall record the time. when the second down comes in ,we compare the last down time with it ,if they are between 150ms or less ,we shall post a msg that it is a double tap.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!