原创

Android事件分发机制(四)——事件分发流程的验证

一、验证事件传递的流程

  1. 创建MyViewGroup

public class MyViewGroup extends FrameLayout {

    public MyViewGroup(@NonNull Context context) {
        super(context);
    }

    public MyViewGroup(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyViewGroup(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MyViewGroup(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.d(MainActivity.LOG,"MyViewGroup: dispatchTouchEvent");
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.d(MainActivity.LOG,"MyViewGroup: onInterceptTouchEvent");
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(MainActivity.LOG,"MyViewGroup: onTouchEvent");
        return  super.onTouchEvent(event);
    }
}

  1. 创建MyView
public class MyView extends View {

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

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.d(MainActivity.LOG,"MyView: dispatchTouchEvent");
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(MainActivity.LOG,"MyView: onTouchEvent");
        return super.onTouchEvent(event);
    }
}
  1. 创建测试Activity
class TestOneActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test_one)
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        Log.d(MainActivity.LOG,"TestOneActivity: dispatchTouchEvent")
        return super.dispatchTouchEvent(ev)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        Log.d(MainActivity.LOG,"TestOneActivity: onTouchEvent")
        return super.onTouchEvent(event)
    }
}

Activity的布局为:

<?xml version="1.0" encoding="utf-8"?>
<com.hunterarley.eventdistribution.testOne.MyViewGroup 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"
    android:background="@color/colorPrimary"
    tools:context=".MainActivity">

    <com.hunterarley.eventdistribution.testOne.MyView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@color/colorAccent"
        />

</com.hunterarley.eventdistribution.testOne.MyViewGroup>

事件默认传递流程:

TestOneActivity: dispatchTouchEvent -> MyViewGroup: dispatchTouchEvent -> MyViewGroup: onInterceptTouchEvent -> MyView: dispatchTouchEvent -> MyView: onTouchEvent -> MyViewGroup: onTouchEvent -> TestOneActivity: onTouchEvent

如日志所示:

事件分发默认流程.png

同一事件序列,如果子View(ViewGroup)没有处理该事件(没有消费事件),那么后续事件就不会再传递到子View中。

MainActivity: dispatchTouchEvent -> MainActivity: onTouchEvent

二、验证cancel事件

  1. 创建一个MyScrollView
public class MyScrollView extends ScrollView {
    public MyScrollView(Context context) {
        super(context);
    }

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

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean result = super.onInterceptTouchEvent(ev);
        Log.d(MainActivity.LOG,"MyScrollView: onInterceptTouchEvent " + result);
        return result;
    }
}

  1. 创建一个MyButton
public class MyButton extends AppCompatButton {
    public MyButton(Context context) {
        super(context);
    }

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

    public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(MainActivity.LOG, "MyButton: onTouchEvent " + event.getAction());
        return super.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.d(MainActivity.LOG, "MyViewGroup: dispatchTouchEvent " + event.getAction());
        return super.dispatchTouchEvent(event);
    }
}

  1. Activity的布局文件为:
<?xml version="1.0" encoding="utf-8"?>
<com.hunterarley.eventdistribution.testTwo.MyScrollView 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"
    android:orientation="vertical"
    tools:context=".testTwo.TestTwoActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <com.hunterarley.eventdistribution.testTwo.MyButton
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:text="测试取消事件"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="1500dp"/>
    </LinearLayout>

</com.hunterarley.eventdistribution.testTwo.MyScrollView>

按下MyButton,并滑动MyScrollView,日志如下所示:

手指按下时.png

滑动到取消事件.png

三、验证ViewGroup的事件拦截

  1. 创建MyViewGroup

public class MyViewGroup extends FrameLayout {

    public MyViewGroup(@NonNull Context context) {
        super(context);
    }

    public MyViewGroup(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyViewGroup(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MyViewGroup(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.d(MainActivity.LOG,"MyViewGroup: dispatchTouchEvent");
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.d(MainActivity.LOG,"MyViewGroup: onInterceptTouchEvent");
        super.onInterceptTouchEvent(ev);
        return true;
    }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d(MainActivity.LOG,"MyViewGroup: 手指按下");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d(MainActivity.LOG,"MyViewGroup: 手指移动");
                break;
            case MotionEvent.ACTION_UP:
                Log.d(MainActivity.LOG,"MyViewGroup: 手指抬起");
                break;
        }
        super.onTouchEvent(event);
        return true;
    }
}

  1. 创建MyView
public class MyView extends View {

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

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.d(MainActivity.LOG,"MyView: dispatchTouchEvent");
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(MainActivity.LOG,"MyView: onTouchEvent");
        return super.onTouchEvent(event);
    }
}

  1. Activity的布局为:
<?xml version="1.0" encoding="utf-8"?>
<com.hunterarley.eventdistribution.testThree.MyViewGroup 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"
    android:background="@color/colorPrimary"
    tools:context=".MainActivity">

    <com.hunterarley.eventdistribution.testThree.MyView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@color/colorAccent"
        />

</com.hunterarley.eventdistribution.testThree.MyViewGroup>

日志输出如下:

拦截事件日志.png

从日志中可以看出事件并没有从ViewGroup中分发到View中去。ViewGroup的onTouchEvent返回为true,标志着事件将有ViewGroup进行处理。onInterceptTouchEvent返回为true表示事件已经被ViewGroup拦截掉。

学海无涯苦作舟

个人博客:http://www.coderlearning.cn/

我的微信公众号.jpg

读书笔记
进阶知识
  • 作者:HunterArley (联系作者)
  • 发表时间:2019-12-17 09:59
  • 版权声明:本网站部分内容转载于合作站点或其他站点,但都会注明作/译者和原出处。如有不妥之处,敬请指出。
  • 公众号转载:请在文末添加作者公众号二维码