14

I have a simple ViewPager. Is there any possibilities programmatically scroll it every five seconds with usual animation?

1 Answer 1

34

Take a look at ViewPager.setCurrentItem(int) and combine it with a TimerTask or a Handler.

Example:

final ViewPager viewPager = ...; final Handler h = new Handler(Looper.getMainLooper()); final Runnable r = new Runnable() { public void run() { viewPager.setCurrentItem(0, true); h.postDelayed(r, 5000); } }; h.postDelayed(r, 5000); 

Be sure to cancel the runnable when appropriate.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! I did't think, that it is so easy. Only one correction: you should call ViewPager.setCurrentItem(int, boolean)
Updated answer with code. Be sure you understand what is going on, it is easy to leak things.
you must change h.postDelayed(r, 5000); to h.postDelayed(this, 5000);
In the line h.postDelayed(r, 5000); in the method run() is "r" not accessible. You could change it to h.postDelayed(this, 5000);, because it's a method in your runnable (which you want to access).
This is not working for me am using VerticalPageViewer
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.