Merge pull request #4553 from TacoTheDank/about-viewpager2

Migrate AboutActivity to ViewPager2
This commit is contained in:
opusforlife2 2020-10-18 10:12:20 +00:00 committed by GitHub
commit 9b9d267cd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 36 deletions

View File

@ -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;
}
}
}

View File

@ -30,7 +30,7 @@
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"