Android 事件分發

2021-07-17 04:40:38 字數 957 閱讀 2437

touch 事件的分發和消費機制dispatchtouchevent()、onintercepttouchevent()和ontouchevent()

dispatchtouchevent 事件分發

true 事件會分發給當前view 並由dispatchtouchevent 方法消費,同時停止向下傳

false 事件分發為兩種情況

1.如果view 獲取的事件直接來自activity,這會返回activity的ontouchevent進行消費

2.如果view獲取的事件來自外層父控制項 這回將時間返回給父view的ontouchevent進行消費

super.dispatchtouchevent(ev) 事件會分發給當前的view的onintercepttouchevent方法

onintercepttouchevent(ev) 事件攔截

true 對事件進行攔截 並將攔截的事件交給當前view的ontouchevent 進行處理

false 將事件方行 傳遞到子view上,在傳遞到子view的dispatchtouchevent

再進行事件的分發

super.onintercepttouchevent(ev)事件預設攔截 並將攔截到的事件交給當前view的ontouchevent進行處理 (和返回ture一樣)

ontouchevent 事件響應

true 事件攔截 自己處理

false 放棄處理 ,返回到上級,並由上層view的ontouchevent 來接受

super.ontouchevent(ev)預設處理事件的邏輯和返回false的一樣

在有註冊ontouchlistener的情況下會先呼叫

ontouch

true 這事件不在向下層傳遞

false 像ontouchevent傳遞

然後 ontouchevent

然後onclick

Android事件分發

android 中與 touch 事件相關的方法包括 dispatchtouchevent motionevent ev onintercepttouchevent motionevent ev ontouchevent motionevent ev 能夠響應這些方法的控制項包括 viewgroup...

Android事件分發

當使用者觸控螢幕時,系統會對觸控事件做出相應的相應,這個事件會產生乙個motionevent,系統根據一定的規則將其傳遞給view進行處理,這個過程就是事件分發機制了。事件的傳遞分為兩個階段,即捕獲階段和冒泡階段。捕獲階段 事件最先由最外層的view接收,然後依次向內層傳遞,直到傳遞到最小的view...

Android 事件分發

案例說明 linearlayout 下面有3個recyclerview 當手指在螢幕的中間上半屏滑動的時候,3個recyclerview會一起滑動 當手指在螢幕的下半屏滑動的餓時候,各自區域的recyclerview單獨滑動 先看下 效果圖 需要重寫linearlayout public class...