Android Event Distribution

Source: Internet
Author: User

When you go to an interview as a fresh student, the interviewer may simply ask you the basics of Android's four components, various layouts, but you're not as simple as a job-seeker, and today you'll discuss many of the things that interviewers like to ask about Android event distribution.

Why should Android have event distribution? Because it's arranged in layers, how does he know which layer you're clicking on, and you need to use the event distribution.

When it comes to event distribution, you might think of those three mighty methods: Event Distribution (dispatchtouchevent (motionevent ev)), event interception (onintercepttouchevent (motionevent ev)), event response ( Ontouchevent (motionevent ev)), where ViewGroup responds to these three methods, the view depends on whether it has a child view, does not respond to event interception without a child view, and activity does not respond to event interception.

Here is a flow chart for event distribution


Of course not all the circumstances are so go, when your view and ViewGroup are the system default, is based on the process to go, as long as the view or viewgroup any one of the events interested in the event, the process is not necessarily what it looks like, this need specific problem specific analysis

Below through a demo analysis of specific problems, demo only paste the code, do not provide code, I hope to move hands, write, to see exactly what it looks like

I wrote the demo one has three parts, activity,view,viewgroup three parts, rewrite the three mighty functions, of course, like Activity so do not write event interception, learn to use flexible

1,activity Code

Package Com.thea.guo;import Android.os.bundle;import Android.app.activity;import android.util.log;import Android.view.menu;import Android.view.motionevent;import Android.view.view;import Android.widget.Button;public Class Mainactivity extends Activity {button button; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} @Overridepublic boolean dispatchtouchevent (motionevent ev) {switch (ev.getaction ()) {case Motionevent.action_down: LOG.D ("Com.thea.guo", "Mainactivity.dispatchtouchevent is Down"); Break;case motionevent.action_move:log.d (" Com.thea.guo "," mainactivity.dispatchtouchevent is moving "); Break;case motionevent.action_up:log.d (" Com.thea.guo ", "Mainactivity.dispatchtouchevent is up"); return super.dispatchtouchevent (EV);} @Overridepublic boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent.action_down: LOG.D ("Com.thea.guo", "Mainactivity.ontouchevent is Down"); BreAk;case motionevent.action_move:log.d ("Com.thea.guo", "mainactivity.ontouchevent is moving"); break;case MOTIONEVENT.ACTION_UP:LOG.D ("Com.thea.guo", "Mainactivity.ontouchevent is up"); Return Super.ontouchevent (event);}}

2.view Code

/** * */package com.thea.guo.view;import android.content.context;import Android.util.attributeset;import Android.util.log;import android.view.motionevent;import Android.widget.button;import android.widget.TextView;/** * @author Sub-Ink * * 2015-1-15 */public class MyView extends TextView {public MyView (context context, AttributeSet Attrs) {Super (context, attrs);} @Overridepublic boolean dispatchtouchevent (motionevent ev) {switch (ev.getaction ()) {case Motionevent.action_down: LOG.D ("Com.thea.guo", "Myview.dispatchtouchevent is Down"); Break;case motionevent.action_move:log.d ("Com.thea.guo" , "Myview.dispatchtouchevent is moving"), Break;case motionevent.action_up:log.d ("Com.thea.guo", " Myview.dispatchtouchevent is up "); return super.dispatchtouchevent (EV);} @Overridepublic boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent.action_down: LOG.D ("Com.thea.guo", "Myview.ontouchevent is Down"), Break;case motionevent.action_move:log.d ("Com.thea.guo", " Myview.ontoUchevent is moving "), Break;case motionevent.action_up:log.d (" Com.thea.guo "," Myview.ontouchevent was up "); Return Super.ontouchevent (event);}}

3.ViewGroup Code

