为什么不是Gesture Listener工作?

时间:2017-01-19 04:43:13

标签: java android

我想使用Gesture Listener,所以我编写了一个简单的代码来测试它,但它没有用。我看了一个YouTube教程并将其复制了。我没有对此进行测试,但这与我想要完成的事情非常相似。这是我第一次使用手势。我就是这样:

MyClassActivity.java

public class MyClassActivity extends Activity implments GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{

private GestureDetector gestureDetector;
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scrolling);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    this.gestureDetector = new GestureDetector(this, this);
    gestureDetector.setOnDoubleTapListener(this);

    textView = (TextView) findViewById(R.id.textView);

} //end of Oncreate

@Override
public boolean onTouchEvent(MotionEvent event) {
    this.gestureDetector.onTouchEvent(event);
    return super.onTouchEvent(event);
}

@Override
public boolean onDown(MotionEvent e) {

    textView.setText("onDown");
    return false;
}

@Override
public void onShowPress(MotionEvent e) {
    textView.setText("onShowPress");
}

@Override
public boolean onSingleTapUp(MotionEvent e) {

    textView.setText("onSingleTap");
    return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 

    textView.setText("onScroll");
    return false;
}

@Override
public void onLongPress(MotionEvent e) {
    textView.setText("onLongPress");
}

@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 :(得分:2)

在onCreate方法结束时尝试此操作。

View v = //Get your view
v.setOnTouchListener(new OnTouchListener(){
    public boolean onTouch(View v, MotionEvent e){
        return gestureDetector.onTouchEvent(e);
    }
});

答案 1 :(得分:1)

你可以试试吗

@Override
public boolean onTouchEvent(MotionEvent event) {
    this.gestureDetector.onTouchEvent(event);
    return true; // paas it true , as you are handling it.
}