老实说,这两个小东东实在是太麻烦了,很不好懂,我自己那api文档都头晕,在网上找到很多资料,才知道是怎么回事,这里总结一下,记住这个原则就会很清楚了:
1、onInterceptTouchEvent()是用于处理事件(类似于预处理,当然也可以不处理)并改变事件的传递方向,也就是决定是否允许Touch事件继续向下(子控件)传递,一但返回True(代表事件在当前的viewGroup中会被处理),则向下传递之路被截断(所有子控件将没有机会参与Touch事件),同时把事件传递给当前的控件的onTouchEvent()处理;返回false,则把事件交给子控件的onInterceptTouchEvent()
2、onTouchEvent()用于处理事件,返回值决定当前控件是否消费(consume)了这个事件,也就是说在当前控件在处理完Touch事件后,是否还允许Touch事件继续向上(父控件)传递,一但返回True,则父控件不用操心自己来处理Touch事件。返回true,则向上传递给父控件(注:可能你会觉得是否消费了有关系吗,反正我已经针对事件编写了处理代码?答案是有区别!比如ACTION_MOVE或者ACTION_UP发生的前提是一定曾经发生了ACTION_DOWN,如果你没有消费ACTION_DOWN,那么系统会认为ACTION_DOWN没有发生过,所以ACTION_MOVE或者ACTION_UP就不能被捕获。)
概念介绍
1、onInterceptTouchEvent()是用于处理事件(重点onInterceptTouchEvent这个事件是从父控件开始往子控件传的,直到有拦截或者到没有这个事件的view,然后就往回从子到父控件,这次是onTouch的)(类似于预处理,当然也可以不处理)并改变事件的传递方向,也就是决定是否允许Touch事件继续向下(子控件)传递,一但返回True(代表事件在当前的viewGroup中会被处理),则向下传递之路被截断(所有子控件将没有机会参与Touch事件),同时把事件传递给当前的控件的onTouchEvent()处理;返回false,则把事件交给子控件的onInterceptTouchEvent()
2、onTouchEvent()用于处理事件(重点onTouch这个事件是从子控件回传到父控件的,一层层向下传),返回值决定当前控件是否消费(consume)了这个事件,也就是说在当前控件在处理完Touch事件后,是否还允许Touch事件继续向上(父控件)传递。返回false,则向上传递给父控件,详细一点就是这个touch事件就给了父控件,那么后面的up事件就是到这里touch触发,不会在传给它的子控件。如果父控件依然是false,那touch的处理就给到父控件的父控件,那么up的事件处理都在父控件的父控件,不会触发下面的。
返回true,如果是子控件返回true,那么它的touch事件都在这里处理,父控件是处理不了,因为它收不到子控件传给他的touch,被子控件给拦截了。(这里啰嗦了这么多就是为了加深记忆,这个两个事件理解起来都这么麻烦了,更何况去记,记我肯定是一下子就忘的了^0^)
(注:可能你会觉得是否消费了有关系吗,反正我已经针对事件编写了处理代码?答案是有区别!比如ACTION_MOVE或者ACTION_UP发生的前提是一定曾经发生了ACTION_DOWN,如果你没有消费ACTION_DOWN,那么系统会认为ACTION_DOWN没有发生过,所以ACTION_MOVE或者ACTION_UP就不能被捕获。)
详细介绍
onInterceptTouchEvent()是ViewGroup的一个方法,目的是在系统向该ViewGroup及其各个childView触发onTouchEvent()之前对相关事件进行一次拦截,Android这么设计的想法也很好理解,由于ViewGroup会包含若干childView,因此需要能够统一监控各种touch事件的机会,因此纯粹的不能包含子view的控件是没有这个方法的,如LinearLayout就有,TextView就没有。
onInterceptTouchEvent()使用也很简单,如果在ViewGroup里覆写了该方法,那么就可以对各种touch事件加以拦截。但是如何拦截,是否所有的touch事件都需要拦截则是比较复杂的,touch事件在onInterceptTouchEvent()和onTouchEvent以及各个childView间的传递机制完全取决于onInterceptTouchEvent()和onTouchEvent()的返回值。并且,针对down事件处理的返回值直接影响到后续move和up事件的接收和传递。
关于返回值的问题,基本规则很清楚,如果return true,那么表示该方法消费了此次事件,如果return false,那么表示该方法并未处理完全,该事件仍然需要以某种方式传递下去继续等待处理。
onInterceptTouchEvent()是ViewGroup的一个方法,目的是在系统向该ViewGroup及其各个childView触发onTouchEvent()之前对相关事件进行一次拦截.
-
down事件首先会传递到onInterceptTouchEvent()方法
-
如果该ViewGroup的onInterceptTouchEvent()在接收到down事件处理完成之后return false,那么后续的move, up等事件将继续会先传递给该ViewGroup,之后才和down事件一样传递给最终的目标view的onTouchEvent()处理。
-
如果该ViewGroup的onInterceptTouchEvent()在接收到down事件处理完成之后return true,那么后续的move, up等事件将不再传递给onInterceptTouchEvent(),而是和down事件一样传递给该ViewGroup的onTouchEvent()处理,注意,目标view将接收不到任何事件。
-
如果最终需要处理事件的view的onTouchEvent()返回了false,那么该事件将被传递至其上一层次的view的onTouchEvent()处理。
-
如果最终需要处理事件的view 的onTouchEvent()返回了true,那么后续事件将可以继续传递给该view的onTouchEvent()处理。
仅仅看这个官方文档解释,就能理解清楚这两个函数关系以及用途的绝对是富有经验的framework高手。
否则,一定需要一个案例来阐释。假设我们有这样一个layout,非常典型的<ol style="LIST-STYLE-POSITION: outside; BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; BORDER-RIGHT-STYLE: none; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="linenums"><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L0"><span style="COLOR: maroon; FONT-WEIGHT: bold" class="tag"><com.test.LayoutView1</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: red" class="atn">xmlns:android</span><span style="COLOR: rgb(0,0,0)" class="pun">=</span><span style="COLOR: blue" class="atv">"http://schemas.android.com/apk/res/android"</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L1"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: red" class="atn">android:orientation</span><span style="COLOR: rgb(0,0,0)" class="pun">=</span><span style="COLOR: blue" class="atv">"vertical"</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: red" class="atn">android:layout_width</span><span style="COLOR: rgb(0,0,0)" class="pun">=</span><span style="COLOR: blue" class="atv">"fill_parent"</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L2"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: red" class="atn">android:layout_height</span><span style="COLOR: rgb(0,0,0)" class="pun">=</span><span style="COLOR: blue" class="atv">"fill_parent"</span><span style="COLOR: maroon; FONT-WEIGHT: bold" class="tag">></span><span style="COLOR: rgb(0,0,0)" class="pln"> </span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L3"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: maroon; FONT-WEIGHT: bold" class="tag"><com.test.LayoutView2</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L4"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: red" class="atn">android:orientation</span><span style="COLOR: rgb(0,0,0)" class="pun">=</span><span style="COLOR: blue" class="atv">"vertical"</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: red" class="atn">android:layout_width</span><span style="COLOR: rgb(0,0,0)" class="pun">=</span><span style="COLOR: blue" class="atv">"fill_parent"</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L5"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: red" class="atn">android:layout_height</span><span style="COLOR: rgb(0,0,0)" class="pun">=</span><span style="COLOR: blue" class="atv">"fill_parent"</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: red" class="atn">android:gravity</span><span style="COLOR: rgb(0,0,0)" class="pun">=</span><span style="COLOR: blue" class="atv">"center"</span><span style="COLOR: maroon; FONT-WEIGHT: bold" class="tag">></span><span style="COLOR: rgb(0,0,0)" class="pln"> </span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L6"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: maroon; FONT-WEIGHT: bold" class="tag"><com.test.MyTextView</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L7"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: red" class="atn">android:layout_width</span><span style="COLOR: rgb(0,0,0)" class="pun">=</span><span style="COLOR: blue" class="atv">"wrap_content"</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: red" class="atn">android:layout_height</span><span style="COLOR: rgb(0,0,0)" class="pun">=</span><span style="COLOR: blue" class="atv">"wrap_content"</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L8"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: maroon; FONT-WEIGHT: bold" class="tag">/></span><span style="COLOR: rgb(0,0,0)" class="pln"> </span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L9"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: maroon; FONT-WEIGHT: bold" class="tag"></com.test.LayoutView2></span><span style="COLOR: rgb(0,0,0)" class="pln"> </span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L0"><span style="COLOR: rgb(0,0,0)" class="pln"></span><span style="COLOR: maroon; FONT-WEIGHT: bold" class="tag"></com.test.LayoutView1></span></li></ol>
用一个示例图来解释这个layout:
通常外围的layoutview1,layoutview2,只是布局的容器不需要响应触屏的点击事件,仅仅Mytextview需要相应点击。但这只是一般情况,一些特殊的布局可能外围容器也要响应,甚至不让里面的mytextview去响应。更有特殊的情况是,动态更换响应对象。
那么首先看一下默认的触屏事件的在两个函数之间的传递流程。如下图:
如果仅仅想让MyTextView来响应触屏事件,让MyTextView的OnTouchEvent返回true,那么事件流就变成如下图,可以看到layoutview1,layoutview2已经不能进入OnTouchEvent:
另外一种情况,就是外围容器想独自处理触屏事件,那么就应该在相应的onInterceptTouchEvent函数中返回true,表示要截获触屏事件,比如layoutview1作截获处理,处理流变成如下图:
以此类推,我们可以得到各种具体的情况,整个layout的view类层次中都有机会截获,而且能看出来外围的容器view具有优先截获权。
当我们去做一些相对来讲具有更复杂的触屏交互效果的应用时候,经常需要动态变更touch event的处理对象,比如launcher待机桌面和主菜单(见下图),从滑动屏幕开始到停止滑动过程当中,只有外围的容器view才可以处理touch event,否则就会误点击上面的应用图标或者widget.反之在静止不动的状态下则需要能够响应图标(子view)的touch事件。摘取framework中abslistview代码如下
<ol style="LIST-STYLE-POSITION: outside; BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; BORDER-RIGHT-STYLE: none; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="linenums"><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L0"><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">public</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">boolean</span><span style="COLOR: rgb(0,0,0)" class="pln"> onInterceptTouchEvent</span><span style="COLOR: rgb(0,0,0)" class="pun">(</span><span style="COLOR: rgb(43,145,175); FONT-WEIGHT: bold" class="typ">MotionEvent</span><span style="COLOR: rgb(0,0,0)" class="pln"> ev</span><span style="COLOR: rgb(0,0,0)" class="pun">)</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,0)" class="pun">{</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L1"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">int</span><span style="COLOR: rgb(0,0,0)" class="pln"> action </span><span style="COLOR: rgb(0,0,0)" class="pun">=</span><span style="COLOR: rgb(0,0,0)" class="pln"> ev</span><span style="COLOR: rgb(0,0,0)" class="pun">.</span><span style="COLOR: rgb(0,0,0)" class="pln">getAction</span><span style="COLOR: rgb(0,0,0)" class="pun">();</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L2"><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L3"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">switch</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,0)" class="pun">(</span><span style="COLOR: rgb(0,0,0)" class="pln">action </span><span style="COLOR: rgb(0,0,0)" class="pun">&</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(43,145,175); FONT-WEIGHT: bold" class="typ">MotionEvent</span><span style="COLOR: rgb(0,0,0)" class="pun">.</span><span style="COLOR: rgb(0,0,0)" class="pln">ACTION_MASK</span><span style="COLOR: rgb(0,0,0)" class="pun">)</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,0)" class="pun">{</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L4"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">case</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(43,145,175); FONT-WEIGHT: bold" class="typ">MotionEvent</span><span style="COLOR: rgb(0,0,0)" class="pun">.</span><span style="COLOR: rgb(0,0,0)" class="pln">ACTION_DOWN</span><span style="COLOR: rgb(0,0,0)" class="pun">:</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,0)" class="pun">{</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L5"><span style="COLOR: rgb(0,0,0)" class="pln"> </span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L6"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">if</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,0)" class="pun">(</span><span style="COLOR: rgb(0,0,0)" class="pln">touchMode </span><span style="COLOR: rgb(0,0,0)" class="pun">==</span><span style="COLOR: rgb(0,0,0)" class="pln"> TOUCH_MODE_FLING</span><span style="COLOR: rgb(0,0,0)" class="pun">)</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,0)" class="pun">{</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L7"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">return</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">true</span><span style="COLOR: rgb(0,0,0)" class="pun">;</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: gray" class="com">//fling状态,截获touch,因为在滑动状态,不让子view处理</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L8"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,0)" class="pun">}</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L9"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">break</span><span style="COLOR: rgb(0,0,0)" class="pun">;</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L0"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,0)" class="pun">}</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L1"><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L2"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">case</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(43,145,175); FONT-WEIGHT: bold" class="typ">MotionEvent</span><span style="COLOR: rgb(0,0,0)" class="pun">.</span><span style="COLOR: rgb(0,0,0)" class="pln">ACTION_MOVE</span><span style="COLOR: rgb(0,0,0)" class="pun">:</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,0)" class="pun">{</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L3"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">switch</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,0)" class="pun">(</span><span style="COLOR: rgb(0,0,0)" class="pln">mTouchMode</span><span style="COLOR: rgb(0,0,0)" class="pun">)</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,0)" class="pun">{</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L4"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">case</span><span style="COLOR: rgb(0,0,0)" class="pln"> TOUCH_MODE_DOWN</span><span style="COLOR: rgb(0,0,0)" class="pun">:</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L5"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">final</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">int</span><span style="COLOR: rgb(0,0,0)" class="pln"> pointerIndex </span><span style="COLOR: rgb(0,0,0)" class="pun">=</span><span style="COLOR: rgb(0,0,0)" class="pln"> ev</span><span style="COLOR: rgb(0,0,0)" class="pun">.</span><span style="COLOR: rgb(0,0,0)" class="pln">findPointerIndex</span><span style="COLOR: rgb(0,0,0)" class="pun">(</span><span style="COLOR: rgb(0,0,0)" class="pln">mActivePointerId</span><span style="COLOR: rgb(0,0,0)" class="pun">);</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L6"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">final</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">int</span><span style="COLOR: rgb(0,0,0)" class="pln"> y </span><span style="COLOR: rgb(0,0,0)" class="pun">=</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,0)" class="pun">(</span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">int</span><span style="COLOR: rgb(0,0,0)" class="pun">)</span><span style="COLOR: rgb(0,0,0)" class="pln"> ev</span><span style="COLOR: rgb(0,0,0)" class="pun">.</span><span style="COLOR: rgb(0,0,0)" class="pln">getY</span><span style="COLOR: rgb(0,0,0)" class="pun">(</span><span style="COLOR: rgb(0,0,0)" class="pln">pointerIndex</span><span style="COLOR: rgb(0,0,0)" class="pun">);</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L7"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">if</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,0)" class="pun">(</span><span style="COLOR: rgb(0,0,0)" class="pln">startScrollIfNeeded</span><span style="COLOR: rgb(0,0,0)" class="pun">(</span><span style="COLOR: rgb(0,0,0)" class="pln">y </span><span style="COLOR: rgb(0,0,0)" class="pun">-</span><span style="COLOR: rgb(0,0,0)" class="pln"> mMotionY</span><span style="COLOR: rgb(0,0,0)" class="pun">))</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,0)" class="pun">{</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L8"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">return</span><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">true</span><span style="COLOR: rgb(0,0,0)" class="pun">;</span><span style="COLOR: gray" class="com">//开始滑动状态,截获touch事件,不让子view处理</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L9"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,0)" class="pun">}</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L0"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">break</span><span style="COLOR: rgb(0,0,0)" class="pun">;</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L1"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,0)" class="pun">}</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L2"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,139); FONT-WEIGHT: bold" class="kwd">break</span><span style="COLOR: rgb(0,0,0)" class="pun">;</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L3"><span style="COLOR: rgb(0,0,0)" class="pln"> </span><span style="COLOR: rgb(0,0,0)" class="pun">}</span><span style="COLOR: rgb(0,0,0)" class="pln"></span> </li><li style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 20px; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(250,250,250); LIST-STYLE-TYPE: decimal; MARGIN: 0px; PADDING-LEFT: 10px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(153,153,153); BORDER-LEFT-STYLE: none; PADDING-TOP: 0px" class="L4"><span style="COLOR: rgb(0,0,0)" class="pln"></span><span style="COLOR: rgb(0,0,0)" class="pun">}</span></li></ol>
总结:
仅仅通过概览性的官方文档是很难理解onInterceptTouchEvent函数的用途的,只有通过演绎这个抽象的规则,配以图文才能获取这个重要的知识。很显然,默认是返回false,不做截获。返回true之后,事件流的后端控件就没有机会处理touch事件了,把默认的事件流中每个处理函数看作一个节点,这个节点只要返回true, 后续的事件就被截止了,这样想就很好理解。
onInterceptTouchEvent()用于处理事件并改变事件的传递方向。返回值为false时事件会传递给子控件的onInterceptTouchEvent();返回值为true时事件会传递给当前控件的onTouchEvent(),而不在传递给子控件,这就是所谓的Intercept(截断)。
onTouchEvent() 用于处理事件,返回值决定当前控件是否消费(consume)了这个事件。可能你要问是否消费了又区别吗,反正我已经针对事件编写了处理代码?答案是有区别!比如ACTION_MOVE或者ACTION_UP发生的前提是一定曾经发生了ACTION_DOWN,如果你没有消费ACTION_DOWN,那么系统会认为ACTION_DOWN没有发生过,所以ACTION_MOVE或者ACTION_UP就不能被捕获。
ViewGroup里的onInterceptTouchEvent默认值是false这样才能把事件传给View里的onTouchEvent.
ViewGroup里的onTouchEvent默认值是false。
View里的onTouchEvent返回默认值是true.这样才能执行多次touch事件。