2014年6月22日 星期日

Android touch event system

Overview

Touch event start from top to bottom (each view can decide whether to intercept event), then back up from bottom to top until some view consumed it!

Overview Flow

Activity.dispatchTouchEvent -> Root View. dispatchTouchEvent
                                                       -> .... -> bottom view.dispatchTouchEvent
   (back up) -> ... -> Root View.onTouchEvent -> Activity.onTouchEvent


Detail flow inside UI view

ViewGroup.dispatchTouchEvent()

  • onInterceptTouchEvent()
    • Check if it should supersede children
    • Return true once consumes all subsequent events
  • For each child view, in reverse order they were added
    • If touch is relevant (inside view), child.dispatchTouchEvent()
    • If not handled by previous, dispatch to next view
  • If no children handle event, listener gets a chance
    • OnTouchListener.onTouch()
  • If no listener, or not handled by child
    • OnTouchEvent

Intercept touch event


  • Override ViewGropu.onInterceptHoverEvent and return true
  • After return true, all subsequent events for the current gesture will come to your onTouchEvent() directly
  • onInterceptHoverEvent  will not be called for input event of current gesture  
  • Current target view will receive ACTION_CANCEL
  • Intercept cannot be reversed until the next gesture

Misc

  • use TouchDelegate if you want to touch area different from view bounding

Reference