截取两只手指在ViewPager内的MapView上触摸 [英] Intercept two fingers touch on MapView inside ViewPager

查看:107
本文介绍了截取两只手指在ViewPager内的MapView上触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试抓住用户应该执行的两个手指手势,以便在周围平移MapView.

So I'm trying to catch a two fingers gesture which the user should perform in order to PAN a MapView around.

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

The given MapView is located inside a Fragment, and the fragment is a ViewPager's page.

预期的行为是ViewPager的默认行为,因此,无论用户面对的是哪一页,用一根手指轻扫都可以更改页面.

The expected behavior is the ViewPager default, so swiping with one finger should change page, no matter which page the user is facing.

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

The only exception is that if the fragment containing the MapView is showing, the user should be able to pan the map with two fingers (and with two fingers only).

到目前为止我已经尝试过的:

What I've tried so far:

简单的XML:

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

和MapView包装器:

and MapView wrapper:

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,该自定义GestureListeneronScroll中侦听检测到的指针数量,并返回true/false,否则不起作用.

I wrap the MapView inside a custom ViewGroup and override the onInterceptTouchEvent, passing the MotionEvent to a custom GestureListener which listen in the onScroll for how many pointer are detected, and returns true/false otherwise, but it isn't working.

有什么办法吗?

推荐答案

我遇到了同样的问题,并且可以通过以下操作来解决此问题...

I had the same problem and I was able to solve it by doing the following...

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);
    }
}

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

After you've create the class, you'll be able to select on the layout editor in palette/project

这篇关于截取两只手指在ViewPager内的MapView上触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