Rewrite the dispatchtouchevent, and GetParent (). Requestdisallowintercepttouchevent (True) when pressed.
Determine the sliding direction, the absolute value of sliding in the X-and y-axis who is big is sliding in what direction
One, Slide getParent () in the vertical direction. Requestdisallowintercepttouchevent (FALSE);
Two, horizontal direction sliding
2.1, when sliding to the No. 0 page of Viewpager, and is sliding from left to right getParent (). Requestdisallowintercepttouchevent (false);
2.2, when sliding to the last page of Viewpager, and is sliding from right to left getParent (). Requestdisallowintercepttouchevent (false); 2.3, other getParent (). Requestdisallowintercepttouchevent (True);
Public classHorizontalscrollviewpagerextendsViewpager { PublicHorizontalscrollviewpager (Context context) {Super(context); } PublicHorizontalscrollviewpager (Context context, AttributeSet attrs) {Super(context, attrs); } /*** Starting coordinates*/ Private floatStartX; Private floatStarty; @Override Public Booleandispatchtouchevent (motionevent ev) {Switch(Ev.getaction ()) { CaseMotionevent.action_down://requests that the parent layer view does not intercept the current control's eventsGetParent (). Requestdisallowintercepttouchevent (true);//passes events to the current control (Horizontalscrollviewpager)//1. Record the starting coordinatesStartX =Ev.getx (); Starty=ev.gety (); Break; CaseMotionevent.action_move://2. Come to the new coordinates floatEndX =Ev.getx (); floatEndY =ev.gety (); //3. Calculate the offset floatDistancex = EndX-StartX; floatDistancey = EndY-Starty; //4. Determine the direction of the slide if(Math.Abs (Distancex) >Math.Abs (Distancey)) { //Slide Horizontally//2.1, when sliding to the No. 0 page of Viewpager, and is sliding from left to right//getParent (). Requestdisallowintercepttouchevent (false); if(Getcurrentitem () ==0&&distancex >0) {getParent (). Requestdisallowintercepttouchevent (false); }//2.2, when sliding to the last page of Viewpager, and is sliding from right to left//getParent (). Requestdisallowintercepttouchevent (false); Else if((getcurrentitem () = = (Getadapter (). GetCount ()-1)) && Distancex <0) {getParent (). Requestdisallowintercepttouchevent (false); }//2.3, other, middle section//getParent (). Requestdisallowintercepttouchevent (true); Else{getParent (). Requestdisallowintercepttouchevent (true); } }Else{ //slide in the vertical directionGetParent (). Requestdisallowintercepttouchevent (false); } Break; Casemotionevent.action_up: Break; default: Break; } return Super. dispatchtouchevent (EV); }}
Silicon Valley News 5--Top News Carousel Event handling