How to swipe ViewPager images automatically using TimeTask

后端 未结 13 2258
天涯浪人
天涯浪人 2020-12-25 13:54

Hi I am new in android and in my app I have a lot of images in my ArrayList that \'s why I want to swipe those images automatically for every 3 seconds with he

13条回答
  •  一个人的身影
    2020-12-25 14:48

    Try this in your onCreate() method

        final Handler handler = new Handler();
        Timer timer = new Timer();
        final Runnable runnable = new Runnable() {
            public void run() {
                int currentPage=viewPager.getCurrentItem();
                //return to first page, if current page is last page  
                if (currentPage == titleNames.length-1) {
                    currentPage = -1;
                }
                viewPager.setCurrentItem(++currentPage, true);
            }
        };
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(runnable);
            }
        },DELAY,PERRIOD)
    

提交回复
热议问题