View event distribution mechanism, view event Distribution
The so-called event distribution is actually the distribution process of MotionEvent events, that is, when a MotionEvent is generated, the system needs to pass the event to a specific View, the transfer process is the distribution process.
The following three methods are used to distribute click events:
Public boolean dispatchTouchEvent (MotionEvent event)
This method is used for event distribution. If an event can be passed to the current View, this method will be called. The returned results will be affected by the onTouchEvent of the View and the dispatchTouchEvent method of the lower level, indicating whether to consume the current event.
Public boolean onInterceptTouchEvent (MotionEvent ev)
The internal call of the preceding method is used to determine whether to intercept an event. If the current View intercepts an event, this method will not be called again in the same event sequence, the returned result indicates whether to intercept the current event.
Public boolean onTouchEvent (MotionEvent event)
The dispatchTouchEvent method is called to process click events and whether the returned results consume the current event. If no event is consumed, the current View cannot receive the event again in the same event sequence.
For a root ViewGroup:
After an event is generated
Pass it first, then dispatchTouchEvent will call the onInterceptTouchEvent of ViewGroup
----- The onInterceptTouchEvent of ViewGroup returns true, which indicates that the current event is intercepted, and the event is handed over to ViewGroup for processing. That is, onTouchEvent will be called.
----- The onInterceptTouchEvent of ViewGroup returns false, indicating that the event is not intercepted. This event is passed to the child element, and then the dispatchTouchEvent of the child element is called.
Event transfer: activity> Window> view
Event transfer conclusion:
1. the same event sequence starts when the finger contacts the screen and ends when the finger leaves the screen.
2. Under normal circumstances, only one View can intercept an event sequence and consume it
3. Once a View is intercepted, the event sequence can only be handled by it. Its onInterceptTouchEvent will not be called.4. Once a View starts, if it does not consume the ACTION_DOWN event, that is, the onTouchEvent returns false, other events in the same event sequence will not be handed over to it for processing.
5. if you do not consume events other than ACTION_DOWN, The Click Event disappears. At this time, the onTouchEvent of the parent element is not called, and the current View continues to receive subsequent events, finally, these disappearing events are passed to the Activity for processing.
6. ViewGroup does not intercept the event onInterceptTouchEvent by default, and fakse is returned.
7. The View does not have onInterceptTouchEvent. Once the event is passed to the View, the onTouchEvent will be called.
8. The onTouchEvent of the View consumes events by default and returns true, unless it cannot click clickable and longClickable at the same time is false; The longClickable of the View is not false by default, and the clickable should be divided into different situations;
9. The enable attribute of View does not affect the default value of onTouchEvent.
10. onClick occurs only when the current View is clickable and receives down and up events.
11. The event transfer process is from external to internal. Events are always first transmitted to the parent element and then distributed to the child element by the parent element.