2

i've a problem with a MapView inside NestedScroollView. My Google Map is displayed correctly but, when i try to scroll the map it doesn't work. I don't know how to solve this. Can anyone help me? Thanks!

That's my code: Dog_view.xml

...
<android.support.v4.widget.NestedScrollView
    android:fillViewport="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/nestedScroll"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <include layout="@layout/dog_view_layout"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>

</android.support.v4.widget.NestedScrollView>

dog_view_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:background="#000"
    android:layout_height="wrap_content"
    android:paddingTop="20dp">
    <com.google.android.gms.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

DogFragment

...
    MapView mMapView;
    private GoogleMap googleMap;


    public DogFragment(){

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.dog_view, container, false);
        this.dog = getArguments().getParcelable("data");
        mMapView = (MapView) rootView.findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);
        mMapView.onResume(); // needed to get the map to display immediately
        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap mMap) {
                googleMap = mMap;
                googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                googleMap.setMyLocationEnabled(true);
            }
        });
        setLayout(rootView);
        return rootView;
    }
.....
}
Federico
  • 62
  • 7

2 Answers2

12

You have to create custom MapView. Follow the code snippet provided below

public class AppMapView extends MapView {

    public AppMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP:
               System.out.println("unlocked");
               this.getParent().requestDisallowInterceptTouchEvent(false);
               break;

            case MotionEvent.ACTION_DOWN:
               System.out.println("locked");
               this.getParent().requestDisallowInterceptTouchEvent(true);
               break;
       }
       return super.dispatchTouchEvent(ev);
   }
}

In XML follow code below:

<com.hantash.nadeem.custom_views.AppMapView
   android:id="@+id/map_ride_route"
   android:layout_width="match_parent"
   android:layout_height="220dp"
   android:layout_margin="10dp"/>
Hantash Nadeem
  • 458
  • 6
  • 10
1

Here is the CustomMapView code who is looking for Kotlin.

class CustomMapView(context: Context, attributeSet: AttributeSet) : MapView(context, attributeSet) {

override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
    when (ev.action) {
        MotionEvent.ACTION_UP -> parent.requestDisallowInterceptTouchEvent(false)
        MotionEvent.ACTION_DOWN -> parent.requestDisallowInterceptTouchEvent(true)
    }
    return super.dispatchTouchEvent(ev)
 } 
}
Mücahid Kambur
  • 1,610
  • 13
  • 10