Initial understanding of Android touch event delivery mechanism

preface

A knowledge point summarized today is the view event transmission mechanism in andorid, which is also the core knowledge point. I believe many developers will feel confused when facing this problem. In addition, another difficult problem of view sliding conflict, such as nesting listviews in Scrollview, is sliding up and down. How to solve it? It is based on the view event transmission mechanism, Therefore, developers need to have a deeper understanding of the event delivery mechanism of view.

catalogue

Relationship among activity, view and ViewGroup

We all know that many of the pages seen in Android are activity components, and then nested controls in the activity, such as textview and relativelayout layout. In fact, the base class of these controls is the abstract class view, and ViewGroup is also a subclass of view. The difference is that ViewGroup can be used as a container for other subclasses. A diagram is as follows:

In short, the carrier of these view controls is activity, which draws from decorview.

Touch event type

ACTION_ Down: the user's finger pressing operation often represents the beginning of a touch event. ACTION_ Move: the user's finger moves on the screen. Generally, a slight movement will trigger a series of movement events. ACTION_ POINTER_ Down: additional finger press operation. ACTION_ POINTER_ Up: extra finger leaving operation action_ Up: the operation of the user's finger leaving the screen. A lifting operation marks the end of a touch event.

In one screen touch operation, action_ Down and action_ Up is required, action_ Move depends on the situation. If you just click, only press and lift operations are detected.

Three stages of event delivery

Dispatch: the event distribution corresponds to the dispatchtouchevent method. In the andorid system, all touch events are distributed through this method.

java boolean dispatchTouchEvent (MotionEvent ev)

In this method, you can decide to consume the event directly or continue to distribute the event to the child view for processing.

Intercept: event interception corresponds to onintercepttouchevent method. This method exists only in ViewGroup and its subclasses, but not in view and activity.

java boolean onInterceptTouchEvent (MotionEvent ev)

This method is used to determine whether to intercept an event. If an event is intercepted, this method will not be called again in the same sequence of events.

Consumption: event consumption corresponds to the ontouchevent method.

java boolean onTouchEvent (MotionEvent event)

It is used to process click events. The returned result indicates whether the current event is consumed. If not, the current view can no longer receive events in the same event sequence

In the Android system, there are three types of with event transfer and processing capabilities:

Activity: it has two methods: dispatchtouchevent and ontouchevent. ViewGroup: it has three methods: dispatchtouchevent, onintercepttouchevent and ontouchevent. View: it has two methods: dispatchtouchevent and ontouchevent.

View event delivery mechanism

The view here refers to view controls other than ViewGroup, such as textview, button Check@R_ 102_ 2419 @ etc. the view control itself is the smallest unit and cannot be used as a container for other views. View has two methods: dispatchtouchevent and ontouchevent. Therefore, a class mytextview inheriting textview is defined here. View the log through code and see how the process goes.

At the same time, a mainactivity class is defined to display mytextview. In this activity, we set click onclick and ontouch listening for mytextview to facilitate tracking and understanding the process of event delivery.

View results:

It can be seen that the events are executed in the order of down move up. The ontouch method takes precedence over the onclick method. If they are passed in the super method, the final result is consumed in the ontouchevent method of mytextview. If they are not consumed, the event will be returned to its parent for consumption. If the parent does not consume, It will eventually be returned to the activity for processing.

ViewGroup event delivery mechanism

ViewGroup exists as the container of view control. ViewGroup has three methods: dispatchtouchevent, onintercepttouchevent and ontouchevent. Similarly, we customize a ViewGroup, which inherits from relativelayout and implements a myrelativelayout.

View results:

It can be seen that the transfer order of touch events is also from activity to ViewGroup, and then recursively passed by ViewGroup to its child views. The ViewGroup intercepts the event through the onintercepttouchevent method. If the method returns true, the event will not continue to be passed to the child view. If false or super.onintercepttouchevent is returned, the event will continue to be passed to the child view. After consuming an event in a child view, the ViewGroup will not receive any event.

Summary

In Android system events, the pseudo codes of view and ViewGroup are as follows:

Three diagrams are used to represent the process of touch mechanism in Android.

1. Touch events in view are not consumed

2. Touch event consumption in view

3. ViewGroup intercepts touch events

Some conclusions:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>