I have a simple ViewPager. Is there any possibilities programmatically scroll it every five seconds with usual animation?
1 Answer
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.
6 Comments
Geralt_Encore
Thanks! I did't think, that it is so easy. Only one correction: you should call ViewPager.setCurrentItem(int, boolean)
Jon Willis
Updated answer with code. Be sure you understand what is going on, it is easy to leak things.
Yousef Zakher
you must change h.postDelayed(r, 5000); to h.postDelayed(this, 5000);
Osiriz
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).Sarath Sadasivan Pillai
This is not working for me am using VerticalPageViewer
|