版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、android觸摸事件分發(fā)攔截機(jī)制總結(jié)android提供了強(qiáng)大的事件處理機(jī)制,包括兩套事件處理 機(jī)制:基于監(jiān)聽(tīng)的事件處理,基于回調(diào)的事件處理。本文重點(diǎn)要說(shuō)的事件分發(fā)攔截機(jī)制就是回調(diào)事件的處理 機(jī)制中的其中一個(gè)知識(shí)。分發(fā)攔截事件的處理機(jī)制也是也是 一個(gè)比較難理解或者不容易記住的知識(shí)。學(xué)習(xí)這個(gè)事件處理機(jī)制之前,我還是先把我個(gè)人對(duì)于這個(gè) 比較疑難雜癥的學(xué)習(xí)方法對(duì)大家說(shuō)一下,希望對(duì)大家學(xué)習(xí)有 一些幫助。就這個(gè)事件分發(fā)攔截機(jī)制來(lái)說(shuō),我之前看了很多 別人網(wǎng)上的資料,博客,雖然能有一些收獲,但是我一直都 沒(méi)能去很深刻理解完全,對(duì)于事件分發(fā)、事件攔截,它們這 兩個(gè)回調(diào)方法,這個(gè)都是很多人知道的,但是它們有什么
2、區(qū) 別,又有什么聯(lián)系?我看完它們的總結(jié)后思路是有些混亂 的,有些網(wǎng)上的資料寫(xiě)的也是并不完全正確或準(zhǔn)確的,那這 個(gè)時(shí)候我們又該怎樣去學(xué)習(xí),或應(yīng)該怎樣去對(duì)待這個(gè)知識(shí) 點(diǎn)?我是從這幾個(gè)方面思考的:1)這個(gè)知識(shí)點(diǎn)有沒(méi)有應(yīng)用 的場(chǎng)合,如果沒(méi)有,那么肯定是沒(méi)有必要去學(xué)習(xí)的,如果有 應(yīng)用的場(chǎng)合,那么它的應(yīng)用場(chǎng)合是什么,比如這里事件分成 機(jī)制可以讓界面中的父布局或子布局在某些情況下,失去屏 幕控制的效果。2)這個(gè)知識(shí)點(diǎn)涉及到的一些方法,比如這 事件觸摸涉及到三個(gè)方法:事件分發(fā)方法,事件攔截方法, 事件處理方法;我們要知道這三個(gè)方法的作用。3)我們要 知道涉及到的這三個(gè)方法在改變參數(shù)/返回值后,實(shí)際效果 會(huì)發(fā)生
3、怎樣的變化,這就需要我們動(dòng)手去試試,并把它記錄 下來(lái),進(jìn)行對(duì)比,你就可以得出結(jié)論。很多東西,如果你不 動(dòng)手試試,你是會(huì)存在一些特殊的疑問(wèn)的。所以動(dòng)手是徹底 消除疑慮最好方法。下面我來(lái)詳細(xì)介紹android的事件分發(fā)攔截處理機(jī)制。也 許你看完了我的總結(jié),也不一定能對(duì)android事件的分發(fā)和 攔截有很深的理解,也是很正常的,因?yàn)槊恳粋€(gè)人的想法都 是不一樣的,但是你能認(rèn)識(shí)到我講解的程序中的實(shí)現(xiàn)功能,或它的實(shí)現(xiàn)效果其實(shí)就已經(jīng)夠了,我們學(xué)習(xí)知識(shí)不就是能得 到些許應(yīng)用嗎,其他的管那么多干嘛,呵呵!一.相關(guān)概念不管是桌面應(yīng)用還是手機(jī)應(yīng)用程序,需要為用戶的動(dòng)作 提供響應(yīng),這種為用戶動(dòng)作提供響應(yīng)的機(jī)制就是事件
4、處理。touch事件處理中的分發(fā)攔截機(jī)制中有三個(gè)方法是必須要 知 道和理解的:dispatchtouchevent (motionevent ev) > onlntercepttouchevent(motioneventev)、ontouchevent (motionevent ev);能夠響應(yīng)這些方法的控件 包括:viewgroup及其子類、activity。方法與控件的對(duì)應(yīng) 關(guān)系如下表所示:方法功能viewgroupactivity事件分發(fā)yesyes事件攔截yesno事件響應(yīng)yesyestouch事件相關(guān)方法public boolean dispatchtouchevent(mot
5、ionevent ev) public boolean onlntercepttouchevent(moti on event ev) public boolean ontouchevent(iviotionevent ev)、touch事件分析(一)事件分發(fā):public boolean dispatchtouchevent(motionevent ev)touch 事件發(fā)生時(shí) activity 的 di spatchtouchevent (motionevent ev)方法會(huì)以隧道 方式(從根元素依次往下傳遞直到最內(nèi)層子元素或在中間某一元素中rti于某一條件停止傳 遞)將事件傳遞給最外層v
6、iew的dispatchtouchevent (motionevent ev)方法,并由該 view 的 dispatchtouchevent(motionevent. ev)方法對(duì)事件進(jìn)行分發(fā)。di spatchtouchevent 的事件分發(fā)邏輯如下:1. 如果 return true,事件會(huì)分發(fā)給當(dāng)前view并由dispatchtouchevent方法進(jìn)行 消費(fèi),同時(shí)事件會(huì)停止向下傳遞;2. 如果return false,事件分發(fā)分為兩種情況:1) 如果當(dāng)前view獲取的事件直接來(lái)自activity,貝!i會(huì)將事件返回給activity的 ontouchevent進(jìn)彳亍消費(fèi);2) 如果當(dāng)
7、前view獲取的事件來(lái)自外層父控件,則會(huì)將事件返回給父view的 ontouchevent 進(jìn)行消費(fèi)。3. 如果返回系統(tǒng)默認(rèn)的super. dispatchtouchevent (ev),事件會(huì)自動(dòng)的分發(fā)給當(dāng)前 v i ew 的 on in tercepttoucheve nt 方法。(二)事件攔截:public boo lean on intcrcopttouchevcnt(motionevent ev)在外層view的dispatchtouchevent (motionevent ev)方法返回系統(tǒng)默認(rèn)的super. dispatchtouchevent(ev)情況下,事件會(huì)自動(dòng)的分發(fā)給當(dāng)
8、前view的 onlntercepttouchevent 方法。onlntercepttouchevent 的事件攔截邏輯如下:1. 如果onlntercepttouchevent返回true,則表示將事件進(jìn)行攔截,并將攔截到的 事件交由當(dāng)前view的ontouchevent進(jìn)行處理;2. 如果onlntercepttouchevent返回false,貝表示將事件放行,當(dāng)前view上的事件會(huì)被傳遞到子view上,再由子view的dispatchtouchevent來(lái)開(kāi)始這個(gè)事件的分發(fā);3. 如果 onlntercepttouchevent 返回 super. onlntercepttouche
9、vent (ev),事件默認(rèn) 會(huì)被攔截,并將攔截到的事件交rfl當(dāng)前view的ontouchevent進(jìn)行處理。(二)事件響應(yīng):public boolean ontouchevent(motionevent ev)在 dispatchtouchevent 返冋 super, d i spatchtouchevent (ev) 并且 onlntercepttouchevent 返回 true 或返回 super, onlntercepttouchevent (ev)的情況下 ontouchevent會(huì)被調(diào)用。ontouchevent 的事件響應(yīng)邏輯如下:1. 如果事件傳遞到當(dāng)前view的onto
10、uchevent方法,而該方法返回了 fal se,那么這 個(gè)事件會(huì)從當(dāng)前view向上傳遞,并且都是由上層view的ontouchevent來(lái)接收,如果傳 遞到上面的ontouchevent也返回false,這個(gè)事件就會(huì)"消失”,而且接收不到下一次事 件。2. 如果返回了 true則會(huì)接收并消費(fèi)該事件。3如果返回super. ontouchevent(ev)默認(rèn)處理事件的邏輯和返回false時(shí)相同。到這里,與touch事件相關(guān)的三個(gè)方法就分析完畢了。但是大家可能對(duì)于什么是事件方法和攔截,可能還是有點(diǎn)難 理解。其實(shí)事件是先有分發(fā),再有攔截,最后才有處理!簡(jiǎn)單的理解:分發(fā)到你這個(gè)view
11、后,然后到你決定是否分發(fā)放到自身view是否去攔截,攔截下來(lái)就可以自己進(jìn)行處理。但是activity頁(yè)面或單個(gè)控件(比如button、textview) 是只有分發(fā)事件和處理事件的方法,而只用容器視圖(linearlayout或其他五大布局)才有攔截事件的方法。對(duì)于事件處理機(jī)制,分發(fā)方法和攔截方法是要理解的,事 件響應(yīng)方法可能是相對(duì)來(lái)說(shuō)容易理解一點(diǎn)的,但是事件響應(yīng) 方法是我們最最重要的方法,為啥,因?yàn)閼?yīng)用唄,因?yàn)槲覀?在頁(yè)面上的所有的點(diǎn)擊事件,觸摸事件的處理都是在這個(gè)事 件響應(yīng)方法里面來(lái)完成的,所以說(shuō)我們只要讓我們這個(gè)頁(yè)面 或view的事件響應(yīng)方法不能得到回調(diào),那么這個(gè)頁(yè)面或view 的點(diǎn)擊事
12、件或觸摸事件就不會(huì)發(fā)生作用了,這不就能達(dá)到我 們需要的效果了嗎?比如按鈕的點(diǎn)擊事件你知道是怎么發(fā)生的?這樣說(shuō)吧,按 鈕點(diǎn)擊之所以能觸發(fā)方法,是因?yàn)橄到y(tǒng)在事件響應(yīng)的屏幕觸 摸事件的手指抬起時(shí)做了回調(diào),如果你的事件響應(yīng)方法不能 正?;卣{(diào),那么你的按鈕是沒(méi)有任何作用的。你也可以寫(xiě)一 個(gè)程序試試,頁(yè)面就設(shè)計(jì)一個(gè)按鈕,你會(huì)發(fā)現(xiàn)當(dāng)你單擊按鈕 按下后沒(méi)抬起前按鈕是不會(huì)有方法執(zhí)行的,而你手指抬起 時(shí),監(jiān)聽(tīng)的方法才能得到執(zhí)行!所以android事件的處理都是圍繞能否讓事件響應(yīng)能夠 得到回調(diào)來(lái)進(jìn)行的,研究事件的攔截分發(fā)也是為了更好決定 是否讓事件響應(yīng)方法得到回調(diào)!下面的內(nèi)容會(huì)通過(guò)各種不同的的測(cè)試案例來(lái)驗(yàn)證上文中三
13、 個(gè)方法對(duì)事件的處理邏輯。onlntercepttouchevent,看名字便知道這是個(gè)攔截事 件。這個(gè)攔截事件需要分兩種情況來(lái)說(shuō)明:1. 假如我們?cè)谀硞€(gè) viewgroup 的 onlntercepttouchevent 中,將action為down的touch事件返回true,那便表示將 該viewgroup的所有下發(fā)操作攔截掉2. 假如我們?cè)谀硞€(gè) viewgroup 的 onlntercepttouchevent 中,將ac ion為down的touch事件都返回false,其他的都 返回true,這種情況下,down事件能正常分發(fā),若子view 都返回false3. onlnterce
14、pttouchevent 有兩個(gè)作用:1.攔截 down 事件的分發(fā)。2.中止up和move事件向目標(biāo)view傳遞,使得目標(biāo)view所在的viewgroup捕獲up和move事件。4. "觸摸事件由 action_down> action_move、aciton_up 組成,其中一次完整的觸摸事件中,down和up都只有一個(gè), move有若干個(gè),可以為0個(gè)?!?這里補(bǔ)充下其實(shí)up事件 是可能為0個(gè)的。假設(shè)場(chǎng)景:一個(gè)layout中包含一個(gè)button控件layout注冊(cè)了 ontouch()事件,button注冊(cè) onclick()事件,當(dāng)點(diǎn)擊這個(gè)button時(shí),是先觸發(fā)布局的o
15、ntouch()呢,述是先觸發(fā)按鈕的onclick ()事件呢?測(cè)試表明:布局先得到了這個(gè)事件。默認(rèn)情況下:它會(huì)把該事件傳遞給子控件一button;button如果處理了該事件(例如頁(yè)面跳 轉(zhuǎn)),那這次點(diǎn)擊事件就到此結(jié)束了;如果button不做處理(什么事都沒(méi)發(fā)生),事件會(huì) 返還給父控件-layout,由layout決定是否自己消費(fèi)該事件,如果父控件處理了,點(diǎn)擊事 件結(jié)束;如果父控件也沒(méi)有處理,事件丟棄,點(diǎn)擊事件結(jié)束。這是默認(rèn)情況下的流程,那特 殊情況下呢?特殊情況下:父控件會(huì)把事件攔截(通過(guò)onlntercepttouchevent ()方法的返冋值確定是否 攔截,返回true攔截;返回fa
16、lse分發(fā)給子控件),自己處理或丟棄,子控件根本就不知 道這次點(diǎn)擊的發(fā)生。簡(jiǎn)易流程如下:點(diǎn)擊事件發(fā)生后,父控件優(yōu)先獲取,如果父控件攔截了(處理或丟棄),子控件無(wú)法 獲取事件;如果父控件分發(fā)了,子控件可以處理或丟棄,如果是處理,整個(gè)事件結(jié)束;如果 是丟棄,事件又被拋給父控件,讓父控件處理或丟棄。同樣在開(kāi)始進(jìn)行案例分析z前,我需要說(shuō)明測(cè)試案例的結(jié)構(gòu),因?yàn)樗械臏y(cè)試都是針對(duì)這一個(gè)案例來(lái) 進(jìn)行的,測(cè)試中只是通過(guò)修改每個(gè)控件屮與touch事件相關(guān)的三個(gè)方法的返回值來(lái)體現(xiàn)不同的悄 況。先來(lái)看張圖:i面的圖為測(cè)試案例的布局文件ui顯示效果,布局文件代碼如下:<?xml version=h1.0n en
17、coding=nutf-8n?><cn.sunznteventtoucheventfather xmlns : android=,ht tp: /schemas android com/apk/res/android11android: layout_width=h f ill_parent11android:layout_height=n fill_parentnandroid: background=11 # 468ad7 "android:gravity="center”android: orientation="ve:rtical” >&
18、lt;cnsunzn.tevent.toucheventchilds android: id=n + id/childsf, android: layout_width=,f200dp android:layout_height=”200dp” android:layout_gravity=ncenterh android:background=n#e1110dn android:text = nstring/hellof, /></cnsunzn.tevent.toucheventfather>藍(lán)色背景為一個(gè)白定義控件toucheventfather,該控件為外層view,
19、繼承自linearlayout,實(shí)現(xiàn)代碼如下:package cn.sunzn.tevent;import android.content.context;import android.util.attributeset;importandroid.util.log;import android.view.motionevent;importandroid.widgetlinearlayout;public class toucheventfather extends linearlayout public toucheventfather(context context) super (co
20、ntext);public toucheventfather(context context, attributeset attrs) super(context, attrs);public boolean dispatchtouchevent(motionevent ev) log. e (11 sunzn11 f n toucheventfather | di spat chtouche vent > n + toucheventutil.gettouchaction(evgetaction();return super.dispatchtouchevent(ev);public
21、boolean onlntercepttouchevent(motionevent ev) log. i (11 sunzn11, 11 toucheventfather | onlntercepttouchevent -> n + toucheve nt util. gettouchact ion (ev< ge taction ();return super.onlntercepttouchevent (ev);public boolean ontouchevenonevent ev) log.d (nsunznnz f,toucheventeather | ontouchev
22、ent -> f, +toucheventutil.gettouchaction(ev.getaction();return super.ontouchevent(ev);紅色背景為一個(gè)自定義控件toucheventchilds,該控件為內(nèi)層view,為toucheventfather的子view,同樣繼承口 linearlayout,實(shí)現(xiàn)代碼如下:package cn.sunzn.tevent;import androids content.context;import androidutilattributeset;importandroid.util.log;import andr
23、oid.view.motionevent;importandroid.widget.linearlayout;public class toucheventchilds extends linearlayout public toucheventchilds(context context) super(context);public toucheventchilds(context context, attributeset attrs) super(context, attrs);public boolean dispatchtouchevent(motionevent ev) log.e
24、(nsunznnz ntoucheventchilds i dispatchtouchevent - n +toucheventutilgettouchaction(evgetaction();return super.dispatchtouchevent(ev);public boolean onlntercepttouchevent(motionevent ev) log. i (f,sunznn, ntoucheventchilds i onlnteircepttouchevent -一 u +toucheventutil.gettouchaction(evgetaction();ret
25、urn super.onlntercepttouchevent (ev);public boolean ontouchevent(motionevent ev) log d ( "sunzn”, 11 toucheventchilds | ontouchevent 一一 n +toucheventutil.gettouchaction(evgetaction();return super.ontouchevent(ev);接著實(shí)現(xiàn)activity的代碼,因?yàn)榭丶械氖录际峭ㄟ^(guò)activity的dispatchtoiuchevent進(jìn)行分 發(fā)的;除此z外還需要重寫(xiě)activity的
26、ontouchevent方法,這是因?yàn)槿绻粋€(gè)控件直接從activity 獲取到事件,這個(gè)事件會(huì)首先被傳遞到控件的dispatchtouchevent方法,如果這個(gè)方法return false,事件會(huì)以冒泡方式返回給activity的ontouchevent進(jìn)行消費(fèi)。實(shí)現(xiàn)代碼如下:package cnsunzntevent;import android.app.activity;import androidosbundle;import android.utillog;import android.view.motionevent;public class toucheventactivity
27、 extends activity public void oncreate(bundle savedlnstancestate) superoncreate(savedlnstancestate);setcontentview(rlayout.mein);public boolean dispatchtouchevent(motionevent ev) log w (f,sunznn, ntoucheventactivity i dispatchtouchevent > toucheventutil.gettouchaction(evgetaction();return super.d
28、ispatchtouchevent(ev);public boolean ontouchevent(motionevent event) log. w (f,sunznn, 11 toucheventactivity i ontouchevent > n + toucheventutil.gettouchaction(event.getaction();return super.ontouchevent(event);最后再附上toucheventutil的代碼,toucheventutil中并沒(méi)有做多少事tff,只足將以上2個(gè)自定義控件屮各個(gè)方法的motionevent集屮到一個(gè)工具類
29、屮并將其對(duì)應(yīng)的動(dòng)作以string形式返回,這樣處理更便于實(shí)時(shí)觀察控件的事件。代碼如下:package cnsunzntevent;import android.view.motionevent;public class toucheventutil public static string gettouchactioactionld) string actionneme = uunknow:id=n + actionld;switch (actionld) case motionevent action_down:actionname = n action_down n;break;case
30、motionevent.action_move:actionname = naction_move,f;break;case motionevent action_up:actionname = naction_upn;break;case motionevent action_cancel:actionname = naction_canceln;break;case motionevent.action_outside:actionname = naction_outsiden;break;return actionname;三、touch案例分析case 1攔截條件運(yùn)行結(jié)果level t
31、imepidapplicatio ntagtext結(jié)果分析代碼運(yùn)行后,爭(zhēng)件首先由touch event activity的dispatchtouchevent方法分發(fā)給toucheventfather控卑 冋f(shuō)alse,表示対獲収到的事件停止向下傳遞,同時(shí)也不対該事件進(jìn)行消費(fèi)。由于toucheventfather ? 給toucheventactivity的ontouchevent進(jìn)行消費(fèi),最后直接由toucheventactivity來(lái)響應(yīng)手指移動(dòng)科case 2攔截條件toucheve ntfathertruesuper, onln tercepttoucheve nt(ev)運(yùn)行結(jié)果lev
32、eltimepidapplicatio ntag text結(jié)果分析代碼運(yùn)行后,事件首先由 touch event activity 的 dispatchtouchevent 方法分發(fā)給 toucheventfather 控件的 dispai 冋true,表示分發(fā)事件到toucheventfather控件并由該控件的dispatchtouchevent進(jìn)行消費(fèi);toucheventac 的dispatchtouchevent,而toucheventfather控件的dispatchtouchevent也不斷的將獲取到的事件進(jìn)行消費(fèi)。攔截條件控件名稱dispatchtouchevent 返回值on
33、lntercepttouchevent 返冋值onttoucheventactivitysuper.dispatchtouchevent(ev)suptoucheve ntfathersuper.dispatchtoucheve nt(ev)truesuptouchev en tchildssuper.dispatchtoucheve nt(ev)super, onln tercepttoucheve nt(ev)sup運(yùn)行結(jié)果leveltimepidapplicati ontagtextw05-10 05:34:46.333481cn.s un zn.teve ntsunz ntouchev
34、entactivity | dispatchtouchevent -> actlohe05-10 05:34:46.333481cn.s un zn.teve ntsunz ntoucheventfather | dispatchtouchevent -> action105-10 05:34:46.333481cn.sun zn.teve ntsunzntoucheventfather | onlntercepttouchevent-> actkd05-10 05:34:46.333481cn.sunzn .teve ntsunz ntoucheventfather | o
35、ntouchevent acti0n_d0ww05-10 05:34:46.333481cn.s un zn.teve ntsunzntoucheventactivity | ontoucheventaction_dovw05-10 05:34:46.343481cn.sun zn.teve ntsunzntoucheventactivity | dispatchtouchevent -> actiow05-10 05:34:46.343481cn.sun zn.teve ntsunzntoucheventactivity | ontouchevent -> action_mo
36、39;w05-10 05:34:46.423481cn.s un zn.teve ntsunzntoucheventactivity | dispatchtouchevent > actlohw05-10 05:34:46.423481cn.sun zn.teve ntsunzntoucheventactivity | ontouchevent -> action_mo'w05-10 05:34:46.433481cn.s un zn.teve ntsunz ntoucheventactivity | dispatchtouchevent -> actlohw05-1
37、0 05:34:46.433481cn.s un zn.teve ntsunz ntoucheventactivity | ontouchevent -> action_moiw05-10 05:34:46.442481cn.sun zn.teve ntsunzntoucheventactivity | dispatchtouchevent -> actlohw05-10 05:34:46.442481cn.sunzn .teve ntsunz ntoucheventactivity | ontouchevent -> action_up結(jié)果分析代碼運(yùn)彳亍丿匸,事件首先由 t
38、oucheventactivity 的 dispatchtouchevent 方法分發(fā)給 toucheventfather 控件的 dispatchtouchevent, if 返冋super.dispatchtouchevent(ev),表示對(duì)事件進(jìn)行分發(fā)并向下傳遞給toucheventfather控件的onlntercepttouchevent方法,該/ 的事件進(jìn)行攔截并將事件傳遞給toucheventfather控件的ontouchevent進(jìn)行處理,toucheventfather控件的ontouchevent返匸 對(duì)事件沒(méi)冇做任何處理直接將事件返冋給上級(jí)控件,由于touchevent
39、father獲取的事件直接來(lái)自toucheventactivity, ontouchevent會(huì)將事件以冒泡方式直接返回給toucheventactivity的ontouchevent進(jìn)行消費(fèi),后續(xù)的事件則會(huì)跳: toucheventactivity 的 ontouchevent 消費(fèi)來(lái)自 toucheventactivity 自身分發(fā)的事件。case 3case 4攔截條件控件名稱dispatchtouchevent 返回值o n 1 n t e rce ptto u c h eve nt 返回值toucheventactivitysuper.dispatchtoucheve nt(ev)t
40、oucheve ntfathersuper.dispatchtoucheve nt(ev)falsetoucheve ntchildssuper.dispatchtoucheve nt(ev)super, onln tercepttoucheve nt(ev)運(yùn)行結(jié)果leveltimepidapplicatio ntagtextw05-10 06:31:47.565512cn.s unzn .teve ntsunzntoucheventactivity | dispatchtoe05-10 06:31:47.565512cn.s un zn.teve ntsunzntoucheventfath
41、er | dispatchtoi105-10 06:31:47.565512cn.sunzn .teve ntsunzntoucheventfather |onln tercept'e05-10 06:31:47.565512cn.sunzn.teve ntsunz ntoucheventchilds | dispatchtouc105-10 06:31:47.565512cn.sunzn .teve ntsunzntoucheventchilds | onlnterceptld05-10 06:31:47.565512cn.sun zn.teve ntsunzntoucheventc
42、hilds | ontoucheverd05-10 06:31:47.565512cn.sunzn .teve ntsunzntoucheventfather |on touchevew05-10 06:31:47.565512cn.s un zn.teventsunz ntoucheventactivity | ontoucheviw05-10 0&31:47.652512cn.sun zn.teve ntsunzntoucheventactivity | dispatchtoiw05-10 06:31:47.652512cn .sunz n.tev entsunzntoucheve
43、ntactivity | ontouchevtw05-10 06:31:47.732512cn.s unzn .teve ntsunzntoucheventactivity | dispatchtoiw05-10 06:31:47.732512cn.sunzn .teve ntsunzntoucheventactivity | ontouchevew05-10 06:31:47.812512cn .sunz n.tev entsunzntoucheventactivity | dispatchtoiw05-10 06:31:47.812512cn.sunzn .teve ntsunzntouc
44、heventactivity | ontoucheviw05-10 06:31:47.892512cn.sunzn.teventsunzntoucheventactivity| dispatchtoiw05-10 06:31:47.892512cn.sunzn.teventsunzntoucheventactivity| ontouchevi結(jié)果分析代碼運(yùn)行后,事件首先由toucheventactivity的dispatchtouchevent方法分發(fā)給toucheventfather控件的dis| 返回super.dispatchtouchevent(ev),表示對(duì)事件進(jìn)行分發(fā)并向下傳遞給t
45、oucheventfather控件的onlntercepttc 放行并傳遞到子控件 toucheventchilds 的 dispatchtouchevent 方法,同樣 toucheventchilds 的 dispatchtouchever 進(jìn)行分發(fā)并向卜傳遞給 toucheventchilds 控件的 onlntercepttouchevent 方法toucheventchilds 的 onlntercepttou 默認(rèn)會(huì)將事件傳遞給 toucheventchilds 的 ontouchevent 進(jìn)行處理,toucheventchilds 的 ontouchevent 返回 si 直接
46、將事件返回給上級(jí)控件,由于toucheventchilds獲取的事件直接來(lái)自toucheventfather,所以touchevent 直接返回給 toucheventfather 的 ontouchevent 進(jìn)行消費(fèi),而 toucheventfather 的 ontouchevent 也返冋了 si ontouchevent也會(huì)將半件返回給上級(jí)控件,而toucheventfather獲取的事件直接來(lái)自toucheventactivity,所0 件以冒泡方式直接返冋給toucheventactivity的ontouchevent進(jìn)行消費(fèi),后續(xù)的事件則會(huì)跳過(guò)toucheventfath 的on
47、touchevent消費(fèi)來(lái)自toucheventactivity自身分發(fā)的事件。case 5攔截條件控件名稱dispatchtoucheve nt 返回值onlntercepttouchevent 返冋值toucheve ntactivitysuper.dispatchtouche ven t(ev) toucheve ntfathersuper.dispatchtouche ven t(ev)falsetoucheve ntchildstruesuper, onln tercepttoucheve nt(ev)運(yùn)行結(jié)果leveltimepidapplicati ontagtextw05-10
48、 08:11:18.403574cn .sunz n.teve ntsunzntoucheventactivity | dispatchtce05-10 08:11:18.403574cn. sunzn .teve ntsunzntoucheventfather | dispatchtoi105-10 0& 11:18.403574cn.sunzn .teve ntsunzntoucheventfather | onintercep-e05-10 08:11:18.403574cn .sunz n.teve ntsunzntoucheventchilds | dispatchtoiw0
49、5-10 08:11:19.652574cn .sunz n.teve ntsunzntoucheventactivity | dispatchtce05-10 08:11:19.652574cn.sunzn .teve ntsunzntoucheventfather | dispatchtoi105-10 08:11:19.652574cn.sunzn .teve ntsunzntoucheventfather | onlntercepe05-10 08:11:19.652574cn.sunzn .teve ntsunzntoucheventchilds | dispatchtoiw05-1
50、0 0& 11:19.872574cn.sunzn .teve ntsunzntoucheventactivity | dispatchtce05-10 0& 11:19.872574cn.sunzn .teve ntsunzntoucheventfather | dispatchtoi105-10 08:11:19.872574cn.sunzn .teve ntsunzntoucheventfather | onlntercep-e05-10 0& 11:19.872574cn.sunzn .teve ntsunzntoucheventchilds | dispatc
51、htoi結(jié)果分析代碼運(yùn)彳亍后,事件首先rtl toucheventactivity 的 dispatchtouchevent 方法分發(fā)給 toucheventfather 控件的 disp 回 super.dispatchtouchevent(ev),事件會(huì)分發(fā)到 toucheventfather 的 onlntercepttouchevent, onintercepttouche 遞到子控件 toucheventchilds 的 dispatchtouchevent 方法,dispatchtouchevent 返冋 true 表示事件被 dispatchtouchevent方法消費(fèi)。后續(xù)的事
52、件也會(huì)不斷的重復(fù)上血的邏輯最終被toucheventchilds的dispatchtou<touch事件分發(fā)中只有兩個(gè)主角:viewgroup和view。activity的touch事件事實(shí)上是調(diào)用它內(nèi)部的viewgroup的touch事件z可以直接當(dāng)成viewgroup處理。view在viewgroup內(nèi),viewgroup也可以在其他viewgroup內(nèi),這時(shí)候把內(nèi)部的viewgroup當(dāng)成view來(lái)分析。viewgroup 的相關(guān)事件有三個(gè):onlntercepttouchevent. dispatchtouchevent.ontouchevento view 的相關(guān)事件只有兩個(gè):
53、dispatchtouchevent. ontouchevento先分析viewgroup的處理流程:首先得有個(gè)結(jié)構(gòu)模型概念:viewgroup和view組成了一棵樹(shù)開(kāi)彳結(jié)構(gòu),最頂層為activity的viewgroup ,下面有若干的viewgroup節(jié)點(diǎn),每個(gè)節(jié)點(diǎn)之下又有若干的viewgroup節(jié)點(diǎn)或者view節(jié)點(diǎn),依次類推。如圖:inview: decoi view 百象vwgroup 1i lviewgrouph白h i當(dāng)一個(gè)touch事件(觸摸事件為例倒達(dá)根節(jié)點(diǎn),即acitivty的viewgroup時(shí),它會(huì)依 次下發(fā),下發(fā)的過(guò)程是調(diào)用子view(viewgroup)的dispatc
54、htouchevent方法實(shí)現(xiàn)的。 簡(jiǎn)單來(lái)說(shuō),就是viewgroup遍歷它包含著的子view ,調(diào)用每個(gè)view的 dispatchtouchevent 方法,而當(dāng)子 view 為 viewgroup 時(shí),又會(huì)通過(guò)調(diào)用 viwgroup 的dispatchtouchevent方法繼續(xù)調(diào)用其內(nèi)部的view的dispatchtouchevent方法。 上述例子中的消息下發(fā)順序是這樣的:-。dispatchtouchevent 方法只負(fù)責(zé)事件的分發(fā),它擁有boolean類型的返回值,當(dāng)返回為true時(shí),順序下發(fā) 會(huì)中斷。在上述例子中如果的dispatchtouchevent返回結(jié)果為true ,那么
55、- 將都接收不到本次touch事件。來(lái)個(gè)簡(jiǎn)單版的代碼加深理解:viewgroupqparam ev* returnpublic boolean dispatchtouchevent(motionevent ev)./其他處理,在此不管view views=getchildview();for (int i = 0;i<views.length;i+) /判斷下touch到屏幕上的點(diǎn)在該子view上面if (. . .) if(viewsi.dispatchtouchevent(ev)return true;./其他處理,在此不管 / view* gparam ev* * view* gparam ev* return*/public boolean dispatchtouchevent(motionevent ev)./其他處理,在此不管return false;在此可以看出r viewgroup的dispatchtouchevent是真正在執(zhí)行分發(fā)工作,而 view的dispatchtouchevent方法,并不執(zhí)行分發(fā)工作,或者說(shuō)它分發(fā)的對(duì)象就是自 己,決定是否把
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年中國(guó)裹腳敷墊數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 2025至2030年中國(guó)梳子頭花數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 2025至2030年中國(guó)扭捻角測(cè)定儀數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 2025至2030年中國(guó)家用集線器數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 2025年中國(guó)鋅片市場(chǎng)調(diào)查研究報(bào)告
- 基于棉漿黑液提取物制備聚合物材料
- 二零二五年度毛竹山承包經(jīng)營(yíng)與竹林碳匯交易合同3篇
- 專用工程車輛租賃合同書(shū)2024版
- 二零二五年度地下車庫(kù)車位劃線與通風(fēng)照明改造合同4篇
- 二零二五年度活動(dòng)板房安裝與智能化家居系統(tǒng)合同3篇
- 蛋糕店服務(wù)員勞動(dòng)合同
- 土地買(mǎi)賣合同參考模板
- 2025高考數(shù)學(xué)二輪復(fù)習(xí)-專題一-微專題10-同構(gòu)函數(shù)問(wèn)題-專項(xiàng)訓(xùn)練【含答案】
- 新能源行業(yè)市場(chǎng)分析報(bào)告
- 2025年天津市政建設(shè)集團(tuán)招聘筆試參考題庫(kù)含答案解析
- 巖土工程勘察.課件
- 60歲以上務(wù)工免責(zé)協(xié)議書(shū)
- 康復(fù)醫(yī)院患者隱私保護(hù)管理制度
- 2022年7月2日江蘇事業(yè)單位統(tǒng)考《綜合知識(shí)和能力素質(zhì)》(管理崗)
- 沈陽(yáng)理工大學(xué)《數(shù)》2022-2023學(xué)年第一學(xué)期期末試卷
- 初一英語(yǔ)語(yǔ)法練習(xí)
評(píng)論
0/150
提交評(píng)論