Android story meeting: event delivery mechanism

1. Goal of the short story

        Helping Android primary and intermediate development students make it easier to understand the Android event delivery mechanism. This article aims to make the Android event delivery mechanism more vivid and help readers get started in the form of stories. After getting started, readers can read other more in-depth blog posts, or read the source code by themselves.

Two, a hundred-character short story 

        There was a car accident at A. B traffic police command center needs to be responsible for processing. After understanding the situation, the B traffic police command center felt that it was necessary to send someone to the scene. B traffic police command center arranged D traffic police to be responsible for handling. The D traffic policeman handles the accident and reports the result to the B traffic police command center.

3. Dismantling the short story

        A地:Android Framework

        Crash: MotionEvent (click event)

        There was a car accident at A: Android Framework received MotionEvent

        B traffic police command center: ViewGroup

        Responsible for processing: dispatchTouchEvent(MotionEvent)

        B traffic police command center needs to be responsible for processing: dispatchTouchEvent(MotionEvent) of ViewGroup is called

        After the B traffic police command center understands the situation, it needs to send someone to the scene: ViewGroup's onInterceptTouchEvent(MotionEvent) is called and returns false

        B traffic police command center arranges D traffic police to be responsible for processing: ViewGroup calls View's dispatchTouchEvent(MotionEvent)

        Processing: onTouchEvent(MotionEvent)

        The D traffic policeman handles the accident and reports the result to the B traffic police command center: the result of View's onTouchEvent (MotionEvent) is true, and returns to the ViewGroup's dispatchTouchEvent (MotionEvent).

Fourth, the focus of the short story

        dispatchTouchEvent(MotionEvent): Indicates that it is responsible for processing, and it is actually the external interface of ViewGroup or View. Expressions pass on responsibility to it.

        onTouchEvent(MotionEvent): Indicates processing, which is actually the specific processing details.

        onInterceptTouchEvent(MotionEvent): Indicates whether to determine whether to hand over the responsibility to the subordinate, that is, whether to call the dispatchTouchEvent(MotionEvent) of the child View.

Five, short story code

        1. ViewGroup.dispatchTouchEvent(MotionEvent)

        2. ViewGroup.onInterceptTouchEvent(MotionEvent) returns false

        3.        View.dispatchTouchEvent(MotionEvent)

        4.                View.onTouchEvent(MotionEvent)

6. Expansion of short stories

        There is also a setOnTouchListener(OnTouchListener) method in View. OnTouchListener can be understood as D traffic police and an intern C. onTouch(View, MotionEvent) returns true to indicate that the intern can solve it, so there is no need to call onTouchEvent(MotionEvent) to let the traffic police solve it.

7. Short story rehearsal

        Different calling situations can correspond to different situations in this story framework. You can find a blog that transmits U-shaped diagrams of Android events, and tell the story of this car accident in comparison. Of course, for the convenience of readers, there is also a corresponding demo code, which can be debugged by yourself: afunx/XFun: It is a Android project. Just for fun. (github.com)

        Other demo codes are included in the link. For event delivery, please see TouchEventMainActivity.java, TouchDemoView.java and TouchDemoViewGroup.java. You can also write your own short story.

Guess you like

Origin blog.csdn.net/afunx/article/details/121734568