Android solves Viewpager sliding problem of double-layer nesting

Source: Internet
Author: User

Before doing one of their own project, encountered the banner picture dynamic switch, I think of the first time is Viewpager, the whole software only part of the advertisement Viewpager fortunately said, but the more complex software problems arise, especially encountered viewpager double-layer nesting problems, Got a lot of information.

Workaround one: Customize Viewpager as the parent Viewpager control

 Public classParentviewpager extends viewpager{Private intchildvpheight=0;  PublicParentviewpager (Context context) {super (context); //TODO auto-generated Constructor stubinit (context); }      PublicParentviewpager (Context context, AttributeSet Attrs) {Super (context, attrs); //TODO auto-generated Constructor stubinit (context); }     Private voidinit (Context context) {//TODO auto-generated Method Stub//Get screen width heightWindowManager WindowManager =(WindowManager) context.getsystemservice (context.        Window_service); intDiswidth =Windowmanager.getdefaultdisplay (). GetWidth (); //The corresponding PX value of the past DP value according to the density of the screenChildvpheight= (int) (Context.getresources (). Getdisplaymetrics (). Density * Diswidth +0.5f); } @Override Publicboolean onintercepttouchevent (Motionevent arg0) {//TODO auto-generated Method Stub//Touch when the child Viewpager is within the page and child Viewpager control height//returns False, at which point the action of the touch is passed to the child Viewpager        if(Getcurrentitem () = =1&& arg0.gety () <childvpheight) {            return false; }        returnsuper.onintercepttouchevent (arg0); }   

Although this method is simple and feasible, but will appear, sub-viewpager if the ScrollView, the sub-viewpager although has been sliding to see where, but the height of the set still can not let the parent Viewpager left and right to slide, Ontouch's action passed through the parent Viewpager to the child control.

Workaround two: Custom Viewpager as child controls

 Public classChildviewpager extends viewpager{/** Touch the point when pressed **/PointF DOWNP=NewPointF (); /** The current point when touching **/PointF curp=NewPointF ();     Onsingletouchlistener Onsingletouchlistener;  PublicChildviewpager (Context context, AttributeSet Attrs) {Super (context, attrs); //TODO auto-generated Constructor stub    }      PublicChildviewpager (Context context) {super (context); //TODO auto-generated Constructor stub} @Override Publicboolean onintercepttouchevent (Motionevent arg0) {//TODO auto-generated Method Stub//returns True when the Intercept touch event reaches this position.//instructions to intercept ontouch in this control, and then execute the ontouchevent of this control        return true; } @Override Publicboolean ontouchevent (Motionevent arg0) {//TODO auto-generated Method Stub//each time the Ontouch event is logged, the current pressed coordinates are recordedcurp.x =Arg0.getx (); CURP.Y=arg0.gety (); if(arg0.getaction () = =Motionevent.action_down) {            //record the coordinates at the time of the press//remember not to use DOWNP = Curp, so that when changing the Curp, DOWNP will also changedownp.x =Arg0.getx (); DOWNP.Y=arg0.gety (); //This code is to inform his father that Viewpager is now doing the operation of this control, do not interfere with my operationGetParent (). Requestdisallowintercepttouchevent (true); }         if(arg0.getaction () = =motionevent.action_move) {            //This code is to inform his father that Viewpager is now doing the operation of this control, do not interfere with my operationGetParent (). Requestdisallowintercepttouchevent (true); }         if(arg0.getaction () = =motionevent.action_up) {            //determine if the coordinates of the press and drop are at a point when up//if it is a point, the Click event will be executed, which is the Click event I wrote myself, not the onclick            if(downp.x==curp.x && downp.y==curp.y)                {Onsingletouch (); return true; }        }         returnsuper.ontouchevent (arg0); }         /** * Click*/     Public voidOnsingletouch () {if(onsingletouchlistener!=NULL) {Onsingletouchlistener.onsingletouch (); }    }     /** * Create Click event interface * @author WANPG **/     Public InterfaceOnsingletouchlistener { Public voidOnsingletouch (); }      Public voidSetonsingletouchlistener (Onsingletouchlistener onsingletouchlistener) { This. Onsingletouchlistener =Onsingletouchlistener; } }

Why do you define Onsingletouch yourself?

Because in Viewpager's ontouchevent I took action on the Ondown, and after the operation, I was unable to continue to pass the touch event down to the onclick and any events of its internal controls, so I made my own judgment, Made a singletouch to achieve the clicked event

Method Two can perfectly solve the problem of the touch sliding of the viewpager after double-layer viewpager nesting

Android solves Viewpager sliding problem of double-layer nesting

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.