Custom controls We often may encounter this situation, we added a few clicks, the handling of the sliding event led to other controls to click, the slide is not used, the sliding reaction is very slow, to stroke a lot of guesses to move a little bit and so on. Perhaps our first reaction is Baidu, Google to search the answer, copy the code directly over. In fact, maybe you can change the solution, and think about why this situation occurs.
The following is a preliminary exploration of the Android event distribution mechanism by bloggers. Hope that you can read the Android event distribution mechanism has a detailed understanding, after the use of Baidu, Google can easily resolve due to event conflicts caused by various problems.
First of all, we have a preliminary understanding of the Android event:
1.Android Touch Event-related functions include:
Dispatchtouchevent (motionevent EV): Functions responsible for event distribution, which are first called in each view
Onintercepttouchevent (motionevent ev): Function of event interception (VIEWGROUPF very important function, as specified below)
Ontouchevent (motionevent ev): function of event response
OnTouch (motionevent EV): Event-Appropriate function
Ontouchevent (motionevent ev) and Ontouch (motionevent ev) are functions of event response, 2 differences: Ontouch will take precedence over ontouchevent calls, Ontouch is called only if the listener is not empty and the clicked control is enable, Ontouch can listen by passing in the Ontouchlistener through the control, and Ontouchevent cannot pass the external setting. (it can be described too abstract, the simple point is that some controls do not have Ontouch events, or the control is not clickable then we want to listen to Ontouch events must rewrite ontouchevent to implement monitoring)
Please see the following view of the Dispatchtouchevent source code in the call Ontouch () and ontouchevent () difference:
if (onfiltertoucheventforsecurity (event)) { //noinspection simplifiableifstatement listenerinfo li = Mlistenerinfo; if (Li! = null && Li.montouchlistener! = null && (mviewflags & enabled_mask) = = ENABLED && Amp Li.mOnTouchListener.onTouch (this, event)) { result = true; } if (!result && ontouchevent (event)) { result = true; } }
The outer judgment is for the moment (to determine if the view is at the top, if the view is not at the top, filter out the Click event), please note the Inner judgment, When the Montouchlistener in Mlistenerinfo is not empty (that is, we register a listener event for view) and the view is clickable, the event is handed to Mlistenerinfo Montouchlistener.ontouch for processing and based on the Ontouch Listener Boolean to determine whether the event continues to pass, depending on the value of result to determine whether to invoke Ontouchevent
Return value Description: When Dispatchtouchevent (motionevent ev) returns to False to continue to pass up, true means stop delivery
The following is the order of event delivery:
Suppose we have a linearlayout that has a button in the layout. Then the touch event is delivered as follows:
Activity's dispatchtouchevent ()------>linearlayout's dispatchtouchevent ()---------> Onintercepttouchevent ()------->button dispatchtouchevent () is passed up from the root element, If we rewrite the dispatchtouchevent () in the middle of a view and return True, then the event stops passing and is consumed by the current function. Ontouch and Ontouchevent are the same (as described above), except that the order is exactly the same as the order of the Dispatchtouchevent, which is passed from the outermost layer to the root element.
As for Onintercepttouchevent (), the function is the function of viewgroup, which means that only viewgroup and subclasses of the class can override the function, for example, our custom view inherits from the LinearLayout ( LinearLayout is a subclass of ViewGroup), then we can rewrite the function to achieve the purpose of the event interception, which is called immediately after Dispatchtouchevent (), which is provided that the function is present and returns false by default. If Onintercepttouchevent () returns to false the event will continue to pass, and if it returns true, then the event will stop continuing to the above dispatchtouchevent () and give the event to its own Ontouch () and Ontouchevent () to deal with.
Let's look at the results of the experiment.
1. No change to the result returned by the event
The event was eventually consumed by CustomButton, from which we could get a diagram of the following events
2. Override Onintercepttouchevent and return to true truncation event to continue delivery
It needs to be explained that the event is not consumed in the coustomlinearlayout (that is, the touch correlation function returns all false), and if the activity is distributed, the activity will eventually be Ontouchevent () consumption, the following is the invocation of the
3.CustomButton ontouchevent () returns false
4. Click on Customlinearlayout, no click to CustomButton
From above we can get
1. In addition to Onintercepttouchevent (), other events are passed from the root element to the click View in order of 1, and are consumed by the view, and any function in the intermediate link returns True ( In addition to Onintercepttouchevent (), the event will be consumed by the function that is currently returning true, stop passing back, and because of the plethora of functions, the blogger does not post a case where each function returns TRUE.
2.ViewGroup subclass, overriding the Onintercepttouchevent () function, returns True, then the function stops passing dispatchtouchevent () to the child view and transfers the event to the current view's Ontouch () and ontouchevent () processing
3.view ontouchevent consumes events by default, and ViewGroup 0nTouchEvent does not consume events
4. The same level of view, according to the control you click on the event delivery to the corresponding view you clicked, if the click Is ViewGroup, then the event will not be consumed, until passed to the distribution of the root element of the ontouchevent () will be consumed
Master the above the basic knowledge of the event delivery, the next time we encounter event conflict can try to solve their own!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Custom Controls" Android event distribution mechanism