MapFragment in ScrollView

前端 未结 8 1780
再見小時候
再見小時候 2020-12-07 17:17

I have one of the new MapFragments in a ScrollView. Actually it\'s a SupportMapFragment, but anyway. It works, but there are two problems:

相关标签:
8条回答
  • 2020-12-07 18:17

    Done after lots of R&D:

    fragment_one.xml should looks like:

    <?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>
    

    Your Java class of FragmentOne.java looks like:

    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;
                    }
                }
            });
    
    0 讨论(0)
  • 2020-12-07 18:18

    This probably has its roots in the same place at causes the problem in this question. The solution there is to use a transparent frame, which is a little lighter weight than a transparent image.

    0 讨论(0)
提交回复
热议问题