Android: event distribution mechanism

distribution map

Click events are divided into four parts: down, move, up, cancel, the first three are what we need to pay attention to
Events are divided into four layers: Activity, ViewGroup, View
The methods involved are divided into three: dispatchTouchEvent, onInterceptTouchEvent, onTouchEvent
insert image description here

Distribution law

  • Activty distributes events down through return super.dispatchTouchEvent(), and will consume events regardless of return false/true
  • The ViewGroup will receive the event from the Activity, and check whether it is passed down through the return value of onInterceptTouchEvent. If the return value is true, it will call the onTouch of the VewGroup, and the event will not be passed down.
  • Passing needs to return false or super.dispatchTouchEvent
  • If onTouch returns false to indicate that the event has not been consumed, then go to onTouchEvent, which will trigger the click listener
  • If onInterceptTouchEvent returns false, it will also be passed to the child view
  • After the child view receives the event, it can call the onTouchEvent of the view itself through super.dispatchTouchEvent, or return false to call the onTouchEvent of the ViewGroup
  • The move and up events will be passed to the down intercepted component, and the onTouch event of the corresponding annotation or the parent view will be called

Guess you like

Origin blog.csdn.net/weixin_51109304/article/details/131152635