本文介绍了Android OnLongClickListener 未在 MapView 上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在我目前正在编写的 Android 应用程序上的 MapView 上注册了一个 OnLongClickListener.但是由于某种原因 onLongClick 事件没有触发.

I just registered an OnLongClickListener on my my MapView on an Android app I'm currently writing. For some reason however the onLongClick event doesn't fire.

这是我到目前为止所写的内容:

Here's what I've written so far:

public class FriendMapActivity extends MapActivity implements OnLongClickListener {
    private static final int CENTER_MAP = Menu.FIRST;
    private MapView mapView;
    private MapController mapController;
    //...
    private boolean doCenterMap = true;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friendmapview);
        this.mapView = (MapView) findViewById(R.id.map_view);
        this.mapController = mapView.getController();

        mapView.setBuiltInZoomControls(true);
        mapView.displayZoomControls(true);
        mapView.setLongClickable(true);
        mapView.setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View v) {
                //NEVER FIRES!!
                return false;
            }
        });

        //...
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_3:
            mapController.zoomIn();
            break;
        case KeyEvent.KEYCODE_1:
            mapController.zoomOut();
            break;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int actionType = ev.getAction();
        switch (actionType) {
        case MotionEvent.ACTION_MOVE:
            doCenterMap = false;
            break;
        }

        return super.dispatchTouchEvent(ev);
    }

        ...
}

我添加的叠加层可能会导致问题吗??有什么建议吗?

May overlays which I'm adding cause the problem?? Any suggestions?

推荐答案

与此同时,我自己找到了解决方案"(或解决方法,随意称呼).我解决这个问题的方法是使用 GestureDetector 并通过实现相应的 OnGestureListener 接口将所有触摸事件转发到该对象.

In the mean time I found the "solution" (or workaround, call it as you like) by myself. The way I worked through this issue is by using a GestureDetector and forwarding all touch events to that object by implementing an according OnGestureListener interface.

如果有人感兴趣,我已经在我的博客上发布了一些代码:http://juristr.com/blog/2009/12/mapview-doesnt-fire-onlongclick-event/

I've posted some code on my blog if anyone is interested:http://juristr.com/blog/2009/12/mapview-doesnt-fire-onlongclick-event/

不要问我为什么直接在 MapView 上连接 OnLongClickListener 不起作用.如果有人有解释,请告诉我:)

Don't ask me why this didn't work by hooking up the OnLongClickListener directly on the MapView. If someone has an explanation let me know :)

更新:
我之前建议的使用 GestureDetector 的解决方案存在一些缺点.所以我更新了我网站上的博文.

UPDATE:
My previously suggested solution using a GestureDetector posed some drawbacks. So I updated the blog post on my site.

这篇关于Android OnLongClickListener 未在 MapView 上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:13