54.android 简单的解决ScrollView与地图的滑动冲突

//ScrollView嵌套地图,用我的这种方式解决滑动冲突,甭管你是什么地图,分分钟搞定。

//ok 先上效果图,不是动图。

//第一步 写个MapContainer类继承RelativeLayout

public class MapContainer extends RelativeLayout {
    private ScrollView scrollView;
    public MapContainer(Context context) {
        super(context);
    }

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

    public void setScrollView(ScrollView scrollView) {
        this.scrollView = scrollView;
    }

    //当点击到地图的时候,让ScrollView不拦截事件,把事件传递到子View
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            scrollView.requestDisallowInterceptTouchEvent(false);
        } else {
            scrollView.requestDisallowInterceptTouchEvent(true);
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }
}

//第二步 布局中使用(最外层ScrollView):

<com.example.hasee.a828huadong.MapContainer
    android:id="@+id/map_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <com.amap.api.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="300dp"/>
</com.example.hasee.a828huadong.MapContainer>

//完整版我的布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/mScroll"
    android:orientation="vertical"
    tools:context="com.example.hasee.a828huadong.MainActivity">

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

    <com.example.hasee.a828huadong.MapContainer
        android:id="@+id/map_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <com.amap.api.maps.MapView
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="300dp"/>
    </com.example.hasee.a828huadong.MapContainer>


        <TextView
            android:gravity="center"
            android:text="解决滑动冲突"
            android:layout_marginTop="20dp"
            android:background="@color/colorAccent"
            android:layout_width="match_parent"
            android:layout_height="50dp"></TextView>
        <TextView
            android:gravity="center"
            android:text="解决滑动冲突"
            android:layout_marginTop="20dp"
            android:background="@color/colorAccent"
            android:layout_width="match_parent"
            android:layout_height="50dp"></TextView>
        <TextView
            android:gravity="center"
            android:text="解决滑动冲突"
            android:layout_marginTop="20dp"
            android:background="@color/colorAccent"
            android:layout_width="match_parent"
            android:layout_height="50dp"></TextView>
        <TextView
            android:gravity="center"
            android:text="解决滑动冲突"
            android:layout_marginTop="20dp"
            android:background="@color/colorAccent"
            android:layout_width="match_parent"
            android:layout_height="50dp"></TextView>
        <TextView
            android:gravity="center"
            android:text="解决滑动冲突"
            android:layout_marginTop="20dp"
            android:background="@color/colorAccent"
            android:layout_width="match_parent"
            android:layout_height="50dp"></TextView>
        <TextView
            android:gravity="center"
            android:text="解决滑动冲突"
            android:layout_marginTop="20dp"
            android:background="@color/colorAccent"
            android:layout_width="match_parent"
            android:layout_height="50dp"></TextView>
        <TextView
            android:gravity="center"
            android:text="解决滑动冲突"
            android:layout_marginTop="20dp"
            android:background="@color/colorAccent"
            android:layout_width="match_parent"
            android:layout_height="50dp"></TextView>
        <TextView
            android:gravity="center"
            android:text="解决滑动冲突"
            android:layout_marginTop="20dp"
            android:background="@color/colorAccent"
            android:layout_width="match_parent"
            android:layout_height="50dp"></TextView>

    </LinearLayout>




</ScrollView>

//好了第三步 Activity里使用:

mScroll = findViewById(R.id.mScroll);
map_container = findViewById(R.id.map_container);
map_container.setScrollView(mScroll);

//-----------------------------------------------------------完----------------------------------------------------------------------------

猜你喜欢

转载自blog.csdn.net/weixin_42061754/article/details/82151516