Event transfer between android controls
Public boolean dispatchTouchEvent (MotionEvent ev ){}
For event distribution, all events in Android must be distributed through this method, and then decide whether to consume the current event or continue distributing it to the child Control for processing. If the return value is true, the distribution is not resumed and the event is not consumed.
Public boolean onInterceptTouchEvent (MotionEvent arg0 ){}
It is used for event processing. If true is returned, the current event is processed by consumption. If false is returned, the event is not processed. The event is handed over to the subcontrol for further distribution.
Public boolean onTouchEvent (MotionEvent arg0 ){}
Responsible for event interception. If true is returned, the system intercepts the current event and does not distribute the event to its onTouchEvent for processing. If the return value is false, the request is not blocked.
An example is provided to illustrate the transmission relationship between the three methods. The interface is shown below:
The transfer relationship is as follows:
For example, Android Viewpage does not allow you to slide the screen (if other views are available, you can customize the controls, and then rewrite these methods)
The Android event mechanism is passed from the parent View to the Child View. You can check whether your current child View has a slidable control and so on to determine whether the event is intercepted. However, this is troublesome, in addition, it cannot solve all the problems (you must check whether the touch point is on the Control). In fact, there is a simple method to inject ViewPager instances into your nested controls, and then inject them in onTouchEvent, onInterceptTouchEvent, which tells the parent View in dispatchTouchEvent, that is, ViewPager does not intercept touch events on the control.
RequestDisallowInterceptTouchEvent (true); true indicates disabled.
The code added to the sub-view is generally as follows:
Public boolean onTouch (View v, MotionEvent event ){
Switch (event. getAction ()){
Case MotionEvent. ACTION_MOVE:
RequestDisallowInterceptTouchEvent (true );
Break;
Case MotionEvent. ACTION_UP:
Case MotionEvent. ACTION_CANCEL:
RequestDisallowInterceptTouchEvent (false );
Break;
}
}
When the user presses, we tell the parent component not to intercept my events (the Child component can normally respond to events at this time). After the sub-component is picked up, it will tell the parent component that it can be blocked.