简体   繁体   English

onTouchEvent从未调用过MapActivity

[英]onTouchEvent never called MapActivity

I have an activity which extends MapActivity. 我有一个扩展MapActivity的活动。 But when I tap the map, the onTouchEvent never gets called. 但是当我点击地图时,onTouchEvent永远不会被调用。 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. 编辑:我现在在custum中创建了这两个方法,创建了ItemizedOverlay来捕获我的事件。 The first one gets called when I tap an overlay. 当我点击叠加层时,第一个被调用。 But the second (onTouchEvent) never get's called when I touch the map. 但是当我触摸地图时,第二个(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;
    }

The MapView associated with a MapActivity almost certainly handles the view for you. MapView与相关MapActivity几乎肯定会为你的看法。 If you look at the docs for Activity#onTouchEvent() it says: "Called when a touch screen event was not handled by any of the views under it." 如果您查看Activity#onTouchEvent()的文档,它会说:“当触摸屏事件未被其下的任何视图处理时调用。”

http://developer.android.com/reference/android/app/Activity.html#onTouchEvent(android.view.MotionEvent) 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? 我猜你想用触摸事件做什么已经由MapView处理了,也许找到把它放在那个代码里面的合适位置?

您应该覆盖dispatchTouchEvent

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. 可能原因是MapActivity不会自动将事件转发到已注册的MapView,或者它只是没有得到关于事件的通知,因为运动事件实际上发生在MapView本身而不是其父级MapActivity上。

What can be done instead is to either register the event directly on the MapView mapView.setOnTouchListener(new OnTouchListener() { 可以做的是直接在MapView mapView.setOnTouchListener上注册事件(new OnTouchListener(){

public boolean onTouch(View v, MotionEvent event) {
  // TODO Auto-generated method stub
   return false;
  }
});

..or to override the MapActivity's dispatchTouchEvent(MotionEvent). ..或重写MapActivity的dispatchTouchEvent(MotionEvent)。 What has to be considered however is to appropriately forward the event in this case. 然而,必须考虑的是在这种情况下适当地转发事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM