Slideshow in android viewpager?

后端 未结 2 671
孤城傲影
孤城傲影 2020-12-29 15:26

I have problem with android viewpager slideshow. I want to show the viewpager layout after a minimum time period. Here is my code sample. My main class: <

相关标签:
2条回答
  • 2020-12-29 16:07

    Use Handler to make it auto slide. Here is some code

    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        public void run() {
                if( position >= 4){
                    position = 0;
                }else{
                    position = position+1;
                }
                toBarPager.setCurrentItem(position, true);
                handler.postDelayed(runnable, 10000);
        }
    };
    

    And don't forget to remove callback onPause()

    @Override
    public void onPause() {
        super.onPause();
        if (handler!= null) {
            handler.removeCallbacks(runnable);
        }
    }
    

    Also, don't forget to rerun onResume()

    @Override
    public void onResume() {
        super.onResume();  // Always call the superclass method first
        handler.postDelayed(runnable, 10000);
    }
    
    0 讨论(0)
  • 2020-12-29 16:09

    use Handler and send[Empty]MessageDelayed/postDelayed and call setCurrentItem on your ViewPager

    0 讨论(0)
提交回复
热议问题