When our fingers are pressed, Android uses layer-by-layer delivery-bubbling way to handle click events. For example, now the company came to a small project, the boss a look assigned to the manager to do, the manager a look assigned to team leader, team leader a bullish simple, assigned to the team members. If in this transfer process (that is, also to the bottom of the allocation), a certain layer think I am responsible for this relatively good will intercept the message, and then handle it, the following will not receive the message of this notice. If the team is up to the bottom, the crew will finish it if they can. If not, then report to the team leader, said the leader I do not come, learning while doing to affect the progress. Team leader A look I also do not come, give manager, manager a look I also not, give the boss. This is also a layer of transmission.
The above means that the message is passed from top to bottom, and if it is intercepted in the process of transmission, stop the transmission. If it is not intercepted, it is passed to the bottom, and if the bottom is not able to consume the message, then another layer of return, back to the upper layer, until it is consumed or reached the topmost level. In this process, there are three important methods:
Dispathtouchevent (motionevent ev) onintercepttouchevent (motionevent ev) ontouchevent (motionevent ev)
Where thedispathtouchevent method is responsible for the distribution of the event, and its return value is to indicate whether the current event is consumed;
The onintercepttouchevent method is used to determine whether to intercept the message, and if the current view intercepts a time, the method is not called again in the same sequence of events. Returns a result indicating whether the current event is blocked.
The ontouchevent method is to handle the event. The return result indicates whether the current event is consumed, and if not consumed, the current view cannot receive the event again in the same time series.
For a root viewgroup, when a click event is generated, it is first passed to it, calling its Dispath method. If the ViewGroup Onintercept method returns True to indicate that it is intercepting the current event, false means not to intercept, this time the event will continue to pass to the child element, and then call the Dispath method of the child element, repeating the above process to the event is processed.
Here are some common questions and workarounds:
First, sliding conflict
The sliding conflict generation of view is likely to be divided into three types:
- External sliding and internal sliding direction inconsistent
- The external sliding direction is consistent with the internal sliding direction
- Nesting the above two cases
For example a common, outside a ListView, inside a scrollview, sliding when there is a conflict?
At this time, the general use of external interception method (that is, combined with onintercepttouchevent,ontouchevent and onkeyevent) to solve, in particular, can refer to the viewpager of the sliding conflict. Here's how:
(1) External interception method
external interception means that all clicks are intercepted by the parent container, and if the parent container needs this event, it is not intercepted if this event is not required. By overriding the parent container's Onintercepttouchevent method:
CaseMotionEvent.ACTION_DOWN:intercepted=false; Break; CaseMotionevent.action_move:if(required by the parent class container) {intercepted=true; } Else{intercepted=false; } Break; CaseMotionEvent.ACTION_UP:intercepted=false; Break; returnintercepted;
Note: The Action_down event parent container must return false, because if the parent class container is intercepted, all events, such as the subsequent move, will be processed directly by the parent class container and cannot be passed to the child element. The up event also returns false because it does not have much meaning for itself, but is different for child elements, and if intercepted, the onclick event of the child element cannot be triggered.
Internal interception Method
This method refers to the parent container does not intercept any time, all events are passed to the child element, if the child element needs this event to be consumed directly, otherwise it will be handed over to the parent container for processing. It needs to match the Requestdisallowintercepttouchevent method to work properly. We need to rewrite the dispatch method of the child element.
Case Motionevent.action_down: parent.requestdisallowintercepttouchevent (true); Break ; Motionevent.action_move: if(the parent container requires such a click event) { Parent.requestdisallowintercepttouchevent (false); } Break ; return Super. Dispatchtouchevent (event);
In this way, the parent container needs to intercept the time other than Action_down by default, so that the parent element can continue to intercept the required events when the child element invokes the request method.
Other of
If you feel that the above two ways too complex, look dizzy, in fact, you can also according to the actual needs of the project to specify their own strategy implementation. For example, you can guess whether the user is trying to manipulate the control by determining which control you are touching, based on the position of the point you're pointing at. If you click a blank place, you can manipulate the external controls.
Processing and FAQs of Click events in Android