DispatchTouchEvent & OnInterceptTouchEvent & onTouchEvent的含義和區別

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.
情況2:return false  

則事件只會在當前方法內消耗不會向下傳遞,最後傳給Activity。
但是不同於情況1,呈U型傳遞的過程不會傳到當前的佈局

情況3:super.dispatchTouchEvent(MotionEvent)

 整個過程會經歷當前ViewGroup的三個觸摸事件,dispatchTouch -> interceptTouch -> onTouchEvent。



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.
    按下的事件會在這裏接收。

  2. 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方法中接收任何事件並且所有的觸摸處理一定會在onTouchEvent中正常發生。

  3. 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().

  4. 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。
情況2:return false
正常傳遞
情況3: super.onInterceptTouchEvent(ev);

正常傳遞






3.OnTouchEvent

     Implement this method to handle touch screen motion events.
     無論觸摸事件是否被處理,true || false 事件都會向下進行傳遞。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章