Event transfer between android controls

Source: Internet
Author: User

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.


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.