1. What is event distribution

Most IT practitioners who have done Android or even UI development have come into contact with this term. As the name implies, IT is the distribution of a series of events. Here we will discuss the distribution mechanism of touch events on Android in detail.

2. Why do we fry cold rice?

Android event distribution, Baidu caught a lot of, why do I have to write this blog? This is a good question. I have read a lot of relevant blog posts and the explanation of “event distribution” in relevant books. However, I may not have been able to understand it thoroughly before, and some articles did not give me a detailed understanding of the reason.

  1. Why is the subview not thereMotionEvent.ACTION_DOWNIf the event returns true, all subsequent events cannot be processed, right?
  2. Why is it that onceviewClick events are consumed, and all subsequent events in the series are determined by theviewConsumed?
  3. On the basis of two, whyView#requestDisallowInterptTouchEvent(false)And hand over control of events?

3. The analysis

I checked the source code for ViewGroup#dispatchTouchEvent(MotionEvent ev) with the above two questions in mind.

First take a look at some of the source code:

onInterceptTouchEvent(ev)
ACTION_DOWN
mFirstTouchTarget ! = null
intercepted = true
mFirstTouchTarget == null
mFirstTouchTarget

Then look at:

MotionEvent.ACTION_DOWN,MotionEvent.ACTION_POINTER_DOWN, MotionEvent.ACTION_HOVER_MOVE
dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)
break
The for loop
newTouchTarget = addTouchTarget(child, idBitsToAssign);
addTouchTarget
mFirstTouchTarget

So what’s the use? Then look at the logic outside the loop:

! =null
return handled

4 summarizes

So now we have a reasonable explanation for problem number one. ACTION_DOWN mFirstTouchTarget is null after the MotionEvent.ACTION_DOWN processing is completed, and then the move of the series of events is distributed. The if check on lines 2507 and 2508 in Figure 1 goes to line 2519 intercepted = true always intercepting the event and not distributing it. As for question 2 and question 3, most of them are actually covered in the above analysis, so you can summarize them yourself

recommended

Family recommended the use of Ali cloud server, affordable and stable with preferential gift package: Ali cloud air tickets

The end of the

My talent and learning is shallow, if there is insufficient, or the place of gibberish, hope haihan and try to be corrected.