PagerAdapter start position

前端 未结 9 1102

I\'m using the following example to impliment my viewPager: http://code.google.com/p/viewpagerexample/issues/list

The problem with this example is that I can\'t figure o

9条回答
  •  失恋的感觉
    2021-01-30 08:36

    I have noticed that if you recreate Activity (orientation change) with ViewPager having FragmentStatePagerAdapter, then the Adapter will reuse it's Fragments. The way to stop it is:

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        if (viewPager != null) {
            // before screen rotation it's better to detach pagerAdapter from the ViewPager, so
            // pagerAdapter can remove all old fragments, so they're not reused after rotation.
            viewPager.setAdapter(null);
        }
        super.onSaveInstanceState(savedInstanceState);
    }
    

    but then after Activity recreation ViewPager alwayes opens page 0 first and setCurrentItem(CurrentPosition); doesn't work. Fix for that is changing page after delay:

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            viewPager.setCurrentItem(newPosition);
        }
    }, 100);
    

提交回复
热议问题