ViewGroup of Android Event Distribution Mechanism

In the last article, we introduced the View article of the Android event distribution mechanism. I believe you have a certain understanding of Android event distribution! Before we talk about it, we need to clarify two concepts: one is View and the other is ViewGroup. Of course, ViewGroup is a container, its parent class is View, and it also contains many child Views and ViewGroups. Well, let's get to the topic of this article!

First of all, we provide a demo rendering:
write picture description here
the interface of the rendering is very simple, and there is only one button in the interface.

1. The layout file

activity_main.xml file

<LinearLayout 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" >

    <cn.ithm.eventdemo.MyLayout
        android:id="@+id/mylayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center" >

        <cn.ithm.eventdemo.MyButton
            android:id="@+id/mybutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    </cn.ithm.eventdemo.MyLayout>

</LinearLayout>

Second, the main class file

1. MyButton.java class

public class MyButton extends Button {

    private static final String TAG = "MyButton";

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.i(TAG, "MyButton dispatchTouchEvent--ACTION_DOWN");
            break;
        case MotionEvent.ACTION_UP:
            Log.i(TAG, "MyButton dispatchTouchEvent--ACTION_UP");

            break;

        default:
            break;
        }
        return super.dispatchTouchEvent(event);
//      return true;
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.i(TAG, "MyButton onTouchEvent--ACTION_DOWN");

            break;
        case MotionEvent.ACTION_UP:
            Log.i(TAG, "MyButton onTouchEvent--ACTION_UP");

            break;

        default:
            break;
        }
        return super.onTouchEvent(event);
    }

}

2.MyLayout.java class

public class MyLayout extends LinearLayout {

    private static final String TAG = "MyLayout";

    public MyLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.i(TAG, "MyLayout dispatchTouchEvent--ACTION_DOWN");
            break;
        case MotionEvent.ACTION_UP:
            Log.i(TAG, "MyLayout dispatchTouchEvent--ACTION_UP");

            break;

        default:
            break;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.i(TAG, "MyLayout onInterceptTouchEvent--ACTION_DOWN");
            break;
        case MotionEvent.ACTION_UP:
            Log.i(TAG, "MyLayout onInterceptTouchEvent--ACTION_UP");

            break;

        default:
            break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.i(TAG, "MyLayout onTouchEvent--ACTION_DOWN");
            break;
        case MotionEvent.ACTION_UP:
            Log.i(TAG, "MyLayout onTouchEvent--ACTION_UP");

            break;

        default:
            break;
        }
        return super.onTouchEvent(event);
    }

}

3.MainActivity.java class

public class MainActivity extends Activity {

    protected static final String TAG = "MainActivity";
    private Button button;
    private LinearLayout myLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.mybutton);
        myLayout = (LinearLayout) findViewById(R.id.mylayout);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Log.i(TAG, "MyButton onclick");
            }
        });

        button.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                switch (arg1.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    Log.i(TAG, "MyButton setOnTouchListener--ACTION_DOWN");

                    break;
                case MotionEvent.ACTION_UP:

                    Log.i(TAG, "MyButton setOnTouchListener--ACTION_UP");
                    break;

                default:
                    break;
                }

                return false;
            }
        });

        myLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Log.i(TAG, "myLayout onclick");
            }
        });

        myLayout.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                switch (arg1.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    Log.i(TAG, "myLayout setOnTouchListener--ACTION_DOWN");

                    break;
                case MotionEvent.ACTION_UP:

                    Log.i(TAG, "myLayout setOnTouchListener--ACTION_UP");
                    break;

                default:
                    break;
                }

                return false;
            }
        });
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.i(TAG, "MainActivity dispatchTouchEvent--ACTION_DOWN");
            break;
        case MotionEvent.ACTION_UP:
            Log.i(TAG, "MainActivity dispatchTouchEvent--ACTION_UP");
            break;

        default:
            break;
        }
        return super.dispatchTouchEvent(ev);
        // return true;
    }

}

3. Analyze the event distribution process

1. First, we click the "Event Dispatch" button to view the LogCat, and the print result is:
write picture description here
We found that its distribution order is: dispatchTouchEvent method of Activity -> dispatchTouchEvent method of ViewGroup -> onInterceptTouchEvent method of ViewGroup -> dispatchTouchEvent of child View Method -> onTouch method of child View -> method of onTouchEvent of child View, then up, which is the same as down. The last is the onClick event of the child view.
2. Then we click on the blank space of the interface and check the LogCat. The print result is:
write picture description here
We found that its distribution order is: Activity's dispatchTouchEvent method -> ViewGroup's dispatchTouchEvent method -> ViewGroup's onInterceptTouchEvent method -> ViewGroup's onTouch method - > ViewGroup's onTouchEvent method, then up, which is the same as down. The last is the onClick event of the ViewGroup.

4. Summary

1. In fact, the event distribution of ViewGroup is one more method onInterceptTouchEvent than View. Why is there such a method? Mainly because ViewGroup has child View, and View has no child view.
2. In the same way, the event distribution of the ViewGroup is consumed at a certain point, and it will not continue to be forwarded.

Finally, the event distribution of Android is simply and roughly finished. I believe readers should have a general understanding.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325874645&siteId=291194637