Home > Back-end >  Migrating from ViewPager to ViewPager2 in Android/Java
Migrating from ViewPager to ViewPager2 in Android/Java

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 the old ViewPager instances commented out for reference on how I had ViewPager working. On the old instance, I used getSupportFragmentManger() to initialize fragmentManager. However, according to the developer docs, I need to change FragmentStatePagerAdapter to FragmentStateAdapter for ViewPager2, but something like getSupportFragmentActivity() doesn't exist. Any idea of a workaround?

import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import java.util.List;
import java.util.UUID;                                                          

public class CrimePagerActivity extends AppCompatActivity {
     //private ViewPager mViewPager;
     private ViewPager2 mViewPager2;
     private List<Crime> mCrimes;

@Override
protected void onCreate(Bundle savedInstancestate) {
    super.onCreate(savedInstancestate);
    setContentView(R.layout.activity_crime_pager);
    mCrimes = CrimeLab.get(this).getCrimes();

   //mViewPager = (ViewPager)findViewById(R.id.crime_view_pager);
   //        FragmentManager fragmentManager = getSupportFragmentManager();
   //        mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
   //            @Override
   //            public Fragment getItem(int position) {
   //                Crime crime = mCrimes.get(position);
   //                return CrimeFragment.newInstance(crime.getID());
   //            }
   //            @Override
   //            public int getCount() {
   //                return mCrimes.size();
   //            }
   //        });

    mViewPager2 = (ViewPager2) findViewById(R.id.crime_view_pager);
    FragmentActivity fragmentActivity = getSupportFragmentActivity();
    mViewPager2.setAdapter(new FragmentStateAdapter(fragmentActivity) {
        @Override
        public Fragment createFragment(int position) {
            Crime crime = mCrimes.get(position);
            return CrimeFragment.newInstance(crime.getID());
        }

        @Override
        public int getItemCount() {
            return mCrimes.size();
        }
    });

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