Android ontouchevent processing mechanism summary (required)

Some touch events will always be used in the project. Each time you use Baidu's various materials, look at the analysis notes of various great gods. This time, I will summarize some knowledge points about touch events. First, it can make yourself more impressed by the touch event and make a reference for future projects. The hardest thing to understand is the ontouchevent method.

1、 Overview

1. Only view, ViewGroup and activity can distribute and consume events.

2. Because the touch event is first contacted on the activity, the activity has no event interception method. That is, there is no dispatchtouchevent method.

3. For views that cannot add child controls, events cannot be distributed and intercepted. It only has ontouchevent events.

2、 Three methods

1.public boolean dispatchTouchEvent(MotionEvent ev)

When a touch event occurs, it will first be distributed by the current activity, that is, the dispatchtouchevent method of the current activity will be executed.

At this time, the method has three return situations:

Return false: indicates that the event will not be distributed. The event will be passed to the ontouchevent method of the upper view or activity in a bubbling manner for consumption.

Return true: indicates that the time has been processed. The event will be consumed by the dispatchtouchevent of the current view or activity. No more delivery, and the event is over.

Return super. Dispatchtouchevent (EV): indicates that the event will be distributed. At this time, the oninterceptertouchevent method of the current view will capture the event and judge whether to intercept the event.

2.public boolean onInterceptTouchEvent(MotionEvent ev)

This method is used by the user to intercept the passed events and judge whether the passed events need to be processed by the current view.

Return false: release the event without intercepting it. The event will be passed to the child control of the current view and distributed by the dispatchtouchevent method in the child control.

Return true: intercept the event and hand it over to the ontouchevent method of the current view for processing.

Return super.inintercepttouchevent (EV): the default interception method is the same as return true. The event will be intercepted and handed over to the ontouchevent method of the current view for processing. (it should be noted here that when there are two views. There is a B view in a view. Click A. if onintercepttouchevent() returns super.intercepttouchevent (EV), the event will be intercepted by a and handed over to a's ontouchevent() for processing. If you click B, if onintercepttouchevent() returns super.intercepttouchevent (EV) in a , the event will not be intercepted and will be distributed to child controls)

3.public boolean onTouchEvent(MotionEvent event)

If the current view intercepts the event, the event will be passed to the method

Return false: indicates that the event is not consumed. The event will be passed to the ontouchevent event in the upper view or activity in a bubbling manner. If the ontouchevent in the top view or activity still returns false. The event will disappear. The next series of events will be captured directly by the ontouchevent method of the upper layer

Return true: indicates that the event has been consumed. This is the end of the event.

Return super.ontouchevent (event): by default, it is the same as return false.

verification:

Several methods in mainactivity fatherview childview return super. * * * * * touchevent (EV)

analysis:

1. When clicking on the screen. The dispatchtouchevent method in mainactivity executes first and prints mainactivity dispatchtouchevent -- > action_ DOWN

2. Because super.dispatchtouchevent (EV) is returned, the event EV will be distributed, but there is no onintercepttouchevent() method in mainactivity, so the event is passed to the dispatchtouchevent method in fatherview. Print fatherview dispatchtouchevent -- > action_ DOWN

3. In fatherview, dispatchtouchevent returns super. Dispatchtouchevent (EV), and all events will be distributed. The method in onintercepttoucheven() in fatherview is executed. Print fatherview onintercepttoucheven -- > action_ DOWN

4. Onintercepttoucheven() in fatherview returns super. Onintercepttouchevent (EV). Here, (1) if you click childview on the screen. The event will not be intercepted and will be passed to the dispatchtouchevent method in childview. (2) If the value fatherview is clicked, the event will be intercepted. The ontouchevent () method in fatherview will be executed. Take (1) as an example, childview dispatchtouchevent -- > action will be printed_ DOWN。

5. The dispatchtouchevent in childview returns super. Dispatchtouchevent (EV), and all events will be distributed. Print childview onintercepttouchevent -- > action_ DOWN。

6. At this time, the onintercepttouchevent in childview returns super.onintercepttouchevent (EV), and there are no child controls, so the event will be intercepted. Print childview ontouchevent -- > action_ DOWN。

7. In childview, the return value of ontouchevent() is super. Ontouchevent (EV). The event will not be consumed, but will be passed to ontouchevent () in the upper space in a bubbling way, where ontouchevent in the upper space returns super.ontouchevent (EV). So let's talk about Printing father ontouchevent -- > action once_ DOWN。 MainActivty-onTouchEvent-->ACTION_ DOWN。

8. Subsequent event actions will no longer be distributed to child views by mainactivity, but will be directly consumed by ontouchevent in mainactivity. Print mainactivity dispatchtouchevent -- > action_ UP,MainActivty-onTouchEvent-->ACTION_ UP

code

MainActivity.java

activity_ main.xml

The above summary of Android ontouchevent processing mechanism is all the content shared by Xiaobian. I hope it can give you a reference and support more 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
分享
二维码
< <上一篇
下一篇>>