Viewpager slide half of the judging method and left slip right slide judgment

Source: Internet
Author: User

When doing the project, will encounter with Viewpager + fragments to achieve multi-page sliding. Sometimes it needs to be done: when the interface is sliding halfway or more than half, you need to switch the title to the next page. This time just rely on Viewpager interface is not judged, here is a method: To determine whether the Viewpager is left or right slide, at the same time on the slide over half the time (this parameter can be adjusted), to carry out related operations.

  //define two variables, this two variable is to indicate the sliding time, Positionoffset is from large to//small changes, or changes from the smallest to the largest. Used to avoid calling a method multiple times while sliding  Private BooleanIssmalltobig =true; Private BooleanIsbigtosmall =false; //record the Positionoffsetpixels value of the last slide   Private intLastvalue =-1; @Override Public voidOnpagescrolled (intPositionfloatPositionoffset,intpositionoffsetpixels) {        if(Positionoffset! = 0) {Mcurrentpage=position; BooleanIsleft =true; if(Lastvalue >=positionoffsetpixels) {                //Right SlideIsleft =false; } Else if(Lastvalue <positionoffsetpixels) {                //Left SlideIsleft =true;        } setindiactorview (Positionoffset,isleft); } lastvalue=Positionoffsetpixels; }    Private voidSetindiactorview (floatPositionoffset,Booleanisleft) {              //Set the sliding boundary, where the slide is set to half        floatRadiusoffsethead = 0.5f; //if the slide is less than half, avoid multiple calls by Issmalltobig this variable//Initindicatorview Method        if(Positionoffset <= radiusoffsethead &&!Issmalltobig)            {Initindicatorview (mcurrentpage); Issmalltobig=true; Isbigtosmall=false; } Else if(Positionoffset > Radiusoffsethead &&!)Isbigtosmall) {          //more than half. If it's left-slip, it's-1, if it's right, it's +1 .            intCurrnt = Isleft? Mcurrentpage + 1:mcurrentpage-1;            Initindicatorview (currnt); Isbigtosmall=true; Issmalltobig=false; }    }

Viewpager slide half of the judging method and left slip right slide judgment

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.