The event routing mechanism for GUI systems in Android
Source: Internet
Author: User
<span id="Label3"></p><p><p>Two days ago on the forum to see someone sent a post, asked an Android GUI event handling Problem: There is a linearlayout, there are a lot of child view, he asked how to listen to this linearlayout click event? His approach is to:</p></p><p><p>Setclickable (true);</p></p><p><p>Setonclicklistener (listener);</p></p><p><p>finally, he finds that the callback function in listener is not called at All.</p></p><p><p>In fact, in the Android GUI system, the hardware-triggered event (keyevent, touchevent, trackballevent, Etc.) was first taken by window, and window forwarded the event to the foreground Activity. But the activity also cannot dispose of itself immediately, but passes the event to the Contentview inside it.</p></p><p><p>If Contentview is a container view (inherited from the ViewGroup type), it is generally the first to determine which child view the event falls On. The event is then dispatch to the child VIEW.</p></p><p><p>Let's take the touch event as an example to see the dispatchtouchevent () function in the ViewGroup class:</p></p><pre> <span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;"> * {</span></span><span style="color: #808080;"><span style="color: #808080;">@inheritDoc</span></span><span style="color: #008000;"><span style="color: #008000;">} </span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">dispatchtouchevent (motionevent Ev) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">onfiltertoucheventforsecurity (ev)) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Action =<span style="color: #000000;"><span style="color: #000000;">ev.getaction (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>XF =<span style="color: #000000;"><span style="color: #000000;">Ev.getx (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>YF =<span style="color: #000000;"><span style="color: #000000;">ev.gety (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>Scrolledxfloat = XF +<span style="color: #000000;"><span style="color: #000000;">mscrollx; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>Scrolledyfloat = YF +<span style="color: #000000;"><span style="color: #000000;">mscrolly; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>Rect frame =<span style="color: #000000;"><span style="color: #000000;">mtemprect; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span>disallowintercept = (mgroupflags & flag_disallow_intercept)! = 0<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(action = =<span style="color: #000000;"><span style="color: #000000;">Motionevent.action_down) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(mmotiontarget! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">this <span style="color: #008000;">is weird, we got a pen down, but we thought it</span> was</span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">already down! </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Xxx:we should probably send an action_up</span> to the current</span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Target.</span></span>Mmotiontarget =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">if we ' re disallowing intercept or if we ' re allowing and we didn ' t</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Intercept</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(disallowintercept | |!<span style="color: #000000;"><span style="color: #000000;">onintercepttouchevent (ev)) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Reset this event ' s action (just to protect Ourselves)</span></span><span style="color: #000000;"><span style="color: #000000;">ev.setaction (motionevent.action_down); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">We know we want to dispatch the event down and find a</span> child</span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">who <span style="color: #008000;">can handle it, start with the Front-most.</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Scrolledxint = (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">) scrolledxfloat; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Scrolledyint = (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">) scrolledyfloat; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>view[] children =<span style="color: #000000;"><span style="color: #000000;">mchildren; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Count =<span style="color: #000000;"><span style="color: #000000;">mchildrencount; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>i = count-1; I >= 0; i--<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>View Child =<span style="color: #000000;"><span style="color: #000000;">children[i]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>((child.mviewflags & visibility_mask) = =<span style="color: #000000;"><span style="color: #000000;">VISIBLE</span></span>|| Child.getanimation ()! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {child.gethitrect (frame); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(frame.contains (scrolledxint, Scrolledyint)) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">offset the event to the view ' s coordinate system</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>XC = scrolledxfloat-<span style="color: #000000;"><span style="color: #000000;">child.mleft; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>YC = scrolledyfloat-<span style="color: #000000;"><span style="color: #000000;">child.mtop; Ev.setlocation (xc, yc); Child.mprivateflags</span></span>&= ~<span style="color: #000000;"><span style="color: #000000;">cancel_next_up_event; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(child.dispatchtouchevent (ev)) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Event handled, We have a target now.</span></span>Mmotiontarget =<span style="color: #000000;">child <span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the event didn ' t get handled, try the next view. </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Don ' t reset the event ' s location, it</span> 's not</span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">necessary Here.</span></span><span style="color: #000000;"><span style="color: #000000;"> } } } } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span>Isuporcancel = (action = = Motionevent.action_up) | |<span style="color: #000000;"><span style="color: #000000;">(action</span></span>==<span style="color: #000000;"><span style="color: #000000;">motionevent.action_cancel); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(isuporcancel) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Note, we ' ve already copied the previous state to our local</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">variable takes effect on the next event</span></span>Mgroupflags &= ~<span style="color: #000000;"><span style="color: #000000;">flag_disallow_intercept; } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the event wasn ' t an action_down, dispatch it to our target if</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">We</span> have One.</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>View target =<span style="color: #000000;"><span style="color: #000000;">mmotiontarget; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(target = =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">we don ' t have a target and this means We ' re handling the</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">event as a regular view.</span></span><span style="color: #000000;"><span style="color: #000000;">ev.setlocation (xf, yf); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>((mprivateflags & cancel_next_up_event)! = 0<span style="color: #000000;"><span style="color: #000000;">) {ev.setaction (motionevent.action_cancel); Mprivateflags</span></span>&= ~<span style="color: #000000;"><span style="color: #000000;">cancel_next_up_event; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. Dispatchtouchevent (ev); } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">If there is a target, see if we ' re allowed to and want to intercept</span> its</span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Events</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!disallowintercept &&<span style="color: #000000;"><span style="color: #000000;">onintercepttouchevent (ev)) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>XC = scrolledxfloat-(<span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span><span style="color: #000000;"><span style="color: #000000;">) target.mleft; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>YC = scrolledyfloat-(<span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span><span style="color: #000000;"><span style="color: #000000;">) target.mtop; Mprivateflags</span></span>&= ~<span style="color: #000000;"><span style="color: #000000;">cancel_next_up_event; Ev.setaction (motionevent.action_cancel); Ev.setlocation (xc, yc); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">target.dispatchtouchevent (ev)) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">target didn ' t handle action_cancel. much we can</span> do</span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">but <span style="color: #008000;">they should has.</span></span><span style="color: #000000;"><span style="color: #000000;"> } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Clear the target</span></span>Mmotiontarget =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Don ' t dispatch this event to our own view, because we already</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">saw it when intercepting, we just want to give the following</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">event to the normal ontouchevent ().</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(isuporcancel) {mmotiontarget</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">finally offset the event to the target ' s coordinate system</span> and</span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Dispatch the Event.</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>XC = scrolledxfloat-(<span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span><span style="color: #000000;"><span style="color: #000000;">) target.mleft; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>YC = scrolledyfloat-(<span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span><span style="color: #000000;"><span style="color: #000000;">) target.mtop; Ev.setlocation (xc, yc); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>((target.mprivateflags & cancel_next_up_event)! = 0<span style="color: #000000;"><span style="color: #000000;">) {ev.setaction (motionevent.action_cancel); Target.mprivateflags</span></span>&= ~<span style="color: #000000;"><span style="color: #000000;">cancel_next_up_event; Mmotiontarget</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">target.dispatchtouchevent (ev); }</span></span></pre><p><p>We can see that viewgroup by invoking the dispatchtouchevent () function of child view, the event follows the dispatch at the first level of the view tree structure (child view, grandchild view ...). )。</p></p><p><p>finally, the innermost child view gets the event, and it doesn't have a child view. It then begins to process the event (that is, the response event).</p></p><p><p>We also take the touch event as an example to see the dispatchtouchevent () function in the view class:</p></p><pre> <span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Pass the touch screen motion event to the target view, or this * view if it is the Target. * * </span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">Event The motion event to being Dispatched. * </span></span><span style="color: #808080;"><span style="color: #808080;">@return</span></span><span style="color: #008000;"><span style="color: #008000;">True If the event is handled by the view, false otherwise. </span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">dispatchtouchevent (motionevent Event) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">onfiltertoucheventforsecurity (event)) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(montouchlistener! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>&& (mviewflags & enabled_mask) = = ENABLED &&<span style="color: #000000;"><span style="color: #000000;">Montouchlistener.ontouch (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">, Event)) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">ontouchevent (event); }</span></span></pre><p><p>first, it is to determine if this touch event is safe, if not safe, to Return.</p></p><p><p>then, determine if the touch event listener is set and, if set, call the Listener's Ontouchlistener handler and Return.</p></p><p><p>If there is no touch event listener, call itself to define the Ontouchevent () method and Return.</p></p><p><p>In this event handler, you return a Boolean type Value. If False is returned, its parent container can also get the event Again. If True is returned, it is the same as telling the parent container: I'm in control, so don't ask.</p></p><p><p>therefore, activity has the lowest level of handling event Permissions.</p></p><p><p>We also notice that in addition to the Hardware-triggered event (keyevent, touchevent, trackballevent, and so on), there are some event, such as: Click, longpress, doubleclick, and so On. These are typically Human-made "hardware-triggered event" packages. For example, The Click event is packaged using MOTIONEVENT.ACTION_UP and Motionevent.action_down in Touchevent. By default, They cause the ontouchevent handler function to return True. As a result, linearlayout children once set their own click event listener, LinearLayout itself will not get the event, because its children have been processed. This is the answer to the question in the forum!</p></p><p><p>however, There is also a Solution. When we look at the dispatchtouchevent () function of viewgroup, we should note that viewgroup before the event is dispatch to child view, The onintercepttouchevent () function is called First (intercept is the meaning of interception). therefore, we can intercept the event that we want to handle by overriding the LinearLayout onintercepttouchevent () method, which is called before it is passed to the CHILD.</p></p><p><p></p></p><p><p>The event routing mechanism for GUI systems in Android</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service