ScrollView 中的 Google Maps API v2 SupportMapFragment - 用户无法垂直滚动地图 [英] Google Maps API v2 SupportMapFragment inside ScrollView - users cannot scroll the map vertically

查看:25
本文介绍了ScrollView 中的 Google Maps API v2 SupportMapFragment - 用户无法垂直滚动地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Google 地图放入滚动视图中,以便用户可以向下滚动其他内容以查看地图.问题是这个滚动视图吃掉了所有的垂直触摸事件,所以地图的UI体验变得很奇怪.

I am trying to put a Google map inside a scroll view, so that the user can scroll down other contents to see the map. The problem is that this scroll view is eating up all the vertical touching events, so the UI experience of map becomes very weird.

我知道在谷歌地图的 V1 中,您可以覆盖 onTouch 或 setOnTouchListener 以在 MotionEvent.ACTION_DOWN 时调用 requestDisallowInterceptTouchEvent.我试图用 V2 实现类似的技巧,但无济于事.

I know that in V1 of the google map, you could override onTouch, or setOnTouchListener to call requestDisallowInterceptTouchEvent upon MotionEvent.ACTION_DOWN. I have tried to implement the similar trick with V2 to no avail.

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

So far I have tried:

  • 覆盖 SupportMapFragment,并在 onCreateView 中,为 View 设置一个触摸监听器
  • 调用 SupportMapFragment 实例的 .getView(),然后 setOnTouchListener
  • 环绕相对布局或框架布局,用透明视图或图像视图屏蔽片段

这些都没有解决滚动问题.我在这里错过了什么吗?如果有人在滚动视图中有地图的工作示例,请您分享代码示例吗?

None of these remedied the scrolling problem. Am I missing something here? If anyone has a working example of a map inside scrolling view, could you please kindly share code example?

推荐答案

在地图视图片段上应用透明图像.

Apply a transparent image over the mapview fragment.

<RelativeLayout
    android:id="@+id/map_layout"
    android:layout_width="match_parent"
    android:layout_height="300dp">

    <fragment
        android:id="@+id/mapview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="-100dp"
        android:layout_marginBottom="-100dp"
        android:name="com.google.android.gms.maps.MapFragment"/>

    <ImageView
        android:id="@+id/transparent_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@color/transparent" />

</RelativeLayout>   

然后为主 ScrollView 设置 requestDisallowInterceptTouchEvent(true).当用户触摸透明图像并移动时,为 MotionEvent.ACTION_DOWNMotionEvent.ACTION_MOVE 禁用透明图像上的触摸,以便地图片段可以接受触摸事件.

Then set requestDisallowInterceptTouchEvent(true) for the main ScrollView. When the user touches the transparent image and moves disable the touch on the transparent image for MotionEvent.ACTION_DOWN and MotionEvent.ACTION_MOVE so that map fragment can take Touch Events.

ScrollView mainScrollView = (ScrollView) findViewById(R.id.main_scrollview);
ImageView transparentImageView = (ImageView) findViewById(R.id.transparent_image);

transparentImageView.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        switch (action) {
           case MotionEvent.ACTION_DOWN:
                // Disallow ScrollView to intercept touch events.
                mainScrollView.requestDisallowInterceptTouchEvent(true);
                // Disable touch on transparent view
                return false;

           case MotionEvent.ACTION_UP:
                // Allow ScrollView to intercept touch events.
                mainScrollView.requestDisallowInterceptTouchEvent(false);
                return true;

           case MotionEvent.ACTION_MOVE:
                mainScrollView.requestDisallowInterceptTouchEvent(true);
                return false;

           default: 
                return true;
        }   
    }
});

这对我有用.希望能帮到你..

This worked for me. Hope it helps you..

这篇关于ScrollView 中的 Google Maps API v2 SupportMapFragment - 用户无法垂直滚动地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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