skip to Main Content

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

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search