Google Maps API v2 SupportMapFragment inside ScrollView - users cannot scroll the map vertically

后端 未结 9 997
深忆病人
深忆病人 2020-11-27 13:01

I am trying to put a Google map inside a scroll view, so that the user can scroll down other contents to see the map. The problem is that this scroll view is eating up all t

9条回答
  •  星月不相逢
    2020-11-27 13:46

    Use custom Google map fragment in your XML.

    Here is the complete code that worked for me. If you guys have any questions, please let me know.

    In your XML file, add the following as map fragment

    
    

    Here is the custom class for map

    package com.myapplication.maputil;
    
    import android.content.Context;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.FrameLayout;
    
    import com.google.android.gms.maps.SupportMapFragment;
        public class GoogleMapWithScrollFix extends SupportMapFragment {
            private OnTouchListener mListener;
    
            @Override
            public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstance) {
                View layout = super.onCreateView(layoutInflater, viewGroup, savedInstance);
    
                TouchableWrapper touchableWrapper = new TouchableWrapper(getActivity());
    
                touchableWrapper.setBackgroundColor(getResources().getColor(android.R.color.transparent));
    
                ((ViewGroup) layout).addView(touchableWrapper,
                        new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    
                return layout;
            }
    
            public void setListener(OnTouchListener listener) {
                mListener = listener;
            }
    
            public interface OnTouchListener {
                void onTouch();
            }
    
            public class TouchableWrapper extends FrameLayout {
    
                public TouchableWrapper(Context context) {
                    super(context);
                }
    
                @Override
                public boolean dispatchTouchEvent(MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            mListener.onTouch();
                            break;
                        case MotionEvent.ACTION_UP:
                            mListener.onTouch();
                            break;
                    }
                    return super.dispatchTouchEvent(event);
                }
            }
        }
    

    Add the following in your activity class, to initialize mapview. That's it. Tada :)

    ((GoogleMapWithScrollFix) getSupportFragmentManager()
                    .findFragmentById(R.id.map_with_scroll_fix)).getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap googleMap) {
                    ScrollView mScrollView = findViewById(R.id.scrollview); //parent scrollview in xml, give your scrollview id value
                    ((GoogleMapWithScrollFix) getSupportFragmentManager()
                            .findFragmentById(R.id.map_with_scroll_fix)).setListener(new GoogleMapWithScrollFix.OnTouchListener() {
                        @Override
                        public void onTouch() {
                            //Here is the magic happens.
                            //we disable scrolling of outside scroll view here
                            mScrollView.requestDisallowInterceptTouchEvent(true);
                        }
                    });
                }
            });
    

提交回复
热议问题