安卓:DoubleTap不改变方向后工作 [英] Android: DoubleTap doesn't work after changing orientation
问题描述
我重视我的的onCreate()
方法touchlistener和gesturelistener。
当我双击我的标题栏,在纵向模式下,一切工作正常。但是,当我转动我的屏幕为横向模式,不再被检测双击。然而,听众仍然被调用。当我转动我的屏幕回纵向模式时,双击再次工作。
我的听众:
//双击动作监听器添加到标题栏。
最后GestureDetector gestureDetector =新GestureDetector(这一点,新GestureDetector.SimpleOnGestureListener(){
公共布尔onDoubleTap(MotionEvent五){
myMethod的();
返回true;
}
});
TextView的电视=(的TextView)findViewById(R.id.tv_title);
tv.setOnTouchListener(新OnTouchListener(){
公共布尔onTouch(视图V,MotionEvent事件){
返回gestureDetector.onTouchEvent(事件);
}
});
已经意识到,长按总是调用,它给了我更多机会找到一个现有的问题/答案。
我发现这个<一个href=\"http://stackoverflow.com/questions/32177573/gesturedetector-ontoucheventmotionevent-e-calling-onlong$p$pss-on-all-gestures\">question.然而,答案是没有在我的处境有关,它没有问题,如果我已实施 OnTouchListener
正常。
要href=\"https://developer.android.com/training/gestures/detector.html\" rel=\"nofollow\"> 我注意到,我应该有可能返回true的android文档的 OnLongClick 返回void!)可能一直保持迫使它消耗到该结果不管。
Android的文档指定 OnTouchListener
的实现如下:
查看MyView的= findViewById(R.id.my_view);
myView.setOnTouchListener(新OnTouchListener(){
公共布尔onTouch(视图V,MotionEvent事件){
// ...响应触摸事件
返回true;
}
});
更改我的code以下的固定问题
//双击动作监听器添加到标题栏。
最后GestureDetector gestureDetector =新GestureDetector(这一点,新GestureDetector.SimpleOnGestureListener(){
公共布尔onDoubleTap(MotionEvent五){
myMethod的();
返回true;
}
});
TextView的电视=(的TextView)findViewById(R.id.tv_title);
tv.setOnTouchListener(新OnTouchListener(){
公共布尔onTouch(视图V,MotionEvent事件){
gestureDetector.onTouchEvent(事件);
返回true; //添加了此
}
});
为什么它在肖像,而不是景观但是,我不知道。
I have attached a touchlistener and gesturelistener in my onCreate()
method.
When I double tap my title bar in portrait mode everything works fine. But when I rotate my screen to landscape mode, the double tap isn't detected anymore. However the Listeners are still being called. And when I rotate my screen back to portrait mode, the double tap works again.
My Listeners:
//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);
}
});
Having realised that long click is always invoked, it gave me more scope to finding an existing question/answer.
I found this question. However, the answer was not relevant in my situation, it did question if I had implemented OnTouchListener
properly.
Going to the android docs I noticed that I should possibly return true, rather than the gesture result. (OnLongClick
returns void!) which might have kept forcing it to consume to that result regardless.
Android docs specify an implementation of OnTouchListener
as follows:
View myView = findViewById(R.id.my_view);
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// ... Respond to touch events
return true;
}
});
Changing my code to the following fixed the issue:
//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
}
});
Why it works in portrait, but not landscape however, I do not know.
这篇关于安卓:DoubleTap不改变方向后工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!