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:
Public
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:
<com.example.viewgrouptouchevent.mylayout
xmlns:android= "Http://schemas.android.com/apk/res/android"
xmlns:tools= "Http://schemas.android.com/tools"
android:id= "@+id/my_layout"
android:layout_width= " Match_parent "
android:layout_height=" match_parent "
android:orientation=" vertical ">
<button
android:id= "@+id/button1"
android:layout_width= "match_parent"
android:layout_height= "WRAP_" Content "
android:text=" Button1 "/>
<button
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 ");
}
);