The Android event distribution and transmission mechanism of the image talking series,
In some complex la S, we often encounter problems such as event conflicts and event invalidation. This requires us to have a deep understanding of the distribution and transfer mechanism of Android events. The best way is to write a demo by yourself, print the Event-related logs to view the running process, and then deeply understand it with the source code. Of course, this will not be done here. There are only summative things here. If you like this kind of things, please continue to look at it.
Old rules: first:
User interaction usually occurs on the touch screen. Therefore, the transmission of Android events must involve ViewGroup and View. The methods for passing event processing in ViewGroup and View are as follows:
ViewGroup:
<span style="font-size:14px;">public boolean dispatchTouchEvent(MotionEvent event)public boolean onTouchEvent(MotionEvent event) public boolean onInterceptTouchEvent(MotionEvent event)</span>
View:
<span style="font-size:14px;">public boolean dispatchTouchEvent(MotionEvent event)public boolean onTouchEvent(MotionEvent event) </span>
DispatchTouchEvent is responsible for event distribution, onTouchEvent is responsible for event processing, and onInterceptTouchEvent is responsible for event interception. You can see that only ViewGroup has the onInterceptTouchEvent method.
After learning about the basic event-related methods, let's look at some key points:
1. Upload sequence of Android events: Activity ---> ViewGroup ---> View.
2. If true is returned in dispatchTouchEvent, the distribution will not continue, and the event will be canceled. If false is returned, the event will continue to be passed.
3. If the onInterceptTouchEvent of ViewGroup returns true, the event is intercepted, so the event is not passed down to the View and handed over to the ViewGroup itself for processing. If false is returned, the event is still transmitted to the View.
4. If onTouchEvent returns true, it indicates that it consumes the event, but does not respond to onClickListener. If it returns false, it will continue to respond to onClickListener, if it has been set.
5. For more information, see the description in.