想要弄明白android的touch事件分发响应机制需要先充分理解一下几个知识点:
View和ViewGroup
touch事件的构成
ViewGroup如何对事件分发和拦截
View和ViewGroup如何对事件进行响应
View和ViewGroup
先看一下官方文档对view类的部分介绍
View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). The ViewGroup subclass is the base class for layouts, which are invisible containers that hold other Views (or other ViewGroups) and define their layout properties.
从这里我们就可以看出View是android中所有界面布局组件的基类,而ViewGroup 是View的一个子类,ViewGroup是一个容器类,可以往里面添加子View。
再看一下android的界面是如何构成的
这里有一篇官方文档UI Overview
下面是该文章中的一部分内容:
All user interface elements in an Android app are built using View
and ViewGroup objects. A View is an object that draws something on the screen that the user can interact with. A ViewGroup is an object that holds other View (and ViewGroup) objects in order to define the layout of the interface.
Android provides a collection of both View and ViewGroup subclasses that offer you common input controls (such as buttons and text fields) and various layout models (such as a linear or relative layout).
User Interface Layout
上面的内容大概告诉大家android的界面的布局是一个树状的层级结构。
由顶级的ViewGroup其中包含一个或者多个View和ViewGroup来实现的。
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
android:id="@+id/text"
android:layout_width="wrap_conten