Adopt StatisticsFragment with ViewPager2

This commit is contained in:
Ebrahim Byagowi 2020-04-08 15:50:08 +04:30
parent cedfb27cca
commit 417c66d73a
2 changed files with 33 additions and 36 deletions

View File

@ -1,17 +1,17 @@
package de.danoeh.antennapod.fragment.preferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
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 de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.PreferenceActivity;
@ -29,25 +29,34 @@ public class StatisticsFragment extends Fragment {
private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPager2 viewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
setHasOptionsMenu(true);
View rootView = inflater.inflate(R.layout.pager_fragment, container, false);
viewPager = rootView.findViewById(R.id.viewpager);
viewPager.setAdapter(new StatisticsPagerAdapter(getChildFragmentManager(), getResources()));
viewPager.setAdapter(new StatisticsPagerAdapter(this));
// Give the TabLayout the ViewPager
tabLayout = rootView.findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
switch (position) {
case POS_LISTENED_HOURS:
tab.setText(getString(R.string.playback_statistics_label));
break;
default:
case POS_SPACE_TAKEN:
tab.setText(getString(R.string.download_statistics_label));
break;
}
}).attach();
rootView.findViewById(R.id.toolbar).setVisibility(View.GONE);
@ -60,39 +69,27 @@ public class StatisticsFragment extends Fragment {
((PreferenceActivity) getActivity()).getSupportActionBar().setTitle(R.string.statistics_label);
}
public static class StatisticsPagerAdapter extends FragmentPagerAdapter {
public static class StatisticsPagerAdapter extends FragmentStateAdapter {
private final Resources resources;
public StatisticsPagerAdapter(FragmentManager fm, Resources resources) {
super(fm);
this.resources = resources;
StatisticsPagerAdapter(@NonNull Fragment fragment) {
super(fragment);
}
@NonNull
@Override
public Fragment getItem(int position) {
if (position == 0) {
return new PlaybackStatisticsFragment();
} else {
return new DownloadStatisticsFragment();
}
}
@Override
public int getCount() {
return TOTAL_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
public Fragment createFragment(int position) {
switch (position) {
case POS_LISTENED_HOURS:
return resources.getString(R.string.playback_statistics_label);
case POS_SPACE_TAKEN:
return resources.getString(R.string.download_statistics_label);
case 0:
return new PlaybackStatisticsFragment();
default:
return super.getPageTitle(position);
case 1:
return new DownloadStatisticsFragment();
}
}
@Override
public int getItemCount() {
return TOTAL_COUNT;
}
}
}

View File

@ -14,7 +14,7 @@
app:tabGravity="fill"
app:tabMode="fixed" />
<androidx.viewpager.widget.ViewPager
<androidx.viewpager.widget.ViewPager2
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"