ScrollView中的MapFragment

Tim*_*mmm 53 android google-maps android-mapview google-play-services

我有一个新的MapFragments ScrollView.实际上它是一个SupportMapFragment,但无论如何.它有效,但有两个问题:

  1. 滚动时,会留下黑色面具.黑色完全覆盖了地图所在的区域,除了+/-缩放按钮所在的孔.见下面的截图.这是在Android 4.0上.

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

滚动问题的部分截图

小智 52

在mapview片段上应用透明图像似乎可以解决问题.这不是最漂亮的,但似乎有效.这是一个显示以下内容的XML代码段:

<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>
Run Code Online (Sandbox Code Playgroud)

  • 我可以确认这是有效的(虽然屏幕的某些部分仍然闪烁.我最终使用FrameLayout作为父级,并且使用`android:background ="@ android:color/transparent"`的简单`<View>`透明视图. (4认同)

Lak*_*ksh 35

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

所以对于它的解决方案,我在这个答案中发现了一个小小的变化.我补充说,

android:layout_marginTop="-100dp"
android:layout_marginBottom="-100dp"
Run Code Online (Sandbox Code Playgroud)

到我的地图片段,因为它是垂直scrollview.所以我的布局现在看起来像这样:

<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>   
Run Code Online (Sandbox Code Playgroud)

要解决requestDisallowInterceptTouchEvent(true)我为主ScrollView 设置的问题的第二部分.当用户触摸透明图像并移动时,我禁用了透明图像上的触摸MotionEvent.ACTION_DOWN,MotionEvent.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;
        }   
    }
});
Run Code Online (Sandbox Code Playgroud)

这对我有用.希望它可以帮助你..


iag*_*een 8

这可能源于同一个地方引起这个问题的问题.解决方案是使用透明框架,其重量比透明图像轻一些.


Hir*_*tel 5

经过大量研发后完成:

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>
Run Code Online (Sandbox Code Playgroud)

您的FragmentOne.java Java类如下所示:

private GoogleMap mMap;
private MapView mapView;
private UiSettings mUiSettings;
private View customView
Run Code Online (Sandbox Code Playgroud)

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;
                }
            }
        });
Run Code Online (Sandbox Code Playgroud)