Android Viewpager Modify the sliding speed

Source: Internet
Author: User

In Android, people who have used Viewpager know that if we use Viewpager to slide, we can do it by swiping the finger, but if it is done by the Setcurrentitem function, You will see that there is no over-animation effect, but rather a direct flash of the past, then at some point we will encounter a demand, for example, we want to use the Setcurrentitem function to do viewpager sliding, and need to have excessive sliding animation, then how to do it? First, we take a general look at the source code of Viewpager:

First of all, let's see what Setcurrentitem is doing.

We continue to look at the setcurrentiteminternal function,

Continue to look at the contents of the Setcurrentiteminternal function,

See this a big lump of code is not a bit of panic, don't be afraid, we actually see this function is enough scrolltoitem, and then we see this function specifically to do is:

This function, in fact, our main concern is the Smoothscrollto function is good, we specifically look at this function,

Then in this function, we actually see the actual execution of the slide in a word, that is mscroller.startscroll (SX, sy, DX, dy, duration);

So we can see that it is mscroller this object to slide, then, the simplest way is to rewrite the class, and how to pay viewpager after rewriting, this is the next, which uses the Java reflection mechanism, we look at:

public class Fixedspeedscroller extends Scroller {private int mduration = 1500;        Public Fixedspeedscroller (Context context) {    Super (context); } public Fix    Edspeedscroller (context context, Interpolator Interpolator) {Super (context, interpolator);    } @Override  public void Startscroll (int startX, int starty, int dx, int dy, int. duration) {//Ignore received duration, use    Fixed one instead super.startscroll (StartX, starty, dx, DY, mduration);    } @Override public void Startscroll (int startX, int starty, int dx, int DY) {//Ignore received duration, use fixed one instead super.startscroll (StartX, starty, dx, DY, mduratio    n); } public Voi    d setmduration (int time) {mduration = time;  } public int    Getmduration () {return mduration; }}

This is our rewrite of the Scroller class, in fact, it is relatively simple, we just change the original sliding method, sliding the specific time, instead of our own set, the parameters passed can be voided. In this way, we can control the speed of the slide by customizing the sliding time. Then the next step is how to assign it to the original Viewpager class, here, the use of the Java reflection mechanism, we look at the specific code:

try {    Field field = ViewPager.class.getDeclaredField ("Mscroller");    Field.setaccessible (true);    Fixedspeedscroller scroller = new Fixedspeedscroller (Mviewpager.getcontext (),    new Accelerateinterpolator ());    Field.set (Mviewpager, scroller);    Scroller.setmduration (2000);} catch (Exception e) {     }

It is through the mechanism of reflection that our custom scroller are passed in, thus realizing our own control of the sliding speed.

Android Viewpager Modify the sliding speed

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.