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