【问题标题】:MapFragment in ScrollViewScrollView 中的 MapFragment
【发布时间】:2012-11-24 15:26:38
【问题描述】:

我在ScrollView 中有一个新的MapFragments。实际上是SupportMapFragment,但无论如何。可以,但是有两个问题:

  1. 滚动时,它会留下黑色蒙版。黑色完全覆盖了地图所在的区域,除了 +/- 缩放按钮所在的洞。请参阅下面的屏幕截图。这是在 Android 4.0 上。

  2. 当用户与地图交互时视图不使用requestDisallowInterceptTouchEvent()以防止ScrollView拦截触摸,因此如果您尝试在地图中垂直平移,它只会滚动包含ScrollView .理论上我可以派生一个视图类 MapView 并添加该功能,但我怎样才能让 MapFragment 使用我自定义的 MapView 而不是标准的呢?

【问题讨论】:

  • “我在 ScrollView 中有一个新的 MapFragments”——如果它运行良好,我会感到惊讶。 “我怎样才能让 MapFragment 使用我定制的 MapView 而不是标准的?” ——你不能,AFAIK。您必须创建自己的片段来托管您的 MapView
  • 我也面临同样的问题,但我在片段中使用原始 MapView。同样的问题,当在 ScrollView 或 ViewPager 中滚动时,会留下黑色蒙版。
  • 在我的 Nexus S (Android 4.1.2) 上,我没有遇到这个问题。但是,我在我测试过的所有 Android 2.2-2.3 设备上都遇到了完全相同的问题。

标签: android google-maps android-mapview google-play-services


【解决方案1】:

在地图视图片段上应用透明图像似乎可以解决问题。这不是最漂亮的,但它似乎工作。这是一个显示此内容的 XML sn-p:

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

        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

        <ImageView
            android:id="@+id/imageView123"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/temp_transparent" />         
</RelativeLayout>

【讨论】:

  • 我可以确认这是可行的(尽管屏幕的某些部分仍然闪烁。我最终使用 FrameLayout 作为父级,以及一个普通的 &lt;View&gt;android:background="@android:color/transparent" 用于透明视图。跨度>
  • 谢谢老兄,我提到了这么多不工作,但你的简单而伟大
  • 使用 MapView 代替 MapFragment 有类似的解决方法吗?
【解决方案2】:

对我来说,添加透明的 ImageView 并没有帮助完全去除黑色蒙版。滚动时地图的顶部和底部仍然显示黑色蒙版。

所以解决方案,我在this answer 中找到了一个小改动。 我补充说,

android:layout_marginTop="-100dp"
android:layout_marginBottom="-100dp"

到我的地图片段,因为它是垂直滚动视图。所以我的布局现在看起来是这样的:

<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 禁用了透明图像上的触摸,以便地图片段可以接受触摸事件。

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

这对我有用。希望对你有帮助。。

【讨论】:

  • @Abdul Wahab:是的。我是这样设置的,我可以点击地图中的标记。
  • 谢谢亲爱的,很有帮助
  • 谢谢,这解决了这两个问题,这就是为什么实际上应该被接受为答案。
  • mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true) 不适用于此解决方案。
【解决方案3】:

这可能源于this question 中的问题。解决方案是使用透明框架,它比透明图像轻一些。

