我在我的 onCreate()
方法中附加了一个 touchlistener 和 gesturelistener。
当我在纵向模式下双击我的标题栏时,一切正常。但是当我将屏幕旋转到横向模式时,不再检测到双击。但是,仍然会调用 Listener。当我将屏幕旋转回纵向模式时,双击再次起作用。
我的听众:
//Add double click gesture listener to Title Bar.
final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
public boolean onDoubleTap(MotionEvent e) {
myMethod();
return true;
}
});
TextView tv = (TextView) findViewById(R.id.tv_title);
tv.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
最佳答案
意识到总是会调用长按后,它给了我更多的空间来查找现有的问题/答案。
我找到了这个 question.但是,答案与我的情况无关,它确实质疑我是否正确实现了 OnTouchListener
。
前往 android docs我注意到我应该返回 true,而不是手势结果。 (OnLongClick
返回 void!)这可能会一直迫使它无论如何都使用该结果。
Android 文档指定了 OnTouchListener
的实现如下:
View myView = findViewById(R.id.my_view);
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// ... Respond to touch events
return true;
}
});
将我的代码更改为以下内容修复了问题:
//Add double click gesture listener to Title Bar.
final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
public boolean onDoubleTap(MotionEvent e) {
myMethod();
return true;
}
});
TextView tv = (TextView) findViewById(R.id.tv_title);
tv.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true; // ADDED THIS
}
});
为什么它适用于纵向,但不适用于横向,我不知道。
关于Android:DoubleTap 在改变方向后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35359716/