Android Sliding conflict handling

Source: Internet
Author: User

To solve the sliding conflict, you have to understand the Android event distribution mechanism well. If you do not know about the Android event distribution mechanism, please study the resources first.

There are generally 2 ways

1 External Interception method

This is very simple, because the event is distributed from the parent view to the child view, so we can override the parent control's onintercepttouchevent, if the parent container needs an event to intercept it, and if it does not need to intercept the child view processing.

The pseudo code is as follows

 Public Booleanonintercepttouchevent (Motionevent event) {BooleanIntercept =false; intx = (int) Event.getx (); inty = (int) event.gety (); Switch(Event.getaction ()) { CaseMotionEvent.ACTION_DOWN:intercept=false;  Break;  CaseMotionevent.action_move:if(This event is required by the parent container) {Intercept=true; } Else{intercept=false; }             Break;  CaseMotionEvent.ACTION_UP:intercept=false;  Break; } mlastx=x; Mlasty=y; returnintercept;}

One thing to note here is that the parent container cannot intercept the Action_down event, because once the parent container intercepts the Action_down event, all subsequent events are handled by it, and the child view receives no events.

2 Internal interception method

The parent container does not intercept any events, all events are passed to child elements, and if the child elements need this event to be consumed directly, otherwise the parent container will be disposed of, and the parent container must be requestdisallowintercepttouchevent to intervene to intercept the event.

The pseudo code is as follows

To rewrite the dispatchtouchevent of a child view

 Public Booleandispatchtouchevent (Motionevent event) {intx = (int) Event.getx (); inty = (int) event.gety (); Switch(Event.getaction ()) { CaseMotionEvent.ACTION_DOWN:parent.requestDisallowInterceptTouchEvent (true);  Break;  CaseMotionevent.action_move:intDeltaX = x-Mlastx; intDeltaY = y-Mlasty; if(This event is required by the parent container) {parent.requestdisallowintercepttouchevent (false); }             Break; } mlastx=x; Mlasty=y; return Super. Dispatchtouchevent (event);//After intervention, throw it to the parent container}

Two to rewrite the parent container's onintercepttouchevent to let the parent container by default intercept all events except Action_down

 Public Boolean onintercepttouchevent (Motionevent event) {    if (event.getaction () = = Motionevent.action_ Down) {        returnfalse;     Else {        returntrue;    }}

The internal interception method is not very good to understand, more Rao.

The external interception method is consistent with the event distribution mechanism, so it is easy to understand, so the external interception method will be preferred in the general case.

Android Sliding conflict handling

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.