OnInterceptTouchEvent, dispatchTouchEvent, onTouchEvent, requestDisallowInterceptTouchEvent
Distribution and delivery of this event in android, the solution for handling,
The essence should be the responsibility chain mode in the java design mode.
Here, we want to use the least, the most easy-to-understand way to record
View Method
// Event distribution. The default value is false public boolean dispatchTouchEvent (MotionEvent event). // The default value is false public boolean onTouchEvent (MotionEvent event)
ViewGroup Method
// Event distribution. The default value is false public boolean dispatchTouchEvent (MotionEvent event) // event processing. The default value is false public boolean onTouchEvent (MotionEvent event). // The default value is false, by default, false is returned. // if true is returned, the event will not be passed down. onTouchEvent processing is performed. // if false is returned, the event public boolean onInterceptTouchEvent (MotionEvent ev) will be passed down)
When an event is triggered, the parent layout will first receive the event for distribution, that is, the General
View separately (if OnTouchListener is set)
DispatchEvent-setOnTouchListener-onTouchEvent
View ViewGroup separately (if OnTouchListener is set)
DispatchTouchEvent-onInterceptTouchEvent-setOnTouchListener-onTouchEvent
3.Nested View in ViewGroup,
That is, the most useful and important
As mentioned above, there is a precondition that the underlying View can receive this event: if the parent level permits this event. If the dispatch method at the parent level is not changed, the system will first call the onInterceptTouchEvent method of the parent View before the system calls the underlying onTouchEvent to determine whether the parent View wants to intercept the action after the touch event.
So the following things must be clearly divided. This is very important. Many people are very vague, or very ran.
The execution sequence of event distribution:
(Parent) dispatchTouchEvent
Bytes
(Parent) onInterceptTouchEvent
Bytes
(Sub) dispatchEvent
Event Processing execution sequence
(Child) onTouchEvent
Bytes
(Parent) onTouchEvent
4. If the click events set by both the parent View and the Child View are faulty
In fact, they can all respond. Not to mention, the layout of the adapter item in ListView is much more written.
5RequestDisallowInterceptTouchEvent
RequestDisallowInterceptTouchEvent is a public method in the ViewGroup class.
Called when a child does not want this parent and its ancestors to intercept touch events with ViewGroup. onInterceptTouchEvent (MotionEvent ).
This parent shocould pass this call onto its parents. this parent must obey this request for the duration of the touch (that is, only clear the flag after this parent has received ed an up or a cancel.
In practice, you can inject an instance of the parent ViewGroup into the ontouch event of the Child view, and call requestDisallowInterceptTouchEvent to prevent the parent view from intercepting click events.
Originally, the first row of listView is nested with ViewPager, which is used in other nested rows.
Operations are required in the Child layout. The parent layout does not intercept its own events.
@ Override public boolean dispatchTouchEvent (MotionEvent ev) {// Let the parent class not intercept touch events. This. getParent (). requestDisallowInterceptTouchEvent (true); return super. dispatchTouchEvent (ev );}