Android的觸摸事件:onClick, onScroll, onFling等等,都是由許多個(gè)Touch組成的。其中Touch的第一個(gè)狀態(tài)肯定是ACTION_DOWN, 表示按下了屏幕。之后,touch將會有后續(xù)事件,可能是:
ACTION_MOVE ?//表示為移動(dòng)手勢
ACTION_UP ?//表示為離開屏幕
ACTION_CANCEL ?//表示取消手勢,不會由用戶產(chǎn)生,而是由程序產(chǎn)生的
一個(gè)Action_DOWN, n個(gè)ACTION_MOVE, 1個(gè)ACTION_UP,就構(gòu)成了Android中眾多的事件。
在Android中,有一類控件是中還可以包含其他的子控件,這類控件是繼承于ViewGroup類,例如:ListView, Gallery, GridView,LinearLayout。
還有一類控件是不能再包含子控件,例如:TextView。
在觸發(fā)OnTouch事件的時(shí)候Android的GroupView會調(diào)用如下三個(gè)函數(shù):
public?boolean?dispatchTouchEvent(MotionEvent?ev)????? //用于事件的分發(fā)
public?boolean?onInterceptTouchEvent(MotionEvent?ev)????//? 用于事件的攔截
public?boolean?onTouchEvent(MotionEvent?ev)???? //處理事件
本文的主要討論對象就是ViewGroup類的控件嵌套時(shí)事件觸發(fā)情況。
對于ViewGroup類的控件,有一個(gè)很重要的方法,就是onInterceptTouchEvent(),用于處理事件并改變事件的傳遞方向,它的返回值是一個(gè)布爾值,決定了Touch事件是否要向它包含的子View繼續(xù)傳遞,這個(gè)方法是從父View向子View傳遞。而方法onTouchEvent(),用于接收事件并處理,它的返回值也是一個(gè)布爾值,決定了事件及后續(xù)事件是否繼續(xù)向上傳遞,這個(gè)方法是從子View向父View傳遞。touch事件在 onInterceptTouchEvent()和onTouchEvent以及各個(gè)childView間的傳遞機(jī)制完全取決于onInterceptTouchEvent()和onTouchEvent()的返回值。返回值為true表示事件被正確接收和處理了,返回值為false表示事件沒有被處理,將繼續(xù)傳遞下去。
ACTION_DOWN事件會傳到某個(gè)ViewGroup類的onInterceptTouchEvent,如果返回false,則DOWN事件繼續(xù)向子ViewGroup類的onInterceptTouchEvent傳遞,如果子View不是ViewGroup類的控件,則傳遞給它的onTouchEvent。
如果onInterceptTouchEvent返回了true,則DOWN事件傳遞給它的onTouchEvent,不再繼續(xù)傳遞,并且之后的后續(xù)事件也都傳遞給它的onTouchEvent。
如果某View的onTouchEvent返回了false,則DOWN事件繼續(xù)向其父ViewGroup類的onTouchEvent傳遞;如果返回了true,則后續(xù)事件會直接傳遞給其onTouchEvent繼續(xù)處理。(后續(xù)事件只會傳遞給對于必要事件ACTION_DOWN返回了true的onTouchEvent。
onInterceptTouchEvent()用于處理事件并改變事件的傳遞方向。處理事件這個(gè)不用說了,你在函數(shù)內(nèi)部編寫代碼處理就可以了。而決定傳遞方向的是返回值,返回為false時(shí)事件會傳遞給子控件的onInterceptTouchEvent();返回值為true時(shí)事件會傳遞給當(dāng)前控件的onTouchEvent(),而不在傳遞給子控件,這就是所謂的Intercept(截?cái)?。
onTouchEvent() 用于處理事件,返回值決定當(dāng)前控件是否消費(fèi)(consume)了這個(gè)事件。可能你要問是否消費(fèi)了又區(qū)別嗎,反正我已經(jīng)針對事件編寫了處理代碼?答案是有區(qū)別!比如ACTION_MOVE或者ACTION_UP發(fā)生的前提是一定曾經(jīng)發(fā)生了ACTION_DOWN,如果你沒有消費(fèi)ACTION_DOWN,那么系統(tǒng)會認(rèn)為ACTION_DOWN沒有發(fā)生過,所以ACTION_MOVE或者ACTION_UP就不能被捕獲。
在沒有重寫onInterceptTouchEvent()和onTouchEvent()的情況下(他們的返回值都是false)。
?
onTouch事件傳遞測試:
[java]?view plaincopy
<?xml?version="1.0"?encoding="utf-8"?>?? <com.rpset.test.MyLinearLayout1?xmlns:android="http://schemas.android.com/apk/res/android"?? ????android:layout_width="fill_parent"?? ????android:layout_height="fill_parent"?? ????android:orientation="vertical"?>?? ????<com.rpset.test.MyLinearLayout2?? ????????android:layout_width="fill_parent"?? ????????android:layout_height="fill_parent"?? ????????android:gravity="center"?? ????????android:orientation="vertical"?>?? ????????<com.rpset.test.MyTextView?? ????????????android:id="@+id/tv"?? ????????????android:layout_width="200px"?? ????????????android:layout_height="200px"?? ????????????android:background="#FFFFFF"?? ????????????android:text="MyTextView"?? ????????????android:textColor="#0000FF"?? ????????????android:textSize="40sp"?? ????????????android:textStyle="bold"?/>?? ????</com.rpset.test.MyLinearLayout2>?? </com.rpset.test.MyLinearLayout1>??
注:?當(dāng)點(diǎn)擊MyTextView時(shí),程序會先進(jìn)入到LinearLayout1的dispatchTouchEvent中,這個(gè)類必須調(diào)用super.dispatchTouchEvent(ev);否則后面的兩個(gè)方法無法觸發(fā),所以發(fā)現(xiàn)這個(gè)方法根本沒有必要重寫,因?yàn)榭蚣苁窃?super.dispatchTouchEvent(ev)中來調(diào)用onInterceptTouchEvent和onTouchEvent方法的,所以手動(dòng)的設(shè)置dispatchTouchEvent的返回值是無效的,除非你不想讓框架觸發(fā)這兩個(gè)方法。
?
對于MyTextView進(jìn)行測試:
測試一:當(dāng)三個(gè)view的dispatchTouchEvent,onInterceptTouchEvent(MyTextView沒有此方法),onTouchEvent均返回false,也就是說事件始終沒有被消費(fèi),那后續(xù)事件(ACTION_DOWN的ACTION_MOVE或者ACTION_UP)不會觸發(fā)。Log信息如下:
[java]?view plaincopy
04-09?12:01:55.019:?D/MyLinearLayout(5435):?MyLinearLayout1——dispatchTouchEvent?action:ACTION_DOWN?? 04-09?12:01:55.027:?D/MyLinearLayout(5435):?MyLinearLayout1——onInterceptTouchEvent?action:ACTION_DOWN?? 04-09?12:01:55.043:?D/MyLinearLayout(5435):?MyLinearLayout2——dispatchTouchEvent?action:ACTION_DOWN?? 04-09?12:01:55.043:?D/MyLinearLayout(5435):?MyLinearLayout2——onInterceptTouchEvent?action:ACTION_DOWN?? 04-09?12:01:55.047:?D/MyLinearLayout(5435):?MyTextView——dispatchTouchEvent?action:ACTION_DOWN?? 04-09?12:01:55.051:?D/MyLinearLayout(5435):?MyTextView——-onTouchEvent?action:ACTION_DOWN?? 04-09?12:01:55.051:?D/MyLinearLayout(5435):?MyLinearLayout2——-onTouchEvent?action:ACTION_DOWN?? 04-09?12:01:55.054:?D/MyLinearLayout(5435):?MyLinearLayout1——-onTouchEvent?action:ACTION_DOWN??
結(jié)論:MyLinearLayout1,MyLinearLayout2,MyTextView都只處理了ACTION_DOWN,其余的TouchEvent被外層的Activity處理了
?
傳遞示意圖:
?測試二:當(dāng)只有MyTextView的onTouchEvent返回true,即事件最終在這里消費(fèi),(action:ACTION_MOVE會重復(fù)出現(xiàn)多次,這里僅代表一下) Log信息如下:
[java]?view plaincopy
04-09?11:58:21.992:?D/MyLinearLayout(4621):?MyLinearLayout1——dispatchTouchEvent?action:ACTION_DOWN?? 04-09?11:58:21.992:?D/MyLinearLayout(4621):?MyLinearLayout1——onInterceptTouchEvent?action:ACTION_DOWN?? 04-09?11:58:21.992:?D/MyLinearLayout(4621):?MyLinearLayout2——dispatchTouchEvent?action:ACTION_DOWN?? 04-09?11:58:22.000:?D/MyLinearLayout(4621):?MyLinearLayout2——onInterceptTouchEvent?action:ACTION_DOWN?? 04-09?11:58:22.000:?D/MyLinearLayout(4621):?MyTextView——dispatchTouchEvent?action:ACTION_DOWN?? 04-09?11:58:22.000:?D/MyLinearLayout(4621):?MyTextView——-onTouchEvent?action:ACTION_DOWN?? ?? 04-09?11:58:22.117:?D/MyLinearLayout(4621):?MyLinearLayout1——dispatchTouchEvent?action:ACTION_MOVE?? 04-09?11:58:22.117:?D/MyLinearLayout(4621):?MyLinearLayout1——onInterceptTouchEvent?action:ACTION_MOVE?? 04-09?11:58:22.117:?D/MyLinearLayout(4621):?MyLinearLayout2——dispatchTouchEvent?action:ACTION_MOVE?? 04-09?11:58:22.117:?D/MyLinearLayout(4621):?MyLinearLayout2——onInterceptTouchEvent?action:ACTION_MOVE?? 04-09?11:58:22.133:?D/MyLinearLayout(4621):?MyTextView——dispatchTouchEvent?action:ACTION_MOVE?? 04-09?11:58:22.133:?D/MyLinearLayout(4621):?MyTextView——-onTouchEvent?action:ACTION_MOVE?? ?? 04-09?11:58:22.179:?D/MyLinearLayout(4621):?MyLinearLayout1——dispatchTouchEvent?action:ACTION_UP?? 04-09?11:58:22.179:?D/MyLinearLayout(4621):?MyLinearLayout1——onInterceptTouchEvent?action:ACTION_UP?? 04-09?11:58:22.179:?D/MyLinearLayout(4621):?MyLinearLayout2——dispatchTouchEvent?action:ACTION_UP?? 04-09?11:58:22.179:?D/MyLinearLayout(4621):?MyLinearLayout2——onInterceptTouchEvent?action:ACTION_UP?? 04-09?11:58:22.179:?D/MyLinearLayout(4621):?MyTextView——dispatchTouchEvent?action:ACTION_UP?? 04-09?11:58:22.179:?D/MyLinearLayout(4621):?MyTextView——-onTouchEvent?action:ACTION_UP??
結(jié)論:MyTextView處理了所有的TouchEvent。
?
傳遞示意圖:
對MyLinearLayout2進(jìn)行測試:
測試一:當(dāng)MyLinearLayout2的onInterceptTouchEvent方法返回true時(shí),發(fā)送截?cái)?#xff0c;事件不再向下傳遞而是直接給當(dāng)前MyLinearLayout2的onTouchEvent處理,那后續(xù)事件(ACTION_DOWN的ACTION_MOVE或者ACTION_UP)不會觸發(fā)。log信息如下:
[java]?view plaincopy
04-09?12:54:44.398:?D/MyLinearLayout(9177):?MyLinearLayout1——dispatchTouchEvent?action:ACTION_DOWN?? 04-09?12:54:44.398:?D/MyLinearLayout(9177):?MyLinearLayout1——onInterceptTouchEvent?action:ACTION_DOWN?? 04-09?12:54:44.398:?D/MyLinearLayout(9177):?MyLinearLayout2——dispatchTouchEvent?action:ACTION_DOWN?? 04-09?12:54:44.398:?D/MyLinearLayout(9177):?MyLinearLayout2——onInterceptTouchEvent?action:ACTION_DOWN?? 04-09?12:54:44.398:?D/MyLinearLayout(9177):?MyLinearLayout2——-onTouchEvent?action:ACTION_DOWN?? 04-09?12:54:44.398:?D/MyLinearLayout(9177):?MyLinearLayout1——-onTouchEvent?action:ACTION_DOWN??
結(jié)論:MyLinearLayout2,MyLinearLayout1都處理了ACTION_DOWN,其余的由最外層的Activity處理了。
?
傳遞示意圖:
測試二:當(dāng)MyLinearLayout2的onTouchEvent方法返回true時(shí),后續(xù)的MOVE,Up事件會經(jīng)過LayoutView1的onInterceptTouchEvent函數(shù),然后到LayoutView2的onTouchEvent函數(shù)中去。不再經(jīng)過MyLinearLayout2的onInterceptTouchEvent函數(shù)。 Log信息如下:
[java]?view plaincopy
04-09?18:17:26.410:?D/MyLinearLayout(21504):?MyLinearLayout1——dispatchTouchEvent?action:ACTION_DOWN?? 04-09?18:17:26.410:?D/MyLinearLayout(21504):?MyLinearLayout1——onInterceptTouchEvent?action:ACTION_DOWN?? 04-09?18:17:26.414:?D/MyLinearLayout(21504):?MyLinearLayout2——dispatchTouchEvent?action:ACTION_DOWN?? 04-09?18:17:26.414:?D/MyLinearLayout(21504):?MyLinearLayout2——onInterceptTouchEvent?action:ACTION_DOWN?? 04-09?18:17:26.418:?D/MyLinearLayout(21504):?MyTextView——dispatchTouchEvent?action:ACTION_DOWN?? 04-09?18:17:26.418:?D/MyLinearLayout(21504):?MyTextView——-onTouchEvent?action:ACTION_DOWN?? 04-09?18:17:26.418:?D/MyLinearLayout(21504):?MyLinearLayout2——-onTouchEvent?action:ACTION_DOWN?? ?? 04-09?18:17:26.437:?D/MyLinearLayout(21504):?MyLinearLayout1——dispatchTouchEvent?action:ACTION_UP?? 04-09?18:17:26.437:?D/MyLinearLayout(21504):?MyLinearLayout1——onInterceptTouchEvent?action:ACTION_UP?? 04-09?18:17:26.441:?D/MyLinearLayout(21504):?MyLinearLayout2——dispatchTouchEvent?action:ACTION_UP?? 04-09?18:17:26.445:?D/MyLinearLayout(21504):?MyLinearLayout2——-onTouchEvent?action:ACTION_UP??
結(jié)論:MyTextView只處理了ACTION_DOWN事件,MyLinearLayout2處理了所有的TouchEvent事件。
傳遞示意圖:
?
測試三:當(dāng)MyLinearLayout2的onInterceptTouchEvent和onTouchEvent 方法都返回true時(shí),后續(xù)的MOVE,Up事件依然會經(jīng)過LayoutView1的onInterceptTouchEvent函數(shù),然后到LayoutView2的onTouchEvent函數(shù)中去。不再經(jīng)過MyLinearLayout2的onInterceptTouchEvent函數(shù)。?Log信息如下:
[java]?view plaincopy
04-09?13:17:30.515:?D/MyLinearLayout(9935):?MyLinearLayout1——dispatchTouchEvent?action:ACTION_DOWN?? 04-09?13:17:30.515:?D/MyLinearLayout(9935):?MyLinearLayout1——onInterceptTouchEvent?action:ACTION_DOWN?? 04-09?13:17:30.515:?D/MyLinearLayout(9935):?MyLinearLayout2——dispatchTouchEvent?action:ACTION_DOWN?? 04-09?13:17:30.515:?D/MyLinearLayout(9935):?MyLinearLayout2——onInterceptTouchEvent?action:ACTION_DOWN?? 04-09?13:17:30.515:?D/MyLinearLayout(9935):?MyLinearLayout2——-onTouchEvent?action:ACTION_DOWN?? ?? 04-09?13:17:30.547:?D/MyLinearLayout(9935):?MyLinearLayout1——dispatchTouchEvent?action:ACTION_MOVE?? 04-09?13:17:30.547:?D/MyLinearLayout(9935):?MyLinearLayout1——onInterceptTouchEvent?action:ACTION_MOVE?? 04-09?13:17:30.547:?D/MyLinearLayout(9935):?MyLinearLayout2——dispatchTouchEvent?action:ACTION_MOVE?? 04-09?13:17:30.551:?D/MyLinearLayout(9935):?MyLinearLayout2——-onTouchEvent?action:ACTION_MOVE?? ?? 04-09?13:17:30.644:?D/MyLinearLayout(9935):?MyLinearLayout1——dispatchTouchEvent?action:ACTION_UP?? 04-09?13:17:30.644:?D/MyLinearLayout(9935):?MyLinearLayout1——onInterceptTouchEvent?action:ACTION_UP?? 04-09?13:17:30.644:?D/MyLinearLayout(9935):?MyLinearLayout2——dispatchTouchEvent?action:ACTION_UP?? 04-09?13:17:30.648:?D/MyLinearLayout(9935):?MyLinearLayout2——-onTouchEvent?action:ACTION_UP??
結(jié)論:MyLinearLayout2處理了所有的TouchEvent。
?傳遞示意圖:
對于MyLinearLayout1進(jìn)行測試:
測試一:當(dāng)MyLinearLayout1的onInterceptTouchEvent方法返回true時(shí),發(fā)送截?cái)?#xff0c;事件不再向下傳遞而是直接給當(dāng)前MyLinearLayout1的onTouchEvent處理,那后續(xù)事件(ACTION_DOWN的ACTION_MOVE或者ACTION_UP)不會觸發(fā)。log信息如下:
[java]?view plaincopy
04-09?13:52:06.789:?D/MyLinearLayout(12363):?MyLinearLayout1——dispatchTouchEvent?action:ACTION_DOWN?? 04-09?13:52:06.789:?D/MyLinearLayout(12363):?MyLinearLayout1——onInterceptTouchEvent?action:ACTION_DOWN?? 04-09?13:52:06.789:?D/MyLinearLayout(12363):?MyLinearLayout1——-onTouchEvent?action:ACTION_DOWN??
結(jié)論:MyLinearLayout1只處理了ACTION_DOWN,其余的被外層的Avtivity處理了。
傳遞示意圖:
測試二:當(dāng)MyLinearLayout1的onTouchEvent 方法返回true時(shí),后續(xù)的MOVE,Up事件會直接傳給MyLinearLayout1的onTouchEvent()。不再經(jīng)過MyLinearLayout1的onInterceptTouchEvent函數(shù)。Log信息如下:
[java]?view plaincopy
04-09?18:26:58.125:?D/MyLinearLayout(22294):?MyLinearLayout1——dispatchTouchEvent?action:ACTION_DOWN?? 04-09?18:26:58.125:?D/MyLinearLayout(22294):?MyLinearLayout1——onInterceptTouchEvent?action:ACTION_DOWN?? 04-09?18:26:58.125:?D/MyLinearLayout(22294):?MyLinearLayout2——dispatchTouchEvent?action:ACTION_DOWN?? 04-09?18:26:58.125:?D/MyLinearLayout(22294):?MyLinearLayout2——onInterceptTouchEvent?action:ACTION_DOWN?? 04-09?18:26:58.125:?D/MyLinearLayout(22294):?MyTextView——dispatchTouchEvent?action:ACTION_DOWN?? 04-09?18:26:58.125:?D/MyLinearLayout(22294):?MyTextView——-onTouchEvent?action:ACTION_DOWN?? 04-09?18:26:58.125:?D/MyLinearLayout(22294):?MyLinearLayout2——-onTouchEvent?action:ACTION_DOWN?? 04-09?18:26:58.125:?D/MyLinearLayout(22294):?MyLinearLayout1——-onTouchEvent?action:ACTION_DOWN?? ?? 04-09?18:26:58.215:?D/MyLinearLayout(22294):?MyLinearLayout1——dispatchTouchEvent?action:ACTION_MOVE?? 04-09?18:26:58.215:?D/MyLinearLayout(22294):?MyLinearLayout1——-onTouchEvent?action:ACTION_MOVE?? ?? 04-09?18:26:58.281:?D/MyLinearLayout(22294):?MyLinearLayout1——dispatchTouchEvent?action:ACTION_UP?? 04-09?18:26:58.285:?D/MyLinearLayout(22294):?MyLinearLayout1——-onTouchEvent?action:ACTION_UP??
結(jié)論:MyTextView和MyLinearLayout2只處理了ACTION_DOWN事件,MyLinearLayout1處理了所有的TouchEvent。
傳遞示意圖:
測試三:當(dāng)MyLinearLayout1的onInterceptTouchEvent和onTouchEvent 方法都返回true時(shí),后續(xù)的MOVE,Up事件會直接傳給MyLinearLayout1的onTouchEvent(),不傳給其他任何控件的任何函數(shù)。不再經(jīng)過MyLinearLayout1的onInterceptTouchEvent函數(shù)。Log信息如下:
[java]?view plaincopy
04-09?13:58:04.199:?D/MyLinearLayout(13116):?MyLinearLayout1——dispatchTouchEvent?action:ACTION_DOWN?? 04-09?13:58:04.199:?D/MyLinearLayout(13116):?MyLinearLayout1——onInterceptTouchEvent?action:ACTION_DOWN?? 04-09?13:58:04.203:?D/MyLinearLayout(13116):?MyLinearLayout1——-onTouchEvent?action:ACTION_DOWN?? ?? 04-09?13:58:04.324:?D/MyLinearLayout(13116):?MyLinearLayout1——dispatchTouchEvent?action:ACTION_MOVE?? 04-09?13:58:04.328:?D/MyLinearLayout(13116):?MyLinearLayout1——-onTouchEvent?action:ACTION_MOVE?? ?? 04-09?13:58:04.367:?D/MyLinearLayout(13116):?MyLinearLayout1——dispatchTouchEvent?action:ACTION_UP?? 04-09?13:58:04.367:?D/MyLinearLayout(13116):?MyLinearLayout1——-onTouchEvent?action:ACTION_UP??
結(jié)論:MyLinearLayout1處理了所有的TouchEvent。
?傳遞示意圖:
來源:?<http://blog.csdn.net/hyp712/article/details/8777835>
總結(jié)
以上是生活随笔為你收集整理的onTouch事件机制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。