事件分发
Android中与Touch事件相关的方法包括:dispatchTouchEvent(MotionEvent ev)、
onInterceptTouchEvent(MotionEvent ev)、onTouchEvent(MotionEvent ev);能够响应
这些方法的控件包括:ViewGroup及其子类、Activity。方法与控件的对应关系如下表所示:
从这张表中我们可以看到 ViewGroup 及其子类对与 Touch 事件相关的三个方法均能响应,而Activity对 onInterceptTouchEvent(MotionEvent ev) 也就是事件拦截不进行响应。
ViewGroup 相当于一个盒子,既能装其他视图,又可以被其他盒子装,所以在ViewGroup 中多了一个功能 onInterceptTouchEvent,既可以往下传递,也可以自己消耗。判断是否自己消耗事件,是的话为(true)结束事件,继续传递为(false),忘下继续传递。
Touch 事件发生时Activity的dispatchTouchEvent(MotionEvent ev) 方法会以隧道方式(从根元素依次往下传递直到最内层子元素或在中间某一元素中由于某一条件停止传递)将事件传递给最外层 View 的 dispatchTouchEvent(MotionEvent ev)方法,并由该View的dispatchTouchEvent(MotionEvent ev)方法对事件进行分发。dispatchTouchEvent的事件分发逻辑如下:
如果 return true,事件会分发给当前 View 并由 dispatchTouchEvent 方法进行消费,同时事件会停止向下传递;
如果 return false,事件分发分为两种情况:
如果当前View 获取的事件直接来自 Activity,则会将事件返回给Activity 的onTouchEvent 进行消费;
如果当前View获取的事件来自外层父控件,则会将事件返回给父 View的onTouchEvent进行消费。
如果返回系统默认的super.dispatchTouchEvent(ev),事件会自动的分发给当前View的onInterceptTouchEvent方法。
可以使用以下代码做测试:
activity_main.xml中自定义布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.xykj.viewdemo.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change" />
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="START" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="STOP" />
</LinearLayout>
<com.xykj.view.MyLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.xykj.view.MyView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/m_view"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:background="#550000ff"
app:radius="50dp"
app:text="Abc123"
app:textSize="20sp" />
<!-- xmlns:app="http://schemas.android.com/apk/res/com.xykj.viewdemo" -->
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="20dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="Hello" />
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="300dp" />
</com.xykj.view.MyLayout>
</LinearLayout>
布局大致为下图所示:
分别创建MyLayout,MyView,MainActivity,并重写其中的dispatchTouchEvent,onTouchEvent方法,在MyView中多一个onInterceptTouchEvent方法,若return super.dispatchTouchEvent(ev); 则相当于return false;事件在此层不拦截;若为ture,在当层拦截并消耗;
可以打印Log信息查看