Search code examples
androidgesture

difference between click and tap in Android


The [GestureDetector.OnGestureListener][1] class has the method [onSingleTapUp(MotionEvent)][2]:

Notified When a tap Occurs with the up MotionEvent que triggered it.

This method has the same onclick's function? Can I use this method to implement the same behavior I want from the onclick method?


Solution

  • I did some tests and it seems that the answer is YES, you may use onSingleTapUp instead of onclick. The onSingleTapUp method can differentiate well a simple click of a double or long click. My test:

    public class MainFragmentScrollerController
    {
        private MainFragment mFragment;
        private GestureDetectorCompat mGestureDetector;
    
        public MainFragmentScrollerController(MainFragment fragment)
        {
            mFragment = fragment;
            mGestureDetector = new GestureDetectorCompat(mFragment.getActivity(), new GestureListener());
        }
    
        public void setGestureListener()
        {
            mFragment.myView.setOnTouchListener(new OnTouchListener()
            {
                @Override
                public boolean onTouch(View v, MotionEvent event)
                {
                    boolean gestureResult = mGestureDetector.onTouchEvent(event);
                    Log.i("GestureListener", "gestureResult = " + gestureResult);
                    return gestureResult;
                }
            });
        }
    
        public class GestureListener extends SimpleOnGestureListener
        {
            private final String TAG = "GestureListener";
            @Override
            public boolean onDown(MotionEvent event)
            {
                Log.d(TAG,"onDown: " + event.toString()); 
                return true;
            }
    
            @Override
            public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY)
            {
                Log.d(TAG, "onFling: " + event1.toString()+event2.toString());
                return true;
            }
    
            @Override
            public boolean onScroll(MotionEvent event1, MotionEvent event2, float distanceX, float distanceY)
            {
                Log.d(TAG, "onScroll: " + event1.toString()+event2.toString());
                return true;
            }
    
            @Override
            public boolean onSingleTapUp(MotionEvent event)
            {
                Log.d(TAG, "onSingleTapUp: " + event.toString());
                return true;
            }
    
            @Override
            public void onLongPress(MotionEvent event)
            {
                Log.d(TAG, "onLongPress: " + event.toString());
            }
        }
    }
    

    Result for simple click:

    09-04 14:20:58.040: D/GestureListener(17689): onDown: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=188.71875, y[0]=26.0625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34463725, downTime=34463725, deviceId=1, source=0x1002 }
    09-04 14:20:58.040: I/GestureListener(17689): gestureResult = true
    09-04 14:20:58.100: I/GestureListener(17689): gestureResult = false
    09-04 14:20:58.130: D/GestureListener(17689): onSingleTapUp: MotionEvent { action=ACTION_UP, id[0]=0, x[0]=188.71875, y[0]=26.0625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34463811, downTime=34463725, deviceId=1, source=0x1002 }
    09-04 14:20:58.130: I/GestureListener(17689): gestureResult = true
    

    Result for long click:

    09-04 14:22:12.520: D/GestureListener(17689): onDown: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=179.22656, y[0]=41.0625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34538199, downTime=34538199, deviceId=1, source=0x1002 }
    09-04 14:22:12.520: I/GestureListener(17689): gestureResult = true
    09-04 14:22:12.560: I/GestureListener(17689): gestureResult = false
    09-04 14:22:12.930: I/GestureListener(17689): gestureResult = false
    09-04 14:22:12.950: I/GestureListener(17689): gestureResult = false
    09-04 14:22:13.010: I/GestureListener(17689): gestureResult = false
    09-04 14:22:13.100: I/GestureListener(17689): gestureResult = false
    09-04 14:22:13.130: I/GestureListener(17689): gestureResult = false
    09-04 14:22:13.150: I/GestureListener(17689): gestureResult = false
    09-04 14:22:13.180: I/GestureListener(17689): gestureResult = false
    09-04 14:22:13.200: D/GestureListener(17689): onLongPress: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=179.22656, y[0]=41.0625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34538199, downTime=34538199, deviceId=1, source=0x1002 }
    09-04 14:22:13.230: I/GestureListener(17689): gestureResult = false
    

    Result for fling:

    09-04 14:24:09.650: D/GestureListener(17689): onDown: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=332.15625, y[0]=2.625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34655337, downTime=34655337, deviceId=1, source=0x1002 }
    09-04 14:24:09.650: I/GestureListener(17689): gestureResult = true
    09-04 14:24:09.690: D/GestureListener(17689): onScroll: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=332.15625, y[0]=2.625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34655337, downTime=34655337, deviceId=1, source=0x1002 }MotionEvent { action=ACTION_MOVE, id[0]=0, x[0]=331.9604, y[0]=-72.36835, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=2, eventTime=34655368, downTime=34655337, deviceId=1, source=0x1002 }
    09-04 14:24:09.690: I/GestureListener(17689): gestureResult = true
    09-04 14:24:09.710: D/GestureListener(17689): onScroll: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=332.15625, y[0]=2.625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34655337, downTime=34655337, deviceId=1, source=0x1002 }MotionEvent { action=ACTION_MOVE, id[0]=0, x[0]=337.91425, y[0]=-124.40411, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=2, eventTime=34655385, downTime=34655337, deviceId=1, source=0x1002 }
    09-04 14:24:09.710: I/GestureListener(17689): gestureResult = true
    09-04 14:24:09.730: D/GestureListener(17689): onFling: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=332.15625, y[0]=2.625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34655337, downTime=34655337, deviceId=1, source=0x1002 }MotionEvent { action=ACTION_UP, id[0]=0, x[0]=337.91425, y[0]=-124.40411, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34655414, downTime=34655337, deviceId=1, source=0x1002 }
    09-04 14:24:09.730: I/GestureListener(17689): gestureResult = true