NestedScrollView里面的MapView没有滚动

时间:2015-10-15 15:53:54

标签: android google-maps android-layout google-play-services android-maps-v2

像我这样用xml充气我的Mapview

<android.support.v4.widget.NestedScrollView
        android:id="@+id/sv_offers"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="56dp"
        android:visibility="gone"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <com.xys.widgets.CustomMapView
                android:id="@+id/mapView"
                android:layout_width="match_parent"
                android:layout_height="125dp"/>


        </LinearLayout>

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

我已经实现了自定义Mapview,如下所示: -

public class CustomMapView extends MapView {

    private ViewParent mViewParent;

    public CustomMapView(Context context) {
        super(context);
    }

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

    public CustomMapView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setViewParent(@Nullable final ViewParent viewParent) { //any ViewGroup
        mViewParent = viewParent;
    }

    public CustomMapView(Context context, GoogleMapOptions options) {
        super(context, options);
    }

    @Override
    public boolean onInterceptTouchEvent(final MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (null == mViewParent) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    Timber.d("Inside if of action down");
                } else {
                    mViewParent.requestDisallowInterceptTouchEvent(true);
                    Timber.d("Inside else of action down");
                }
                break;
            case MotionEvent.ACTION_UP:
                if (null == mViewParent) {
                    getParent().requestDisallowInterceptTouchEvent(false);
                    Timber.d("Inside if of action up");
                } else {
                    mViewParent.requestDisallowInterceptTouchEvent(false);
                    Timber.d("Inside else of action up");
                }
                break;
            default:
                break;
        }

        return super.onInterceptTouchEvent(event);
    }
}

在我的Activity

的onCreate()中使用了mapview
mapView = (CustomMapView) findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
        GoogleMap googleMap = mapView.getMap();

        googleMap.setMyLocationEnabled(false);
        googleMap.getUiSettings().setMyLocationButtonEnabled(false);
        googleMap.getUiSettings().setCompassEnabled(false);
        googleMap.getUiSettings().setAllGesturesEnabled(false);

        mapView.setViewParent(nestedScrollContainer);

nestedScrollContainer是我的嵌套scrollView.Tried在SO中提供了许多变通办法,但似乎无法找到滚动问题的解决方法。欢迎帮助!谢谢

2 个答案:

答案 0 :(得分:6)

在代码MapViewNestedScrollView--> LinearLayout--> com.xys.widgets.CustomMapView两级层次结构。

因此,在您的情况下,您可以访问NestedScrollView,如下所示

getParent().getParent().requestDisallowInterceptTouchEvent(true); 

更改此行 getParent().requestDisallowInterceptTouchEvent(true);到此

getParent().getParent().requestDisallowInterceptTouchEvent(true);

以下是您案例的完整代码

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.xys.widgets.CustomMapView
            android:id="@+id/mapView"
            android:layout_width="match_parent"
            android:layout_height="125dp"/>


    </LinearLayout>

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

`

public class CustomMapView extends MapView {

        private ViewParent mViewParent;

        public CustomMapView(Context context) {
            super(context);
        }

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

        public CustomMapView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

        public void setViewParent(@Nullable final ViewParent viewParent) { //any ViewGroup
            mViewParent = viewParent;
        }

        public CustomMapView(Context context, GoogleMapOptions options) {
            super(context, options);
        }

        @Override
        public boolean onInterceptTouchEvent(final MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:

                        getParent().getParent().requestDisallowInterceptTouchEvent(true);
                        Timber.d("Inside if of action down");
                    break;
                case MotionEvent.ACTION_UP:

                        getParent().getParent().requestDisallowInterceptTouchEvent(false);
                        Timber.d("Inside if of action up");

                    break;
                default:
                    break;
            }

            return super.onInterceptTouchEvent(event);
        }
    }

答案 1 :(得分:0)

可以在NestedScrollView中获得几乎正方形的MapView。

    //Fix map height
    ViewGroup.LayoutParams lp = mapView.getLayoutParams();
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    lp.height = metrics.widthPixels;
    mapView.setLayoutParams(lp);