Start with the delivery of the event first.
When an event arrives at the interface, its entrance is dispatchtouchevent. This method is the only interface for the view handling event, and all events that arrive at the view must go through this method.
Simply put, the system received an event, to throw into a linearlayout inside, how to do?
Direct tune this dispatchtouchevent, receive the return TRUE or false, is finished. The back of the deal with this linearlayout does not matter.
Then someone asked, that onintercepttouchevent, Ontouchevent, Onclicklistenr, or this linearlayout inside the button, not yet to deal with it, how is it finished?
Although external calls only dispatchtouchevent, within this method it itself calls these methods according to a series of logic.
The order in which the pieces are passed between views is probably this:
The dispatchtouchevent--> of the parent container calls the dispatchtouchevent--> of the inner container to invoke the dispatchtouchevent of the base control.
The container here is ViewGroup, the control is a variety of view.
With this passing, a complex component becomes unified for its upper layers: I don't care what's in your mess. Basic controls, how to place them, and how to handle them. I divide the minute hundreds of thousands of up and down, find a small specific button I tired not tired? Anyway, I'll leave it to you and decide what to do with you.
Also his subordinates are the idea of throwing events to the next level, eventually conveying a button to let it respond to processing.
This is the chain of responsibility model. But this creates a problem: if the parent container can only act as a pass-through, the event can only be responded to by a child control, how do I swipe scrollview? Viewpager How to switch? Work can not be pressure to the bottom of the staff, there are always some things my manager has to do it, such as report writing work plan bubble front desk flirt test and so on. The cough is far away.
So there needs to be a way of judging it. An incident came, I myself review, this is my duty, then the following things I will take down, not down. Otherwise, continue to preach. Hey, I'm so witty.
The way to judge this is onintercepttouchevent.
In this case, the concept of "action" and "event" should be distinguished.
An action is a complete operation of the user, such as a click, a swipe, and so on.
And the event is motionevent.
We know that all events motionevent are transient, and an event itself represents just what this moment looks like. You cannot see from a single motionevent whether the user is currently sliding or pressing, how far it has slipped, left or right. A complete sliding action is made up of constantly triggering action_down, Action_move x N, Action_up (or cancel). These action_down, Action_move, represent the type of event (Getaction ()).
Then the problem comes again. A separate transient action_move, how do I know if this event was down 2 seconds ago or 5 seconds ago with that down? How can I tell if a complete action has been done?
The view is through the Getdowntime () property. The sequence of events for each action has the same downtime, that is, the time the geteventtime of the Down event returns.
Therefore, a complete action is composed of a group of motionevent, they have a common downtime. When the view receives an event with a new downtime, it considers that the previous action has been processed.
This piece is rarely mentioned, and usually not used. But it's important.
Although the parameter of this method is an event, it actually intercepts a complete action. This means that if you return true for a down event or a move event (meaning to tell the view: "I'm going to intercept, this action I'll handle!"), all events after that are no longer passed to the child view until a new action starts!
Here we can try to explain why it is often difficult to slide left and right when Viewpager is placed inside the ScrollView. (Cheese can try to write a demo yourself, you will find that the left and right sliding switch will become much more difficult than outside.)
The reason is that when the ScrollView receives the move event, it will judge the difference in the y-coordinate of the two events before and after a certain threshold is considered to be a sliding event, and then ruthlessly passes through the onintercepttouchevent.
And this threshold is particularly small ...
When we swipe left and right, it's hard to keep our fingers fully horizontal. A little bit of angle will produce a change in the Y value, and then ScrollView uncle's onintercepttouchevent think "lying trough this is up and down sliding events ah, the labor can not ignore AH!!!!". Then decisively returns true.
So the poor Viewpager no longer received the subsequent events, also can not switch.
Talk about the Android touch event distribution mechanism