GestureListener 的问题

Problems with GestureListener

我有一个 class 声明为:

public class TestActivity extends Activity implements OnGestureListener {

我实现的接口是import android.view.GestureDetector.OnGestureListener;

作为 class var,我有一个 GestureDetector (private GestureDetector gestureDetector;),我在 onCreate 事件上初始化它:gestureDetector = new GestureDetector(this, this);

然后,我重写接口方法:

@Override
public boolean onDown(MotionEvent e) {
    Log.w(this.getClass().toString(), e.toString());
    return true;
}

@Override
public void onShowPress(MotionEvent e) {
    Log.w(this.getClass().toString(),e.toString());
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    Log.w(this.getClass().toString(),e.toString());
    return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    Log.w(this.getClass().toString(),e1.toString());
    return true;
}

@Override
public void onLongPress(MotionEvent e) {
    Log.w(this.getClass().toString(),e.toString());
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    Log.w(this.getClass().toString(),e1.toString());
    return true;
}

我找到了一个使用 GestureListener (new GestureDetector(this)) 的已弃用构造函数的示例,但我也无法做到这一点。

我忘记了哪一步?

您可以这样使用 GestureDetector:

// Gesture detection
gestureDetector = new GestureDetector(this, new MyGestureDetector());
viewFlipperProduct.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
            v.performClick();
            return gestureDetector.onTouchEvent(event);
    }
});

private class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            return true;
    }

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