onTouchEvent 从未调用过 MapActivity
我有一个扩展 MapActivity 的活动。但是当我点击地图时,onTouchEvent 永远不会被调用。这是为什么呢?
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("temp", "onTouchEvent");
return true;
}
编辑:我现在在自定义创建的 ItemizedOverlay 中有这两种方法来捕获我的事件。 当我点击叠加层时,第一个被调用。但是当我触摸地图时,第二个(onTouchEvent)永远不会被调用。
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
this.movement = true;
Log.d("temp", "overlayItem tapped" + item.getTitle());
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
this.movement = false;
Log.d("temp", "overlayItem tapped finish");
return true;
}
I have an activity which extends MapActivity. But when I tap the map, the onTouchEvent never gets called. Why is this?
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("temp", "onTouchEvent");
return true;
}
edit: I now have these 2 methods in a custum created ItemizedOverlay to catch my events.
The first one gets called when I tap an overlay. But the second (onTouchEvent) never get's called when I touch the map.
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
this.movement = true;
Log.d("temp", "overlayItem tapped" + item.getTitle());
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
this.movement = false;
Log.d("temp", "overlayItem tapped finish");
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与
MapActivity
关联的MapView
几乎肯定会为您处理视图。如果您查看Activity#onTouchEvent()
的文档,它会显示:“当触摸屏事件未被其下的任何视图处理时调用。”http://developer.android.com/参考/android/app/Activity.html#onTouchEvent(android.view.MotionEvent)
我猜你想用这个触摸事件做什么已经由
MapView
处理了,也许找到合适的地方把它放进去那个代码?The
MapView
associated with aMapActivity
almost certainly handles the view for you. If you look at the docs forActivity#onTouchEvent()
it says: "Called when a touch screen event was not handled by any of the views under it."http://developer.android.com/reference/android/app/Activity.html#onTouchEvent(android.view.MotionEvent)
I'm guessing what you want to do with that touch event is already handled by the
MapView
, maybe find the right place to put it inside of that code?您应该覆盖 dispatchTouchEvent
You should override dispatchTouchEvent
处理程序没有被执行。我还没有找到这种行为的确切原因。原因可能是 MapActivity 不会自动将事件转发到注册的 MapView,或者它只是没有收到有关该事件的通知,因为运动事件实际上发生在 MapView 本身而不是其父级 MapActivity 上。
可以做的就是直接在 MapView 上注册事件
mapView.setOnTouchListener(new OnTouchListener() {
..或者覆盖MapActivity的dispatchTouchEvent(MotionEvent)。但是在这种情况下必须考虑的是适当地转发事件。
The handler isn't being executed. I didn't find the exact reason for this behavior yet. Probably the reason is that the MapActivity doesn't automatically forward the event to the registered MapView or it just doesn't get notified about the event, since the motion event actually occurs on the MapView itself and not its parent, the MapActivity.
What can be done instead is to either register the event directly on the MapView
mapView.setOnTouchListener(new OnTouchListener() {
..or to override the MapActivity's dispatchTouchEvent(MotionEvent). What has to be considered however is to appropriately forward the event in this case.