The Android event distribution mechanism is fully parsed, taking you from the source perspective to thoroughly understand (the)

Source: Internet
Author: User
Tags xmlns

Remember in the previous article, I took you from the source point of view of the analysis of the Android view of the event distribution mechanism, I believe that the reading of the Friends of the view of the event distribution has a more profound understanding.

Have not read the friends, please refer to the Android event distribution mechanism to fully resolve, take you from the source point of view thoroughly understand (above).

So today we will continue the last unfinished topic, from the source point of view analysis of VIEWGRUOP event distribution.

First, let's explore what is ViewGroup? What's the difference between it and a normal view?

As the name suggests, ViewGroup is a collection of view sets that contains a lot of child view and Vewgroup, and is the parent or indirect parent of all layouts in Android, such as LinearLayout, Relativelayout and so on are inherited from ViewGroup. But ViewGroup is actually a view, but it has more features that can contain child view and define layout parameters than view. The schematic diagram of the ViewGroup inheritance structure looks like this:

As you can see, the various layouts that we usually use in our projects are all part of ViewGroup's subclass.

After a brief introduction viewgroup, we now use a demo to demonstrate the Vewgroup event distribution process in Android.

First we come from the definition of a layout, named Mylayout, inherited from LinearLayout, as follows:


class Mylayout extends LinearLayout {public  
    mylayout (context context, AttributeSet Attrs) {  
        super ( context, attrs);  

Then, open the main layout file, Activity_main.xml, and add our custom layout to it:


xmlns:android= "Http://"
    xmlns:tools= "Http://"
    android:id= "@+id/my_layout"
    android:layout_width= " Match_parent "
    android:layout_height=" match_parent "
    android:orientation=" vertical ">  
        android:id= "@+id/button1"
        android:layout_width= "match_parent"
        android:layout_height= "WRAP_" Content "
        android:text=" Button1 "/>  
        android:id=" @+id/button2 "
        android:layout_ Width= "Match_parent"
        android:layout_height= "wrap_content" android:text= "Button2
</ Com.example.viewgrouptouchevent.mylayout>

As you can see, we've added two buttons to the mylayout and then registered a listener event in mainactivity for both the buttons and the Mylayout:

Mylayout.setontouchlistener (New Ontouchlistener () {  
    @Override public
    boolean Ontouch (View V, motionevent Event) {  
        log.d ("TAG", "Mylayout on Touch");  
        return false;  
Button1.setonclicklistener (New Onclicklistener () {  
    @Override public
    void OnClick (View v) {  
        log.d ("TAG") "You clicked Button1");  
; Button2.setonclicklistener (New Onclicklistener () {  
    @Override public
    void OnClick (View v) {  
        log.d ("TAG "," You clicked Button2 ");  

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.