Android GestureDetector 无法检测到 FrameLayout 的 onScroll 事件
我有一个扩展 FrameLayout 的视图,需要通知其上的滚动事件。 该视图有一个实现 GestureDetector 的类的实例,该类由重写的 onInterceptTouchEvent 方法调用。
private class HorizontalScrollListener implements OnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
...
return false;
}
@Override
public boolean onDown(MotionEvent e) {
...
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
...
System.out.println();
}
@Override
public void onShowPress(MotionEvent e) {}
@Override
public boolean onSingleTapUp(MotionEvent e) { return false; }
}
唯一的问题是,当我尝试滚动时,可能会调用 onDown 和 onLongPress 方法,但实际的 onScroll 方法永远不会被调用。
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
boolean result = super.onInterceptTouchEvent(event);
if (gestureDetector.onTouchEvent(event)) {
return result;
} else {
return false;
}
}
I have a view that extends FrameLayout and need to be notified of the scrolling events on it.
this view has an instance of a class that implements the GestureDetector which is invoked by the overriden onInterceptTouchEvent method.
private class HorizontalScrollListener implements OnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
...
return false;
}
@Override
public boolean onDown(MotionEvent e) {
...
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
...
System.out.println();
}
@Override
public void onShowPress(MotionEvent e) {}
@Override
public boolean onSingleTapUp(MotionEvent e) { return false; }
}
The only problem is that the onDown and onLongPress methods could get called wheen I try to scroll but the actual onScroll methods never gets invoked.
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
boolean result = super.onInterceptTouchEvent(event);
if (gestureDetector.onTouchEvent(event)) {
return result;
} else {
return false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一旦返回
true
,就不会再次为运动序列调用onInterceptTouchEvent
。之后事件会直接发送到onTouchEvent
(因为它们现在被子级拦截)。您需要在此处进行两处更改:
OnGestureListener.onDown()
应返回true
,以便检测器可以处理更复杂的手势,例如滚动onInterceptTouchEvent
应始终返回 < code>false 以保持事件流流向此方法onInterceptTouchEvent
is not called again for a motion sequence once it returnstrue
. Events are sent toonTouchEvent
directly afterwards (since they are now being intercepted from the children).You need two changes here:
OnGestureListener.onDown()
should returntrue
so the detector can process more complex gestures like scrollsonInterceptTouchEvent
should always returnfalse
to keep the stream of events flowing to this method