I have a simple ViewPager
. Is there any possibilities programmatically scroll it every five seconds with usual animation?
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.
You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .
To enable / disable the swiping, just overide two methods: onTouchEvent and onInterceptTouchEvent . Both will return "false" if the paging was disabled. You just need to call the setPagingEnabled method with false and users won't be able to swipe to paginate.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With