onPageSelected doesn't work for first page

前端 未结 10 1996
[愿得一人]
[愿得一人] 2021-01-30 12:30

My pager adapter ( extends PagerAdepter ) has a textview in it. And I update this textview with MainActivity\'s onPageSelected . Its update textview for position > 0 , but start

10条回答
  •  梦如初夏
    2021-01-30 13:12

    For me work next code

        viewPager.addOnPageChangeListener(myOnPageChangeListener);
    

    .

        ViewPager.OnPageChangeListener myOnPageChangeListener = new ViewPager.OnPageChangeListener() {
    
        //declare key
        Boolean first = true;
    
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (first && positionOffset == 0 && positionOffsetPixels == 0){
                onPageSelected(0);
                first = false;
            }
        }
    
        @Override
        public void onPageSelected(int position) {
            //do what need
        }
    
        @Override
        public void onPageScrollStateChanged(int state) {
        }
    };
    

提交回复
热议问题