其他分享
首页 > 其他分享> > android – GestureDetectorCompact不工作?

android – GestureDetectorCompact不工作?

作者:互联网

我的代码

 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;
    }
}

我正在调试每个方法,但没有一个被调用.
哪里我错了?

解决方法:

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

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

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

标签:gesturedetector,android
来源: https://codeday.me/bug/20190829/1758130.html