Search code examples
javaandroidgesturedetector

Android: how to reduce trigger duration of onSingleTapConfirmed?


I have got a view where I use :

 class GestureTap extends GestureDetector.SimpleOnGestureListener {
      
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
}
}

I think that onSingleTapConfirmed is launched after a too long duration.

Is it possible to reduce this duration ?

Thanks.


Solution

  • The duration is a final value, you can not change it. so,you may adjust your code strategy,like this:

    int MOVE_RANGE= 100;
    int TIME_LONG_PRESS = 1000;
    Runnable r = Runnable();
    
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
    
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                handler.removeCallbacks(r);
                break;
            case MotionEvent.ACTION_MOVE:
                if (Math.abs(mLastMotionX - x) > TOUCH_MAX|| Math.abs(mLastMotionY - y) > TOUCH_MAX) {
                    //Beyond the long press range
                    handler.removeCallbacks(r);
                }
                break;
            case MotionEvent.ACTION_DOWN:
                handler.removeCallbacks(r);
                mLastMotionX = x;
                mLastMotionY = y;
                // Start counting
                handler.postDelayed(r, TIME_LONG_PRESS );
                break;
        }
        return true;
    }