Summary of android event handling mechanism

Delivery of onTouchEvent

 

When there are multiple levels of View, the action is passed down until the deepest View is encountered, if the parent level allows it. Therefore, the touch event first calls the onTouchEvent of the bottom View. If the onTouchEvent of the View receives a touch action and handles it accordingly, there are two return methods: return true and return false; return true will tell the system the current View This touch event needs to be handled. The ACTION_MOVE and ACTION_UP issued by the system in the future still need to continue to be monitored and received, and this action has been processed. It is impossible for the parent view to trigger onTouchEvent. So each action can only have at most one onTouchEvent interface returning true. If it returns false, the system will be notified that the current View does not care about the touch event this time. At this time, the action will be passed to the parent and the onTouchEvent of the parent View will be called. However, any action issued after this touch event will not be accepted by the View, and onTouchEvent will never be triggered in this touch event. That is to say, once the View returns false, then ACTION_MOVE, ACTION_UP and other ACTIONs will follow. The View will not be passed in, but the action of the next touch event will still be passed in.

 

The onInterceptTouchEvent interception of the parent layer

 

As mentioned earlier, there is a prerequisite for the underlying View to receive this event: if the parent level allows it. Assuming that the dispatch method of the parent level is not changed, the onInterceptTouchEvent method of the parent View will be called before the system calls the underlying onTouchEvent to determine whether the parent View will intercept the action after the touch event. If onInterceptTouchEvent returns true, then all actions after this touch event will not be passed to the deep View, but will all be passed to the onTouchEvent of the parent View, which means that the parent has intercepted this touch event, and subsequent actions There is also no need to ask onInterceptTouchEvent. OnInterceptTouchEvent will not be called again when the action is issued after this touch event until the next touch event. If onInterceptTouchEvent returns false, then this action will be sent to a deeper View, and each subsequent action will ask the parent layer's onInterceptTouchEvent if it needs to intercept this touch event. Only the ViewGroup has the onInterceptTouchEvent method, because an ordinary View must be the deepest View, and the touch event can be passed here is the last stop, and the View's onTouchEvent will definitely be called.

 

底层View的getParent().requestDisallowInterceptTouchEvent(true)

 

For the underlying View, there is a way to prevent the parent's View from intercepting touch events, which is to call the getParent().requestDisallowInterceptTouchEvent(true); method. Once the underlying View calls this method after receiving the touch action, the parent View will no longer call onInterceptTouchEvent, and will not be able to intercept subsequent actions (if the parent ViewGroup and the bottommost View need to intercept touches of different focus or different gestures, Can't use this write dead).

 

//Notify the parent ViewGroup, you can't intercept
public boolean dispatchTouchEvent(MotionEvent ev) {   
    getParent().requestDisallowInterceptTouchEvent(true);  
    return super.dispatchTouchEvent(ev);    
}  

//It can also be written like this. When the user presses, we tell the parent component not to intercept my event (the child component can respond to the event normally at this time), and after picking it up, it will tell the parent component that it can be blocked.
public boolean onTouch(View v, MotionEvent event) {  
   switch (event.getAction()) {  
   case MotionEvent.ACTION_MOVE:   
       pager.requestDisallowInterceptTouchEvent(true);  
       break;  
   case MotionEvent.ACTION_UP:  
   case MotionEvent.ACTION_CANCEL:  
       pager.requestDisallowInterceptTouchEvent(false);  
       break;  
}

 

Original link: http://www.jianshu.com/p/a2185e4b1b53

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327032472&siteId=291194637