Android view event distribution and consumption source code simple understanding

Android view event distribution and consumption source code simple understanding

preface:

During the development process, I felt that the view event was particularly brain burning. After reading it for a long time, I thought I understood it. In the middle, I checked the reading notes of singwhatiwanna fans on the Internet, which made me feel enlightened.

Very important learning method: turn complexity into simplicity and focus only on the key points.

The source code is a lump. Don't expect to understand every line of code. First, it's unnecessary. Second, a lot of non critical code will make you blur the really important parts. The following is just the learning results of the elder sister. If you want to understand deeply, you need to see the source code yourself.

2. Source code analysis

Because the source code is too long and not easy to understand, I won't post it here because it's unnecessary.

The following is the simplified source code of Xuejie.

(1)ViewGroup.dispatchTouchEvent(event)

(2)View.dispatchTouchEvent(event)

3. Summary

Summarize the event distribution and consumption process of ViewGroup:

The whole process includes three parts: judge whether to intercept - > find the child view receiving the touch event - > event distribution and consumption

Judge whether to intercept:

(1) ACTION_ Down or non action_ When down and the child view receiving the touch event is found, onintercepttouchevent (event) determines whether to intercept or not

(2) Non action_ When down, and the child view receiving the touch event is not found, it indicates that the touch event needs to be intercepted

Here we explain that there are two factors that affect whether the ViewGroup can intercept touch events: whether the child view and onintercepttouchevent (event) receiving touch events are found. The process of finding the child view receiving touch events only needs to be in action_ Just make sure when you go down. If action_ I didn't find it when I went down, so action_ Move and action_ Up cannot be found, so the touch event is directly intercepted by ViewGroup. If the child view receiving the touch event is found, action_ Move and action_ In the up case, check the onintercepttouchevent (event) of the ViewGroup to see if it is intercepted.

Find the child view that receives the touch event:

(1) Find in two cases: action_ Down and ViewGroup does not intercept.

(2) Search method: traverse all child views. If the XY coordinate of the touch event is within the range of a child view of the ViewGroup, recursively distribute the touch event for the child view. If a child view is found to handle the touch event (return true), the loop will jump out.

Here we explain the search criteria. Obviously, only action is needed to find the child view that receives the touch event_ It's OK under the down condition. There's no need for action_ Move and action_ Check all up, otherwise repeat the operation. If the ViewGroup has been intercepted, you obviously don't need to think about the child views.

Event distribution and consumption:

(1) There are two situations: distribution and consumption of ViewGroup or distribution and consumption of child views of ViewGroup

(2) If the child view receiving the touch event is not found after the above two steps, the ViewGroup will distribute and consume it. The distribution and calling process is: viewgroup.dispatchtouchevent - > view.dispatchtouchevent - > montouchlistener.ontouch - > ontouchevent - > onclick

(3) If the child view receiving the touch event is found, the recursive distribution and consumption of the touch event will be performed for the child view

Supplement:

(1) In the source code, mfirsttouchevent represents the child view that receives the touch event

(2) Both steps 2 and 3 perform the operation of dispatchtransformed touchevent (event, child). Step 2 is only to find the child view receiving the touch event. Step 3 is mainly used for event distribution and consumption. If the method has been executed for a child view in step 2, it will not be repeated in step 3. I don't know if there is any mistake.

4. Conclusion

(1) Callback method

ViewGroup:dispatchTouchEvent -> onInterceptTouchEvent -> onTouchEvent

View: dispatchTouchEvent -> onTouch

(2) Call order

Action execution order: action_ DOWN -> ACTION_ MOVE -> ACTION_ UP

ViewGroup: dispatchTouchEvent -> onInterceptTouchEvent -> onTouchEvent()

View: dispatchTouchEvent -> onTouchEvent

Event distribution and delivery sequence: parent view - > child view

Viewgroup1.dispatchtouchevent - > viewgroup2.dispatchtouchevent - > view3.dispatchtouchevent (followed by view3. Ontouchevent)

Event consumption transfer order: child view - > parent view

View3.onTouchEvent -> ViewGroup2.onTouchEvent -> ViewGroup1.onTouchEvent

Personally, I understand that this transfer order is caused by dispatchtransformatedtouchevent. Here is a recursive call, and the entry of the whole event is ViewGroup. Dispatchtouchevent

The above is the article sharing of Android view event distribution and consumption source code. For the distribution mechanism of Android view events, you can search the corresponding articles on this site for extended learning!

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>