Capture double tap from map fragment

允我心安 提交于 2019-12-11 08:27:20

问题


I am trying to capture the double tap event using a gesture detector.

I have an overlay frame that is placed on top of my map fragment, and implements ontouch and ongesture listeners. The problem is I can only seem to get one layer to capture the events. If I change onDown to return true the overlay consumes all events and the map is not usable. If I have it return false the map instead also uses the double tap event, and zooms in when I do not want it to do that.

Here is the relevant code for the overlay:

@Override
public boolean onDown(MotionEvent e) {
    return false;
}

@Override
public void onShowPress(MotionEvent e) {
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    return false;
}

@Override
public void onLongPress(MotionEvent e) {
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    return false;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    return mDetector.onTouchEvent(event);
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
    return false;
}

@Override
public boolean onDoubleTap(MotionEvent e) {
    if(mEventListener != null){
        mEventListener.onSelect();
    }
    return true;
}

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
    return false;
}


public interface OnSelectListener{
    void onSelect();
}

public void setEventListener(OnSelectListener mEventListener){
    this.mEventListener = mEventListener;
}

And here is how my layout is set up:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.google.android.gms.maps.MapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout="@layout/circle_options">
</fragment>

<com.derongan.ambiance.OverlayFrame
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frame_layout">

回答1:


The problem was solved by doing two things:

First, I needed to override onInterceptTouchEvent as well as onTouchEvent. This way my gesturedetector was fed both the initial (uncaptured) events, and the later capture ones.

Additionally, because of the way events propagate in android it is not enough to have the mapfragment be contained in a fragment underneath the Frame. Instead the frame needed to be made into the parent of the fragment so that the intercept and touch events would work properly.



来源:https://stackoverflow.com/questions/33487485/capture-double-tap-from-map-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!