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