GestureDetectorCompact不工作?

时间:2016-05-26 09:14:29

标签: android gesturedetector

我的代码

 public class TestActivity extends AppCompatActivity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {

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

        GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(this,this);
        gestureDetectorCompat.setOnDoubleTapListener(this);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

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

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        return false;
    }
}

我调试每个方法,但没有人调用。 我哪里错了?

2 个答案:

答案 0 :(得分:4)

不会自动调用GestureDetectorCompat。您必须覆盖onTouchEvent(MotionEvent event)中的Activity,并将事件传递给手势检测器。 E.g。

public boolean onTouchEvent(MotionEvent event) {
    return gestureDetectorCompat.onTouchEvent(event);
}

您可以找到文档here。请参考它,了解返回值的含义。

答案 1 :(得分:1)

将以下内容添加到您的代码中。它提到了here

@Override 
    public boolean onTouchEvent(MotionEvent event){ 
        this.mDetector.onTouchEvent(event);
        // Be sure to call the superclass implementation
        return super.onTouchEvent(event);
    }