OnLongPress does not work as expected

I have a top view in which gesture detection is implemented using the following code.

surfaceview.setOnTouchListener(new OnSwipeTouchListener(this ) {

    });


  public class OnSwipeTouchListener implements OnTouchListener {

    private final GestureDetector gestureDetector;

    public OnSwipeTouchListener(Context ctx) {
        gestureDetector = new GestureDetector(ctx, new GestureListener());
    }

    public boolean onTouch(final View view, final MotionEvent motionEvent) {
        return gestureDetector.onTouchEvent(motionEvent);
    }

    private final class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            //Toast.makeText(getApplicationContext(),"On Down press", Toast.LENGTH_SHORT).show();

            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;



            return result;
        }

        @Override
        public void onLongPress(MotionEvent arg0) {
            // TODO Auto-generated method stub

            Log.d("g","g");


                Toast.makeText(getApplicationContext(),"On long press", Toast.LENGTH_SHORT).show();
                Log.d("h","h");



        }

        @Override
        public boolean onScroll(MotionEvent arg0, MotionEvent arg1,
                float arg2, float arg3) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void onShowPress(MotionEvent arg0) {
            // TODO Auto-generated method stub
            //Toast.makeText(getApplicationContext(),"On Show press", Toast.LENGTH_SHORT).show();

        }

        @Override
        public boolean onSingleTapUp(MotionEvent arg0) {
            // TODO Auto-generated method stub

            return false;
        }


        @Override
        public boolean onDoubleTap(MotionEvent arg0) {

            return true;
        }
    }


}

I want to do an action after the user touches the screen and holds it for a while. I think that such an action should go inside the method onLongPress. However, the toast inside the method starts when the user clicks long after the first touch, that is, on the second tap. Is this how this method should work, or am I wrong?

+4
source share

Source: https://habr.com/ru/post/1537770/


All Articles