Home > front end >  Migrating from ViewPager
Migrating from ViewPager

Time:04-28

I'm trying to convert my app to use ViewPager 2, however I'm stuck on converting to FragmentStateAdapter from FragmentStatePagerAdapter. I have

CodePudding user response:

Either use this as your activity is a FragmentActivity or if you want to fill up part of the screen get a reference to the fragment.

Other constructor options are

FragmentStateAdapter(fragment: Fragment)
FragmentStateAdapter(fragmentActivity: FragmentActivity)

viewpager -> viewpager2 migration documentation

CodePudding user response:

In order to make it work you can fix using

        viewPager.setAdapter(new FragmentStateAdapter(this.getSupportFragmentManager(),getLifecycle()) {
        
        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return null;
        }

        @Override
        public int getItemCount() {
            return 0;
        }
       });

I had the same migration issue sometime ago and found that creating a custom fragment state adapter way quite flexible and much more readable.

  • Related