拦截两个手指触摸ViewPager内的MapView

时间:2017-11-04 11:51:28

标签: android android-fragments android-viewpager android-mapview

所以我试图捕捉用户应该执行的两个手指手势,以便在MapView左右平移。

给定的MapView位于Fragment内,而该片段是ViewPager页面。

预期的行为是ViewPager的默认行为,因此无论用户面向哪个页面,用一根手指滑动都应该更改页面。

唯一的例外是,如果显示包含MapView的片段,则用户应该能够用两根手指(仅用两根手指)平移地图。

到目前为止我已尝试过:

简单XML:

<package.MapRelativeLayout>
    <com.google.android.gms.maps.MapView/>
</package.MapRelativeLayout>

和MapView包装器:

public class MapRelativeLayout extends RelativeLayout {

    GestureDetector mGestureDetector;

    public MapRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(context, new GestureListener());
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // return super.onInterceptTouchEvent(ev);
        boolean shouldMapPan = mGestureDetector.onTouchEvent(ev);
        Log.d("MAP", "should pan: " + shouldMapPan);
        return shouldMapPan;
    }

    private class GestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onDown(MotionEvent e) {
            return super.onDown(e);
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            // return super.onScroll(e1, e2, distanceX, distanceY);
            if (e2.getPointerCount() == 2) {
                return false;
            } else {
                return true;
            }
        }
    }

}

我将MapView打包在自定义ViewGroup内并覆盖onInterceptTouchEvent,将MotionEvent传递给自定义GestureListener,并在{{1}中收听检测到多少指针,否则返回true / false,但它不起作用。

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,我可以通过执行以下操作来解决它...

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.UiSettings;

public class EnhanceMapView extends MapView implements OnMapReadyCallback{

    private boolean isScrollable;
    private OnMapReadyCallback onMapReadyCallback;
    private UiSettings uiSettings;

    public EnhanceMapView(Context context) {
        super(context);
    }

    public EnhanceMapView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public EnhanceMapView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
    }

    public EnhanceMapView(Context context, GoogleMapOptions googleMapOptions) {
        super(context, googleMapOptions);
    }

    @Override
    public void getMapAsync(OnMapReadyCallback onMapReadyCallback) {
        this.onMapReadyCallback = onMapReadyCallback;
        super.getMapAsync(EnhanceMapView.this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        uiSettings = googleMap.getUiSettings();
        isScrollable = false;
        uiSettings.setScrollGesturesEnabled(false);
        onMapReadyCallback.onMapReady(googleMap);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(isScrollable == uiSettings.isScrollGesturesEnabled()){
            switch (ev.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_POINTER_DOWN:
                    isScrollable = true;
                    uiSettings.setScrollGesturesEnabled(true);
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    isScrollable = false;
                    uiSettings.setScrollGesturesEnabled(false);
                    break;
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return !uiSettings.isScrollGesturesEnabled() && super.onInterceptTouchEvent(ev);
    }
}

创建类后,您可以在调色板/项目的布局编辑器中进行选择