onLongPress not working as expected

℡╲_俬逩灬. 提交于 2019-12-10 17:35:08

问题


I am having a surfaceview on which gesture detection is implemented using 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 user touches screen and holds there for some time. I think such action should go inside onLongPress method. However the toast inside the method is initiated when user presses long after first tap i.e. on second tap. Is this how this method is supposed to work or I am making some mistake ?

来源:https://stackoverflow.com/questions/23252675/onlongpress-not-working-as-expected

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