简体   繁体   English

NestedScrollView 内的 MapView 不滚动

[英]MapView inside NestedScrollView not scrolling

Inflating my Mapview in xml like this像这样在 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>

And i have implemented the Custom Mapview as follows:-我已经实现了自定义地图视图如下: -

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);
    }
}

And intilized my mapview in onCreate() of my Activity并在我的活动的 onCreate() 中加入我的地图视图

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

Where nestedScrollContainer is my nested scrollView.Tried many workarounds provided in SO but cant seem to get a workaround for scrolling issue.Help would be appreciated Thanks nestedScrollContainer是我的嵌套滚动视图。尝试了 SO 中提供的许多解决方法,但似乎无法找到解决滚动问题的解决方法。感谢您的帮助

In your code MapView inside NestedScrollView--> LinearLayout--> com.xys.widgets.CustomMapView Two level hierarchy. 在你的代码中, MapView里面有NestedScrollView--> LinearLayout--> com.xys.widgets.CustomMapView两级层次结构。

So in your case you can access NestedScrollView like below 因此,在您的情况下,您可以访问NestedScrollView,如下所示

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

Change this line getParent().requestDisallowInterceptTouchEvent(true); 更改此行getParent().requestDisallowInterceptTouchEvent(true); to this 对此

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

Below is full code for your case 以下是您的案例的完整代码

    <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);
        }
    }

It is possible to get almost square MapView inside NestedScrollView. 可以在NestedScrollView中获得几乎正方形的MapView。

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

I've found this as a solution -我发现这是一个解决方案 -

class CustomMapView(context: Context, attrs: AttributeSet?): MapView(context, attrs) {

override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
    this.performClick()
    when (ev.action) {
        MotionEvent.ACTION_DOWN ->         // Disallow ScrollView to intercept touch events.
            this.parent.requestDisallowInterceptTouchEvent(true)
        MotionEvent.ACTION_UP ->         // Allow ScrollView to intercept touch events.
            this.parent.requestDisallowInterceptTouchEvent(false)
    }

    // Handle MapView's touch events.
    super.dispatchTouchEvent(ev)
    return true
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM