Android事件冲突

Android事件冲突处理方案无非两种:内部拦截法和外部拦截法
外部拦截法伪代码:

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        //外部拦截法
        boolean intercept = false;
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                intercept = false;
                break;
            case MotionEvent.ACTION_MOVE:
                if (父容器需要事件) {
                    intercept = true;
                } else {
                    intercept = false;
                }
                break;
            case MotionEvent.ACTION_UP:
                intercept = false;
                break;
            default:
                break;
        }
        return intercept;
    }

内部拦截法伪代码:
主要通过requestDisallowInterceptTouchEvent(),该方法可以让父 view的事件拦截生效,那么如果父View的拦截失效,则子View即可获得事件

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                  getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_MOVE:
                if (父容器需要事件){
                  getParent().requestDisallowInterceptTouchEvent(false);
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
            default:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

由于父容器的down事件不受requestDisallowInterceptTouchEvent()影响(因为在down事件拦截前会执行重置操作),而down事件如果被父容器拦截,则后续事件都由父容器处理,因此需要在父容器的事件拦截中实现如下代码才能完成内部拦截法的全过程

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        //内部拦截法
        if (ev.getAction() == MotionEvent.ACTION_DOWN){
           return false;
        }
        return true;
    }

接下来我们通过一个例子来演示事件冲突的处理,该例子中就是父容器ScrollView中嵌套了ListView,同方向的滑动冲突,布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="match_parent"
    tools:context=".MainActivity">

    <ScrollView
        android:id="@+id/sv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="@color/purple_200"
                android:text="textView1"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="@color/purple_700"
                android:text="textView2"
                android:textColor="@color/white" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="@color/purple_200"
                android:text="textView1"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="@color/purple_700"
                android:text="textView2"
                android:textColor="@color/white" />

            <ListView
                android:id="@+id/lv"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="@color/purple_200"
                android:text="textView1"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="@color/purple_700"
                android:text="textView2"
                android:textColor="@color/white" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="@color/purple_200"
                android:text="textView1"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="@color/purple_700"
                android:text="textView2"
                android:textColor="@color/white" />
        </LinearLayout>
    </ScrollView>

</android.support.constraint.ConstraintLayout>

在不处理滑动冲突时,效果如下,ScrollView可以滑动,ListView不能滑动


未命名-副本.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容