Merge pull request #4553 from TacoTheDank/about-viewpager2
Migrate AboutActivity to ViewPager2
This commit is contained in:
commit
9b9d267cd4
|
@ -8,16 +8,18 @@ import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.fragment.app.FragmentManager;
|
import androidx.fragment.app.FragmentManager;
|
||||||
import androidx.fragment.app.FragmentPagerAdapter;
|
import androidx.lifecycle.Lifecycle;
|
||||||
import androidx.fragment.app.FragmentStatePagerAdapter;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
import androidx.viewpager.widget.PagerAdapter;
|
import androidx.viewpager2.adapter.FragmentStateAdapter;
|
||||||
import androidx.viewpager.widget.ViewPager;
|
import androidx.viewpager2.widget.ViewPager2;
|
||||||
|
|
||||||
import com.google.android.material.tabs.TabLayout;
|
import com.google.android.material.tabs.TabLayout;
|
||||||
|
import com.google.android.material.tabs.TabLayoutMediator;
|
||||||
|
|
||||||
import org.schabi.newpipe.BuildConfig;
|
import org.schabi.newpipe.BuildConfig;
|
||||||
import org.schabi.newpipe.R;
|
import org.schabi.newpipe.R;
|
||||||
|
@ -64,20 +66,20 @@ public class AboutActivity extends AppCompatActivity {
|
||||||
"https://github.com/lisawray/groupie", StandardLicenses.MIT)
|
"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
|
* fragments for each of the sections. We use a
|
||||||
* {@link FragmentPagerAdapter} derivative, which will keep every
|
* {@link FragmentStateAdapter} derivative, which will keep every
|
||||||
* loaded fragment in memory. If this becomes too memory intensive, it
|
* loaded fragment in memory.
|
||||||
* may be best to switch to a
|
|
||||||
* {@link FragmentStatePagerAdapter}.
|
|
||||||
*/
|
*/
|
||||||
private SectionsPagerAdapter mSectionsPagerAdapter;
|
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
|
@Override
|
||||||
protected void onCreate(final Bundle savedInstanceState) {
|
protected void onCreate(final Bundle savedInstanceState) {
|
||||||
|
@ -93,14 +95,25 @@ public class AboutActivity extends AppCompatActivity {
|
||||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||||
// Create the adapter that will return a fragment for each of the three
|
// Create the adapter that will return a fragment for each of the three
|
||||||
// primary sections of the activity.
|
// primary sections of the activity.
|
||||||
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
|
mSectionsPagerAdapter =
|
||||||
|
new SectionsPagerAdapter(getSupportFragmentManager(), getLifecycle());
|
||||||
|
|
||||||
// Set up the ViewPager with the sections adapter.
|
// Set up the ViewPager with the sections adapter.
|
||||||
mViewPager = findViewById(R.id.container);
|
mViewPager = findViewById(R.id.container);
|
||||||
mViewPager.setAdapter(mSectionsPagerAdapter);
|
mViewPager.setAdapter(mSectionsPagerAdapter);
|
||||||
|
|
||||||
final TabLayout tabLayout = findViewById(R.id.tabs);
|
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
|
@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.
|
* one of the sections/tabs/pages.
|
||||||
*/
|
*/
|
||||||
public class SectionsPagerAdapter extends FragmentPagerAdapter {
|
public static class SectionsPagerAdapter extends FragmentStateAdapter {
|
||||||
public SectionsPagerAdapter(final FragmentManager fm) {
|
public SectionsPagerAdapter(final FragmentManager fm, final Lifecycle lifecycle) {
|
||||||
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
|
super(fm, lifecycle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public Fragment getItem(final int position) {
|
public Fragment createFragment(final int position) {
|
||||||
switch (position) {
|
switch (position) {
|
||||||
case 0:
|
default:
|
||||||
|
case POS_ABOUT:
|
||||||
return AboutFragment.newInstance();
|
return AboutFragment.newInstance();
|
||||||
case 1:
|
case POS_LICENSE:
|
||||||
return LicenseFragment.newInstance(SOFTWARE_COMPONENTS);
|
return LicenseFragment.newInstance(SOFTWARE_COMPONENTS);
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getCount() {
|
public int getItemCount() {
|
||||||
// Show 2 total pages.
|
// Show 2 total pages.
|
||||||
return 2;
|
return TOTAL_COUNT;
|
||||||
}
|
|
||||||
|
|
||||||
@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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@
|
||||||
|
|
||||||
</com.google.android.material.appbar.AppBarLayout>
|
</com.google.android.material.appbar.AppBarLayout>
|
||||||
|
|
||||||
<androidx.viewpager.widget.ViewPager
|
<androidx.viewpager2.widget.ViewPager2
|
||||||
android:id="@+id/container"
|
android:id="@+id/container"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
|
Loading…
Reference in New Issue