问题
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);
}
});
回答1:
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.
来源:https://stackoverflow.com/questions/35359716/android-doubletap-doesnt-work-after-changing-orientation