How to swipe ViewPager images automatically using TimeTask

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

    try this code: In MainActivity -

    int currentIndex=0; //for tracking current item 
    

    Create and set your TimerTask as per your requirement then in run() of TimerTask:

    public void run() {
        if((currentIndex+1)>imageId.length() ){
          currentIndex=0;
        }else{
          currentIndex++;
        }
        ViewPager.setCurrentItem(currentIndex);
    }
    
    0 讨论(0)
  • 2020-12-25 14:44

    a simple edit to @L. Swifter's code snippet for the ones wondering about variables, I wrapped it all in a method which you can add to your activity after setting the adapter

    private void automateViewPagerSwiping() {
        final long DELAY_MS = 500;//delay in milliseconds before task is to be executed
        final long PERIOD_MS = 3000; // time in milliseconds between successive task executions.
        final Handler handler = new Handler();
        final Runnable update = new Runnable() {
            public void run() {
                if (viewPager.getCurrentItem() == adapter.getCount() - 1) { //adapter is your custom ViewPager's adapter
                    viewPager.setCurrentItem(0);
                }
                else {
                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
                }
            }
        };
    
        timer = new Timer(); // This will create a new Thread
        timer.schedule(new TimerTask() { // task to be scheduled
            @Override
            public void run() {
                handler.post(update);
            }
        }, DELAY_MS, PERIOD_MS);
    }
    
    0 讨论(0)
  • 2020-12-25 14:44

    You can use android TabLayout for indicators, ViewPager for slide screen and TimerTask to slide automatic

    please check this link for step by step guideline and demo

    CLICK HERE

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-25 14:53

    Here is the total code using TimerTask:

    public class MainActivity extends AppCompatActivity {
    
    ViewPager viewPager;
    
    Integer[] imageId = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4};
    String[] imagesName = {"image1","image2","image3","image4"};
    
    Timer timer;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
    
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        PagerAdapter adapter = new CustomAdapter(MainActivity.this,imageId,imagesName);
        viewPager.setAdapter(adapter);
    
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                viewPager.post(new Runnable(){
    
                    @Override
                    public void run() {
                        viewPager.setCurrentItem((viewPager.getCurrentItem()+1)%imageId.length);
                    }
                });
            }
        };
        timer = new Timer();
        timer.schedule(timerTask, 3000, 3000);
    }
    
    @Override
    protected void onDestroy() {
        timer.cancel();
        super.onDestroy();
    }
    }
    
    0 讨论(0)
提交回复
热议问题