GestureListener的问题

时间:2015-02-18 10:46:26

标签: android gesture-recognition gestures

我有一个声明为此的类:

public class TestActivity extends Activity implements OnGestureListener {

实现的界面是import android.view.GestureDetector.OnGestureListener;

由于类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))构造函数执行此操作的示例,但我也无法正常工作。

我忘记了什么步骤?

1 个答案:

答案 0 :(得分:0)

你可以这样使用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;
    }
}