MapFragment in ScrollView

前端 未结 8 1779
再見小時候
再見小時候 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 17:57

    For me adding a transparent ImageView did not help remove the black mask completely. The top and bottom parts of map still showed the black mask while scrolling.

    So the solution for it, I found in this answer with a small change. I added,

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

    to my map fragment since it was vertical scrollview. So my layout now looked this way:

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

    To solve the second part of the question I set requestDisallowInterceptTouchEvent(true) for my main ScrollView. When the user touched the transparent image and moved I disabled 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..

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

    I used this structures and I overcame the problem.

    I used a container view for maps fragment.

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

    The container class:

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

    Applying a transparent image over the mapview fragment seems to resolve the issue. It's not the prettiest, but it seems to work. Here's an XML snippet that shows this:

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

    For the second part of the question - you can derive a fragment class from SupportMapFragment, and use that in your layout instead. You can then override Fragment#onCreateView, and instantiate your custom MapView there.

    If that does not work, you can always create your own fragment - then you just need to take care of calling all the lifecycle methods yourself (onCreate, onResume, etc). This answer has some more details.

    0 讨论(0)
  • 2020-12-07 18:16

    No need to struggle with customScrollViews and transparent layout files. simply use lighter version of google map and your issue will be resolved.

      map:liteMode="true"
    

    add above property inside map fragment in your layout file. and issue will be fixed.

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