android - NestedScrollView 内的 MapView 不滚动

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

我已经按如下方式实现了自定义 map View :-

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 。尝试了 SO 中提供的许多解决方法,但似乎无法解决滚动问题。帮助将不胜感激!谢谢

最佳答案

在你的代码 MapView 里面 NestedScrollView--> 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);
        }
    }

关于android - NestedScrollView 内的 MapView 不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33153123/

相关文章:

android - Parse.com - 发布我的应用程序后我收到重复的推送通知

java - 意外的 Intent 行为 - 单击时出错

java - 如何修复 android 中 fragment 的排序问题和数据显示?

ios - 如何在 Swift 3 的 Map Kit 中更改选定的图钉图像?

android - 如何将Android软键键盘 "Go"按钮更改为 "Next"

Android:传递 "Clicked"状态?

android - 启用飞行模式而不禁用android中的wifi和蓝牙

javascript - 呈现标记 "on top of everything"(包括在 floatPane 中呈现的任何内容)

javascript - 使用 Google Maps API 将多个字符添加到标记

android - 将 recyclerview 中的图片添加到新 Activity 中