From 09af0e2448fe98babb1a0fdf20e6ec45246da349 Mon Sep 17 00:00:00 2001 From: TacoTheDank Date: Sat, 17 Oct 2020 19:15:10 -0400 Subject: [PATCH] Migrate AboutActivity to viewpager2 --- .../schabi/newpipe/about/AboutActivity.java | 73 ++++++++++--------- app/src/main/res/layout/activity_about.xml | 2 +- 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/about/AboutActivity.java b/app/src/main/java/org/schabi/newpipe/about/AboutActivity.java index 81ba78ba9..c24636cf0 100644 --- a/app/src/main/java/org/schabi/newpipe/about/AboutActivity.java +++ b/app/src/main/java/org/schabi/newpipe/about/AboutActivity.java @@ -8,16 +8,18 @@ import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; -import androidx.fragment.app.FragmentPagerAdapter; -import androidx.fragment.app.FragmentStatePagerAdapter; -import androidx.viewpager.widget.PagerAdapter; -import androidx.viewpager.widget.ViewPager; +import androidx.lifecycle.Lifecycle; +import androidx.recyclerview.widget.RecyclerView; +import androidx.viewpager2.adapter.FragmentStateAdapter; +import androidx.viewpager2.widget.ViewPager2; import com.google.android.material.tabs.TabLayout; +import com.google.android.material.tabs.TabLayoutMediator; import org.schabi.newpipe.BuildConfig; import org.schabi.newpipe.R; @@ -64,20 +66,20 @@ public class AboutActivity extends AppCompatActivity { "https://github.com/lisawray/groupie", StandardLicenses.MIT) }; + private static final int POS_ABOUT = 0; + private static final int POS_LICENSE = 1; + private static final int TOTAL_COUNT = 2; /** - * The {@link PagerAdapter} that will provide + * The {@link RecyclerView.Adapter} that will provide * fragments for each of the sections. We use a - * {@link FragmentPagerAdapter} derivative, which will keep every - * loaded fragment in memory. If this becomes too memory intensive, it - * may be best to switch to a - * {@link FragmentStatePagerAdapter}. + * {@link FragmentStateAdapter} derivative, which will keep every + * loaded fragment in memory. */ private SectionsPagerAdapter mSectionsPagerAdapter; - /** - * The {@link ViewPager} that will host the section contents. + * The {@link ViewPager2} that will host the section contents. */ - private ViewPager mViewPager; + private ViewPager2 mViewPager; @Override protected void onCreate(final Bundle savedInstanceState) { @@ -93,14 +95,25 @@ public class AboutActivity extends AppCompatActivity { getSupportActionBar().setDisplayHomeAsUpEnabled(true); // Create the adapter that will return a fragment for each of the three // primary sections of the activity. - mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); + mSectionsPagerAdapter = + new SectionsPagerAdapter(getSupportFragmentManager(), getLifecycle()); // Set up the ViewPager with the sections adapter. mViewPager = findViewById(R.id.container); mViewPager.setAdapter(mSectionsPagerAdapter); final TabLayout tabLayout = findViewById(R.id.tabs); - tabLayout.setupWithViewPager(mViewPager); + new TabLayoutMediator(tabLayout, mViewPager, (tab, position) -> { + switch (position) { + default: + case POS_ABOUT: + tab.setText(R.string.tab_about); + break; + case POS_LICENSE: + tab.setText(R.string.tab_licenses); + break; + } + }).attach(); } @Override @@ -162,40 +175,30 @@ public class AboutActivity extends AppCompatActivity { } /** - * A {@link FragmentPagerAdapter} that returns a fragment corresponding to + * A {@link FragmentStateAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ - public class SectionsPagerAdapter extends FragmentPagerAdapter { - public SectionsPagerAdapter(final FragmentManager fm) { - super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); + public static class SectionsPagerAdapter extends FragmentStateAdapter { + public SectionsPagerAdapter(final FragmentManager fm, final Lifecycle lifecycle) { + super(fm, lifecycle); } + @NonNull @Override - public Fragment getItem(final int position) { + public Fragment createFragment(final int position) { switch (position) { - case 0: + default: + case POS_ABOUT: return AboutFragment.newInstance(); - case 1: + case POS_LICENSE: return LicenseFragment.newInstance(SOFTWARE_COMPONENTS); } - return null; } @Override - public int getCount() { + public int getItemCount() { // Show 2 total pages. - return 2; - } - - @Override - public CharSequence getPageTitle(final int position) { - switch (position) { - case 0: - return getString(R.string.tab_about); - case 1: - return getString(R.string.tab_licenses); - } - return null; + return TOTAL_COUNT; } } } diff --git a/app/src/main/res/layout/activity_about.xml b/app/src/main/res/layout/activity_about.xml index f1498f2b2..525b2371f 100644 --- a/app/src/main/res/layout/activity_about.xml +++ b/app/src/main/res/layout/activity_about.xml @@ -30,7 +30,7 @@ -