问题
My onScroll
and areonTouchEvent
getting called, but not the onDown
method. In fact, when I log the distaceX
in onDown
I get several of them logged out, and the first is always relative to the spot where I ended the last scroll.
@Override
public boolean onTouchEvent(MotionEvent me){
this.detector.onTouchEvent(me);
return super.onTouchEvent(me);
}
@Override
public boolean onDown(MotionEvent e) {
Log.d("---onDown---",".");
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Log.d("---onScroll---", "" + distanceX);
return false;
}
Any ideas why onDown
would not be called?
EDIT:
I changed my onTouchEvent
to the following block, and only the ACTION_MOVE gets logged.
float gestureDistance = 0;
@Override
public boolean onTouchEvent(MotionEvent me){
switch (me.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
Log.d("actionDown", "gestureDistance:" + gestureDistance);
break;
case MotionEvent.ACTION_UP:
Log.d("actionUp", "gestureDistance:" + gestureDistance);
break;
case MotionEvent.ACTION_MOVE:
Log.d("actionMove", "gestureDistance:" + gestureDistance);
break;
default:
Log.d("action default", "default");
break;
}
EDIT: I think the problem is that my view is a WebView. Can I keep the webview from stealing my events?
回答1:
Why don't you do something like:
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
//Do something on action down.
case MotionEvent.ACTION_UP:
//Do something on action up.
break;
case MotionEvent.ACTION_MOVE:
//Do something when moving.
break;
}
return true;
}
Hope this helps.
回答2:
Adding this to my onCreate
fixed the problem.
webView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
detector.onTouchEvent(event);
return true;
}
});
来源:https://stackoverflow.com/questions/11730425/ongesturelistener-ondown-method-never-gets-called