/** * */package com.thea.guo.view;import android.content.context;import Android.util.attributeset;import Android.util.log;import android.view.motionevent;import android.widget.linearlayout;/** * @author zi mo * * 2015-1-15 * * public class Myviewgroup extends LinearLayout {public Myviewgroup (context context, AttributeSet Attrs) {Super (context, at TRS);} @Overridepublic boolean dispatchtouchevent (motionevent ev) {switch (ev.getaction ()) {case Motionevent.action_down: LOG.D ("Com.thea.guo", "Myviewgroup.dispatchtouchevent is Down"); Break;case motionevent.action_move:log.d (" Com.thea.guo "," myviewgroup.dispatchtouchevent is moving "); Break;case motionevent.action_up:log.d (" Com.thea.guo "," Myviewgroup.dispatchtouchevent is up "); return super.dispatchtouchevent (EV);} @Overridepublic boolean onintercepttouchevent (motionevent ev) {switch (ev.getaction ()) {case Motionevent.action_down: LOG.D ("Com.thea.guo", "Myviewgroup.onintercepttouchevent is Down"); Break;case motionevent.action_move:log.d (" Com.thEa.guo "," myviewgroup.onintercepttouchevent is moving "); Break;case motionevent.action_up:log.d (" Com.thea.guo "," Myviewgroup.onintercepttouchevent is up "); return super.onintercepttouchevent (EV);} @Overridepublic boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent.action_down: LOG.D ("Com.thea.guo", "Myviewgroup.ontouchevent is Down"), Break;case motionevent.action_move:log.d ("Com.thea.guo", "Myviewgroup.ontouchevent is moving"); Break;case motionevent.action_up:log.d ("Com.thea.guo", " Myviewgroup.ontouchevent is up "); Return Super.ontouchevent (event);}}

4.xml Code

<com.thea.guo.view.myviewgroup xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= " Http://schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_ Parent "     android:gravity=" center ">    <com.thea.guo.view.myview        android:id=" @+id/my_button "        Android:layout_width= "200DP"        android:layout_height= "200DP"        android:text= "@string/hello_world"         Android:background= "@android: Color/holo_blue_bright" >    </com.thea.guo.view.myview></ Com.thea.guo.view.myviewgroup>

The above is all the code, in fact, you can also part of the code to encapsulate a better, such as the down,move,up part is the same, you can put this part of the package. Let's start by analyzing the special cases mentioned above.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1. Modify the ViewGroup dispatchtouchevent (event distribution) return value

(1) The return value is True

The above two pictures more prominent explanation of its distribution process, first from the activity of the dispatchtouchevent began distribution, the system by default continue to distribute, distributed to ViewGroup Dispatchtouchevent, ViewGroup's Dispatchtouchevent returns True, which means it is interested in this event, and it wants to keep its own processing, and the event stops distributing.

(2) The return value is False



It is also distributed from the dispatchtouchevent of the activity, and the dispatchtouchevent of Dispatchtouchevent,viewgroup distributed to ViewGroup returns FALSE, ViewGroup not interested, but it also does not cooperate with you, he directly does not give you down pass, this time need to ontouchevent to deal with, because the view is from the activity started, so you need activity of ontouchevent to deal with

2. Modify the Onintercepttouchevent return value of the ViewGroup

(1) The return value is True



or from the activity of the dispatchtouchevent began distribution, distributed to ViewGroup Dispatchtouchevent,viewgroup judge whether to intercept, Since the return value of onintercepttouchevent here is true, I'm sorry it's intercepted, it wants its ontouchevent to handle this event, ViewGroup ontouchevent processing is completed and continues to pass down to the activity's ontouchevent

(2) The return value is False



is not familiar Ah, yes, viewgroup in the onintercepttouchevent default is not intercept, first from the activity of dispatchtouchevent began distribution, Distribute to Dispatchtouchevent in ViewGroup, then determine if ViewGroup is intercepted, return false here, indicate that it does not intercept, and then continue to distribute, distributed to the dispatchtouchevent in view, View under No child view, so he directly to his ontouchevent processing, ontouchevent not interested in this, continue to pass

3. Modify the Dispatchtouchevent return value of the view

(1) The return value is True



This is still the distribution process, when distributed to the view, it returns True, that is, it is interested and it wants to handle it on its own.

(2) The return value is False



Or that formula or that flavor, the view dispatchtouchevent return False, it is directly not distributed, left to Ontouch processing, because the view is passed from ViewGroup, that also viewgroup from a processing.

4. Modify the return value of ontouchevent in view

(1) The return value is True



The event is passed as always, when passed to the view of the ontouchevent, return true, it will say sorry, not to you pass, to this it to deal with its own

(2) The return value is False



The back of the ontouchevent and view are almost, here no longer wordy, the following summary, end this blog


1.dispatchTouchEvent (Event distribution)

When Dispatchtouchevent returns True, it is handled by itself

When Dispatchtouchevent returns false, it is handed to ontouchevent to handle

The system is distributed to onintercepttouchevent by default

2.onInterceptTouchEvent (Event intercept)

When Onintercepttouchevent returns True, it is handled by his own ontouchevent

When Onintercepttouchevent returns false, continue to distribute

System does not intercept by default

3.onTouchEvent (Event response)

When Ontouchevent returns True, handle it yourself

When Ontouchevent returns false, it continues to pass

System continues to pass by default

Android Event Distribution

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.