The Android event distribution mechanism makes you want to like it easily.

I have been preparing for the interview recently. Have reviewed a lot. Use the simplest vernacular to express the new knowledge points learned from the past. One is to record. Second, if it can help everyone, better

1. Three important methods of event distribution

First of all, I randomly built a ViewGroup, the three important methods are

  • dispatchTouchEvent
  • onInterceptTouchEvent
  • onTouchEvent
public class MyViewGroup extends ViewGroup {
    
    
    //...省略部分代码
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
    
    
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
    
    
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    
    
        return super.onTouchEvent(event);
    }
}

Then give a brief description of these 3 methods

method effect Call time
dispathTouchEvent() Distribute (deliver) click events When the click event can be passed to the current View, the method will be called
onInterceptTouchEvent () Determine whether an event is intercepted (only in ViewGroup, normal View does not have this method) Called inside dispathTouchEvent()
onTouchEvent () Handling click events Called inside dispathTouchEvent()

The order of event distribution is Activity(Window) --> ViewGroup --> View


2. Enlarge the move and analyze the event distribution process.


In the picture:

  • super: call the parent method
  • true: consumption event, that is, the event does not continue to pass down
  • false: the event is not consumed, and the event does not continue to be passed down / handed over to the parent control onTouchEvent() for processing

Look at the above figure and the analysis is as follows. There are 3 steps in total:


Step 1 (in Activity):

Event first dispatchTouchEvent() in Activity
returns value:
true / false -------> consumption event (here false is also consumption event)
super -------------> event is issued Go to ViewGroup and call dispatchTouchEvent() of ViewGroup


Step 2 (in ViewGroup)

The dispatchTouchEvent()
return value of the event in the ViewGroup :
true --------------> Consumption event
false -------------> No event is consumed, no more Send it to the onTouchEvent() of the parent control to handle
super -------------> The event is sent to the onInterceptTouchEvent() of the current ViewGroup

The
return value of the event onInterceptTouchEvent() in the ViewGroup :
true --------------> The event is delivered to the onTouchEvent() of the current ViewGroup.
false / super -----> The event is delivered to Child View, call dispatchTouchEvent() of the child View

The
return value of onTouchEvent() of the event in the ViewGroup :
true --------------> Consumption event
false / super -----> No event is consumed, nor sent, and handed over to the parent control OnTouchEvent() handling


Step 3 (in the child View)

The dispatchTouchEvent()
return value of the event in the View :
true --------------> Consumption event
false -------------> No event is consumed, not down Send it to the onTouchEvent() of the parent control to handle
super -------------> The event is sent to the onTouchEvent() of the current View

The onTouchEvent()
return value of the event in the View :
true --------------> Consumption event
false / super -----> Do not consume the event, nor send it to the parent control OnTouchEvent() handling


After reading these analyses, you can come to the event distribution mechanism casually! ! 3 steps overview. Are you excited to like it?

Guess you like

Origin blog.csdn.net/leol_2/article/details/102798906