How to swipe ViewPager images automatically using TimeTask

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

    Create handler in activity, then schedule a task. I think Handler is enough for this small task. Don't go for timer.

    Runnable timeCounter = new Runnable() {
    
            @Override
            public void run() {
                     if((currentIndex+1)>imageId.length() ){
                          currentIndex=0;
                      }else{
                          currentIndex++;
                     }
                    ViewPager.setCurrentItem(currentIndex);
                    handler.postDelayed(timeCounter, 3*1000);
    
            }
        };
    handler.postDelayed(timeCounter, 3*1000);
    

    then in onDestroy() or where ever you want to stop

    handler.removeCallbacks(timeCounter);
    
    0 讨论(0)
提交回复
热议问题