Android OnLongClickListener does not shoot at MapView

I just registered OnLongClickListener in my MapView in the Android app that I am writing. For some reason, however, the onLongClick event does not fire.

Here is what I have 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);
    }

        ...
}

Can the overlays that I add cause a problem? Any suggestions?

+2
source share
4 answers

At the same time, I found a “solution” (or a workaround, calling it whatever you like). The way I worked on this problem is to use the GestureDetector and redirect all touch events to this object by implementing the corresponding OnGestureListener interface.

, - : http://juristr.com/blog/2009/12/mapview-doesnt-fire-onlongclick-event/

, , OnLongClickListener MapView. - , :)

UPDATE:
GestureDetector . .

+2

. , 100%, , , .

, - , , / / ..

private Handler mHandler = new Handler();

private final Runnable mTask = new Runnable() {
    @Override
    public void run() {
        // your code here
    }
};

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        // record the start time, start the timer
        mEventStartTime = ev.getEventTime();
        mHandler.postDelayed(mTask, LONG_PRESS_TIME);
    } else if (ev.getAction() == MotionEvent.ACTION_UP) {
        // record the end time, dont show if not long enough
        mEventEndTime = ev.getEventTime();
        if (mEventEndTime - mEventStartTime < LONG_PRESS_TIME) {
            mHandler.removeCallbacks(mTask);        
        }
    } else {
        // moving, panning, etc .. up to you whether you want to
        // count this as a long press - reset timing to start from now
                    mEventStartTime = ev.getEventTime();
        mHandler.removeCallbacks(mTask);
                    mHandler.postDelayed(mTask, LONG_PRESS_TIME);
    }

    return super.onTouchEvent(ev);
}
+2

, ; .

OnMapLongClickListener() OnMapLongClickListener.

, :) , , .

+2

WebView performLongClick() , Android Text Text Text , onLongClick .

0
source

Source: https://habr.com/ru/post/1785419/


All Articles