GestureDetector:为什么这段代码不起作用?

时间:2013-12-19 15:59:18

标签: android

为什么这个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);
    }

2 个答案:

答案 0 :(得分:2)

每次收到触摸事件时,您都在初始化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);
    }

答案 1 :(得分:0)

您使用的是错误的构造函数。使用手势的完整示例:

    private GestureDetectorCompat gesture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Just the default methods
        gesture = new GestureDetectorCompat(this, new OnGestureListener() {

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void onShowPress(MotionEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                    float distanceY) {
                // TODO Auto-generated method stub
                return false;
            }

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

            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                    float velocityY) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean onDown(MotionEvent e) {
                // TODO Auto-generated method stub
                return false;
            }
        });

        gesture.setOnDoubleTapListener(new OnDoubleTapListener() {

            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean onDoubleTapEvent(MotionEvent e) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean onDoubleTap(MotionEvent e) {
                Log.i("GESTURE","DoubleTap");
                return true;
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        gesture.onTouchEvent(event);
        return true;
    }


}