onPageSelected isn't triggered when calling setCurrentItem(0)

后端 未结 10 1709
梦如初夏
梦如初夏 2020-12-22 20:13

I have an Activity with a ViewPager which displays a bunch of pictures. When it starts the ViewPager\'s position is set based on what the user selected in a previous Activit

相关标签:
10条回答
  • 2020-12-22 20:21

    The following solution seems to work for me. i.e. I get a callback at position 0 when the viewpager is first loaded and for all subsequent selections either from user scrolling or a setCurrentItem(x) call. I haven't observed any undesired behaviour.

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
                    if (positionOffsetPixels == 0) {
                     //Do something on selected page at position
                    }
    
                }
    
                @Override
                public void onPageSelected(int position) {}
    
                @Override
                public void onPageScrollStateChanged(int state) {}
            });
    
    0 讨论(0)
  • 2020-12-22 20:21

    The simplest solution will be using of function e.g.

        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    
            @Override
            public void onPageScrollStateChanged(int state) {}
    
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
    
            @Override
            public void onPageSelected(int position) {
                onPage(position);
            }
        });
    
        // call first time
        onPage(0); 
    
        private void onPage(int position) {
            //...
        }
    
    0 讨论(0)
  • 2020-12-22 20:25

    if you don't care screen animotion try

    pager.setCurrentItem(1);
    pager.setCurrentItem(0);
    
    0 讨论(0)
  • 2020-12-22 20:27

    onPageSelected is not triggered for page which is opened first. For this case I manually perform all needed actions. Also, I suppose that call to onPageSelected(0) should work too.

    0 讨论(0)
  • 2020-12-22 20:27

    If the ViewPager is in a fragment, in the onViewCreated method, call mPager.setOnPageChangeListener(this)

    0 讨论(0)
  • 2020-12-22 20:28

    The cleanest solution I've found to this so far is to take a reference to the onPageChangeListener you set on the ViewPager (since I don't think there's a ViewPager.getOnPageChangeListener() method), then after you've set the ViewPager's adapter, call:

    onPageChangeListener.onPageSelected(viewPager.getCurrentItem());
    

    However, the fragment for the page at the current index won't have been instantiated yet (at least if you're using FragmentStatePagerAdapter), so you may need to wrap it in a runnable, ala:

    viewPager.post(new Runnable(){
    @Override
        public void run() {
            onPageChangeListener.onPageSelected(viewPager.getCurrentItem());
        }
    });
    

    Also, if within the onPageSelected handler you need a reference to the fragment, you'll have to do that yourself. I use an abstract base class for my FragmentStatePagerAdapter which overrides the instantiate and destroy methods, and adds/removes the fragments from a SparseArray.

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