【讨论】:

    【解决方案4】:

    经过大量研发后完成:

    fragment_one.xml 应如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollViewParent"
        android:orientation="vertical" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="400dip" >
    
                <com.google.android.gms.maps.MapView
                    android:id="@+id/mapView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
    
                <View
                    android:id="@+id/customView"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:background="@android:color/transparent" />
            </RelativeLayout>
    
            <!-- Your other elements are here -->
    
        </LinearLayout>
    
    </ScrollView>
    

    FragmentOne.java 的 Java 类如下所示:

    private GoogleMap mMap;
    private MapView mapView;
    private UiSettings mUiSettings;
    private View customView
    

    onCreateView

    mapView = (MapView) rootView.findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    
    if (mapView != null) {
       mMap = mapView.getMap();
       mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
       mUiSettings = mMap.getUiSettings();
       mMap.setMyLocationEnabled(true);
       mUiSettings.setCompassEnabled(true); 
       mUiSettings.setMyLocationButtonEnabled(false);
    }
    
    
    scrollViewParent = (ScrollView)rootView.findViewById(R.id.scrollViewParent);
    customView = (View)rootView.findViewById(R.id.customView);
    
    customView.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.
                            scrollViewParent.requestDisallowInterceptTouchEvent(true);
                            // Disable touch on transparent view
                            return false;
    
                        case MotionEvent.ACTION_UP:
                            // Allow ScrollView to intercept touch events.
                            scrollViewParent.requestDisallowInterceptTouchEvent(false);
                            return true;
    
                        case MotionEvent.ACTION_MOVE:
                            scrollViewParent.requestDisallowInterceptTouchEvent(true);
                            return false;
    
                        default:
                            return true;
                    }
                }
            });
    

    【讨论】:

    • 我坐在嵌套的小部件结构中增加了复杂性。但我终于通过使用您的解决方案找到了出路。 (我的滚动视图只能通过使用 view.getParent())
    • 就像魅力一样,在我的案例中投票最多的答案失败时帮助了我..谢谢。
    【解决方案5】:

    我使用了这个结构,我克服了这个问题。

    我为地图片段使用了容器视图。

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:text="Another elements in scroll view1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    
    
            <com.erolsoftware.views.MyMapFragmentContainer
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="250dp">
    
                <fragment
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/eventMap"
                    tools:context="com.erolsoftware.eventapp.EventDetails"
                    android:name="com.google.android.gms.maps.SupportMapFragment"/>
    
            </com.erolsoftware.views.MyMapFragmentContainer>
    
            <TextView
                android:text="Another elements in scroll view2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    
    
    
        </LinearLayout>
    
    
    
    
    </ScrollView>
    

    容器类:

    public class MyMapFragmentContainer extends LinearLayout {
    
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev)
        {
            if (ev.getActionMasked() == MotionEvent.ACTION_DOWN)
            {
                ViewParent p = getParent();
                if (p != null)
                    p.requestDisallowInterceptTouchEvent(true);
            }
    
            return false;
        }
    
        public MyMapFragmentContainer(Context context) {
            super(context);
        }
    
        public MyMapFragmentContainer(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MyMapFragmentContainer(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
    }
    

    【讨论】:

      【解决方案6】:

      无需纠结于 customScrollViews 和透明布局文件。只需使用较轻版本的谷歌地图,您的问题就会得到解决。

        map:liteMode="true"
      

      在布局文件的地图片段内添加上述属性。问题将得到解决。

      【讨论】:

      • 我认为更快、更轻巧、更好的答案
      【解决方案7】:

      对于问题的第二部分 - 您可以从 SupportMapFragment 派生一个片段类,并在您的布局中使用它。然后,您可以覆盖 Fragment#onCreateView,并在那里实例化您的自定义 MapView。

      如果这不起作用,您始终可以创建自己的片段 - 然后您只需要自己处理调用所有生命周期方法(onCreate、onResume 等)。 This answer 有更多细节。

      【讨论】:

        【解决方案8】:
        <ScrollView
                    android:layout_width="match_parent"
            ::
            ::
            ::
              <FrameLayout
                    android:id="@+id/map_add_business_one_rl"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                 >
                                <fragment
        
                                    android:id="@+id/map"
                                    android:name="com.google.android.gms.maps.SupportMapFragment"
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_marginTop="-100dp"
                                    android:layout_marginBottom="-100dp"
        
                                    />
        
                </FrameLayout>
        
            ::
            ::
            ::
             </ScrollView>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-07-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-09
          相关资源
          最近更新 更多