Android: FragmentPagerAdapter: getItem method called twice on First time

前端 未结 5 1466
一整个雨季
一整个雨季 2020-12-15 04:44

In My application, I have used the ViewPager. Like,

(say main.xml)



        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 05:01

    I faced the same issue - getItem() method was called twice. I don't know about your purposes of using that method by mine was to trigger changes in a hosting activity when new slide appears.

    Well, first of all getItem() is not a right method to get event of appearing new slide.

    I used ViewPager.OnPageChangeListener listener on ViewPager itself for this purpose. Here is what I did:

    In Activity that holds slide fragments:

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signature_wizard_activity);
    
        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                onSlideChanged(position); // change color of the dots
            }
            @Override
            public void onPageSelected(int position) {}
            @Override
            public void onPageScrollStateChanged(int state) {}
        });
    }
    

    onPageScrolled() is called every time your slide appears, as a parameter it gets current position of the slide that is shown. Nice thing is that it is called including the first time it appears, not just when slide was changed.

    When it can be used? For example if you have some wizard activity with ViewPager where you slide fragments with some hints, you probable would like to have a bar with grey dots below the slider that would represent the number of slides in total, and one of the dots will be different color representing the current slide. In this case ViewPager.OnPageChangeListener and method onPageScrolled() will be the best option.

    Or if you have buttons PREV. and NEXT which change slides and you want to disable PREV. button on the first slide and disable NEXT button on the last slide. Here is how you do it inside onPageScrolled() method:

     @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(position == 0){
                    prevBtn.setTextColor(ContextCompat.getColor(SignatureWizardActivity.this, R.color.main_grey_500));
                    prevBtn.setEnabled(false);
                }else{
                    prevBtn.setTextColor(ContextCompat.getColor(SignatureWizardActivity.this, R.color.main_blue));
                    prevBtn.setEnabled(true);
                }
    
                if(position == NUM_PAGES -1){
                    nextBtn.setTextColor(ContextCompat.getColor(SignatureWizardActivity.this, R.color.main_grey_500));
                    nextBtn.setEnabled(false);
                }else{
                    nextBtn.setTextColor(ContextCompat.getColor(SignatureWizardActivity.this, R.color.main_blue));
                    nextBtn.setEnabled(true);
                }
            }
    

    where NUM_PAGES is a constant with total number of slides

提交回复
热议问题