ViewGroup的onInterceptTouchEvent()方法

public boolean onInterceptTouchEvent(MotionEvent ev) {

return false;//繼續(xù)向下傳播挣菲,為true的時候就直接發(fā)送給當前viewGroup的onTouchEvent()方法處理了且不繼續(xù)向下傳播

}

Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.可以通過實現(xiàn)這個方法去攔截所有的touch事件乎完。這個可以讓viewgroup監(jiān)視分發(fā)給它的子view的事件,可以在任何時候取得當前手勢事件的控制權(quán)佑菩。

Using this function takes some care, as it has a fairly complicated interaction with {@link View#onTouchEvent(MotionEvent) View.onTouchEvent(MotionEvent)}, and using it requires implementing that method as well as this one in the correct way. Events will be received in the following order:

You will receive the down event here. 首先离唐,你會在這里接受到down事件背伴。

The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.down事件可能會被這個viewGroup的一個子孩子處理掉错忱,或者給自己的onTouchEvent()事件處理了。這意味著你需要實現(xiàn)onTouchEvent()方法然后返回true,然后你就會繼續(xù)接收到剩下的手勢事件(而不是繼續(xù)向上找一個父View去處理這個事件)挂据。同時以清,onTouchEvent()方法返回true的時候,viewGroup的onInterceptTouchEvent()方法就不會再接收到陸續(xù)的方法事件崎逃,所有的touch事件會正常的直接給onTouchEvent()方法掷倔。

For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().如果這個方法返回false.接下來的后續(xù)事件都會先傳遞到這里,然后再傳遞給處理這個事件的目標view的onTouchEvent个绍。

If you return true from here, you will not receive any following events: the target view will receive the same event but with the action {@link MotionEvent#ACTION_CANCEL}, and all further events will be delivered to your onTouchEvent() method and no longer appear here.如果你這里返回true,你不會再接收到后續(xù)的事件:目標view會接受到相同的事件產(chǎn)生的ACTION_CANCEL勒葱。浪汪。其他的事件也會直接傳遞到你的onTouchEvent,而再也不會在onInterceptTouchEvent()方法里捕捉到。

@param ev The motion event being dispatched down the hierarchy. @return Return true to steal motion events from the children and have them dispatched to this ViewGroup through onTouchEvent(). The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here. 動作事件會按層級分發(fā)凛虽。如果返回true就會從他的子孩子搶占過來這個事件且將這個事件分發(fā)給當前ViewGroup的onTouchEvent()事件處理死遭。當前的目標View會接受到ACTION_CANCEL事件,且不會再接受到后續(xù)的事件了凯旋。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末呀潭,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子至非,更是在濱河造成了極大的恐慌钠署,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件荒椭,死亡現(xiàn)場離奇詭異谐鼎,居然都是意外死亡,警方通過查閱死者的電腦和手機趣惠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進店門狸棍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人味悄,你說我怎么就攤上這事草戈。” “怎么了傍菇?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長界赔。 經(jīng)常有香客問我丢习,道長,這世上最難降的妖魔是什么淮悼? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任咐低,我火速辦了婚禮,結(jié)果婚禮上袜腥,老公的妹妹穿的比我還像新娘见擦。我一直安慰自己,他們只是感情好羹令,可當我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布鲤屡。 她就那樣靜靜地躺著,像睡著了一般福侈。 火紅的嫁衣襯著肌膚如雪酒来。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天肪凛,我揣著相機與錄音堰汉,去河邊找鬼辽社。 笑死,一個胖子當著我的面吹牛翘鸭,可吹牛的內(nèi)容都是我干的滴铅。 我是一名探鬼主播,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼就乓,長吁一口氣:“原來是場噩夢啊……” “哼汉匙!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起档址,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤盹兢,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后守伸,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體绎秒,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年尼摹,在試婚紗的時候發(fā)現(xiàn)自己被綠了见芹。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡蠢涝,死狀恐怖玄呛,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情和二,我是刑警寧澤徘铝,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站惯吕,受9級特大地震影響惕它,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜废登,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一淹魄、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧堡距,春花似錦甲锡、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至易稠,卻和暖如春疚俱,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背缩多。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工呆奕, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留养晋,地道東北人。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓梁钾,卻偏偏與公主長得像绳泉,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子姆泻,可洞房花燭夜當晚...
    茶點故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容