How to swipe ViewPager images automatically using TimeTask

后端 未结 13 2257
天涯浪人
天涯浪人 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:47

    Another version of the answer:-

                        private int currentPage = -1;
                        // start auto scroll of viewpager
                        final Handler handler = new Handler();
                        final Runnable Update = new Runnable() {
                            public void run() {
                                viewPager.setCurrentItem(++currentPage, true);
                                // go to initial page i.e. position 0
                                if (currentPage == NUM_PAGES -1) {
                                    currentPage = -1;
                                    // ++currentPage will make currentPage = 0
                                }
                            }
                        };
    
                        timer = new Timer(); // This will create a new Thread
                        timer.schedule(new TimerTask() { // task to be scheduled
    
                            @Override
                            public void run() {
                                handler.post(Update);
                            }
                        }, 500, 1500);
    

提交回复
热议问题