为什么这个GestureDetector不起作用?
我希望可以调用GestureDetector并检测到双击

@Override
    public void  onTouchEvent(MotionEvent event) {

        gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener(){
            @Override
            public boolean onDoubleTap(MotionEvent event) {
                Log.e("onDoubleTap", event.toString());
                //handle double tap
                int x = (int) event.getX();
                int y = (int) event.getY();
                return true;
            }
        });
        gestureDetector.onTouchEvent(event);
    }

最佳答案

每次收到触摸事件时,您都将初始化gestureDetector类,从而防止其保存任何状态,因此无法检测到双击。您可以改为延迟加载:

@Override
    public void  onTouchEvent(MotionEvent event) {
        if(gestureDetector == null){
            gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener(){
                @Override
                public boolean onDoubleTap(MotionEvent event) {
                    Log.e("onDoubleTap", event.toString());
                    //handle double tap
                    int x = (int) event.getX();
                    int y = (int) event.getY();
                    return true;
                }
            });
        }
        gestureDetector.onTouchEvent(event);
    }

关于android - GestureDetector:为什么此代码不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20686165/

10-12 03:51