Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager programmatically scrolling

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

like image 910
Geralt_Encore Avatar asked May 15 '12 14:05

Geralt_Encore


People also ask

How to scroll ViewPager programmatically?

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.

How do I configure 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 .

How do I stop ViewPager from sliding on Android?

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.


1 Answers

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.

like image 163
Jon Willis Avatar answered Oct 03 '22 03:10

Jon Willis