DispatchTouchEvent & OnInterceptTouchEvent & onTouchEvent的含義和區別
來源:程序員人生 發布時間:2015-03-25 11:52:06 閱讀次數:3968次
1.DispatchTouchEvent
Pass the touch screen motion event down to the target view, or this view if it is the target.
傳遞觸摸事件到目標視圖,或他就是目標視圖。
情況1:return true
則事件只會在當前方法內消耗掉,且不會向下傳遞,最后傳給Activity.
|
.png) |
情況2:return false
則事件只會在當前方法內消耗且不會向下傳遞,最后傳給Activity。
但是不同于情況1,呈U型傳遞的進程不會傳到當前的布局
|
.png) |
情況3:super.dispatchTouchEvent(MotionEvent)
全部進程會經歷當前ViewGroup的3個觸摸事件,dispatchTouch -> interceptTouch -> onTouchEvent。
|
.png) |
2.OnInterceptTouchEvent
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.
實現這個方法是為了監測所有的屏幕觸摸事件。這個允許你視察 事件分發給你的子視圖,并且具有當前任何點的手勢的權限。
Using this function takes some care, as it has a fairly complicated interaction with 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:
使用這個方法需要注點意, 由于它和OnTouchEvent有著非常復雜的交互,在使用它的時候1樣需要實現這個方法并且正確使用。事件將會依照以下順序接收:
- You will receive the down event here.
按下的事件會在這里接收。
- 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.
按下事件將會被這視圖里面的子視圖處理,否則就被分發onTouchEvent 方法去處理 ; 這就意味著你應當實現 onTouchEvent 返回true, 所以你將會繼續看到剩下的手勢。而且,通過從onTouchEvent方法返回true,你不會在onInterceptTouchEvent方法中接收任何事件并且所有的觸摸處理1定會在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,每一個緊隨著的事件將會 傳遞到這里然后分發到目標的的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 MotionEvent.ACTION_CANCEL, and all further events will be delivered to your onTouchEvent()
method and no longer appear here.
如果你返回true,你就不會接收到以下事件:目標視圖將接收到一樣的事件而且只有MotionEvent.Action_Cancel,而且所有未來的事件將會傳遞到你的onTouchEvent 方法,并且不在會出現在這里。
情況1:return true
事件傳遞將依照dispatch-> intercept-> onTouch進行傳遞,終究返回到Activity。 |
.png) |
情況2:return false
正常傳遞
|
.png) |
情況3: super.onInterceptTouchEvent(ev);
正常傳遞
|
.png) |
3.OnTouchEvent
Implement this method to handle touch screen motion events.
不管觸摸事件是不是被處理,true || false 事件都會向下進行傳遞。
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