ViewPager set current page programmatically

后端 未结 3 1654
庸人自扰
庸人自扰 2020-12-17 08:21

I wrote a custom ViewPager to disable Swipe Scroll, but I want to swipe programmatically. I have three Tab in my view pager, but when

3条回答
  •  遥遥无期
    2020-12-17 09:02

    try this :

      viewPager.postDelayed(new Runnable() {
    
        @Override
        public void run() {
            viewPager.setCurrentItem(position);
        }
    }, 10);
    

    sometimes, setCurrentItem on viewpager doesn't work. As pager's content was controlled by a spinner. both the spinners and the pagers state were restored onResume, and because of this the spinners onItemSelected listener was called during the next event propagation cycle.

    By using handler we can make this work because it set the pagers current position after the onItemSelected event fired.

提交回复
热议问题