Port pager_fragment users to ViewPager2

This commit is contained in:
Ebrahim Byagowi 2020-04-08 16:20:31 +04:30
parent 417c66d73a
commit e8166a7f7f
5 changed files with 103 additions and 95 deletions

View File

@ -2,19 +2,21 @@ package de.danoeh.antennapod.fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
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;
/**
@ -29,10 +31,11 @@ public class DownloadsFragment extends Fragment {
public static final int POS_RUNNING = 0;
private static final int POS_COMPLETED = 1;
public static final int POS_LOG = 2;
private static final int TOTAL_COUNT = 3;
private static final String PREF_LAST_TAB_POSITION = "tab_position";
private ViewPager viewPager;
private ViewPager2 viewPager;
private TabLayout tabLayout;
@Override
@ -44,12 +47,23 @@ public class DownloadsFragment extends Fragment {
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
viewPager = root.findViewById(R.id.viewpager);
DownloadsPagerAdapter pagerAdapter = new DownloadsPagerAdapter(getChildFragmentManager(), getResources());
viewPager.setAdapter(pagerAdapter);
viewPager.setAdapter(new DownloadsPagerAdapter(this));
// Give the TabLayout the ViewPager
tabLayout = root.findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
switch (position) {
case POS_RUNNING:
tab.setText(R.string.downloads_running_label);
break;
case POS_COMPLETED:
tab.setText(R.string.downloads_completed_label);
break;
case POS_LOG:
tab.setText(R.string.downloads_log_label);
break;
}
}).attach();
return root;
}
@ -83,46 +97,29 @@ public class DownloadsFragment extends Fragment {
viewPager.setCurrentItem(lastPosition);
}
public static class DownloadsPagerAdapter extends FragmentPagerAdapter {
public static class DownloadsPagerAdapter extends FragmentStateAdapter {
final Resources resources;
public DownloadsPagerAdapter(FragmentManager fm, Resources resources) {
super(fm);
this.resources = resources;
DownloadsPagerAdapter(@NonNull Fragment fragment) {
super(fragment);
}
@NonNull
@Override
public Fragment getItem(int position) {
public Fragment createFragment(int position) {
switch (position) {
case POS_RUNNING:
return new RunningDownloadsFragment();
case POS_COMPLETED:
return new CompletedDownloadsFragment();
default:
case POS_LOG:
return new DownloadLogFragment();
default:
return null;
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case POS_RUNNING:
return resources.getString(R.string.downloads_running_label);
case POS_COMPLETED:
return resources.getString(R.string.downloads_completed_label);
case POS_LOG:
return resources.getString(R.string.downloads_log_label);
default:
return super.getPageTitle(position);
}
public int getItemCount() {
return TOTAL_COUNT;
}
}
}

View File

@ -6,13 +6,17 @@ import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
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;
public class EpisodesFragment extends Fragment {
@ -27,7 +31,7 @@ public class EpisodesFragment extends Fragment {
private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPager2 viewPager;
//Mandatory Constructor
public EpisodesFragment() {
@ -46,11 +50,25 @@ public class EpisodesFragment extends Fragment {
toolbar.setTitle(R.string.episodes_label);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
viewPager = rootView.findViewById(R.id.viewpager);
viewPager.setAdapter(new EpisodesPagerAdapter());
viewPager.setAdapter(new EpisodesPagerAdapter(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_ALL_EPISODES:
tab.setText(R.string.all_episodes_short_label);
break;
case POS_NEW_EPISODES:
tab.setText(R.string.new_episodes_label);
break;
case POS_FAV_EPISODES:
tab.setText(R.string.favorite_episodes_label);
break;
}
}).attach();
return rootView;
}
@ -74,15 +92,15 @@ public class EpisodesFragment extends Fragment {
viewPager.setCurrentItem(lastPosition);
}
public class EpisodesPagerAdapter extends FragmentPagerAdapter {
public class EpisodesPagerAdapter extends FragmentStateAdapter {
public EpisodesPagerAdapter() {
super(getChildFragmentManager());
EpisodesPagerAdapter(@NonNull Fragment fragment) {
super(fragment);
}
@Override
@NonNull
public Fragment getItem(int position) {
@Override
public Fragment createFragment(int position) {
switch (position) {
case 0:
return new NewEpisodesFragment();
@ -94,22 +112,8 @@ public class EpisodesFragment extends Fragment {
}
@Override
public int getCount() {
public int getItemCount() {
return TOTAL_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case POS_ALL_EPISODES:
return getString(R.string.all_episodes_short_label);
case POS_NEW_EPISODES:
return getString(R.string.new_episodes_label);
case POS_FAV_EPISODES:
return getString(R.string.favorite_episodes_label);
default:
return super.getPageTitle(position);
}
}
}
}

View File

@ -4,13 +4,20 @@ import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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.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;
/**
@ -18,6 +25,11 @@ import de.danoeh.antennapod.R;
*/
public class GpodnetMainFragment extends Fragment {
private static final int NUM_PAGES = 2;
private static final int POS_TOPLIST = 0;
private static final int POS_TAGS = 1;
private static final int POS_SUGGESTIONS = 2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
@ -26,63 +38,59 @@ public class GpodnetMainFragment extends Fragment {
toolbar.setTitle(R.string.gpodnet_main_label);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
ViewPager viewPager = root.findViewById(R.id.viewpager);
GpodnetPagerAdapter pagerAdapter = new GpodnetPagerAdapter(getChildFragmentManager());
ViewPager2 viewPager = root.findViewById(R.id.viewpager);
GpodnetPagerAdapter pagerAdapter = new GpodnetPagerAdapter(this);
viewPager.setAdapter(pagerAdapter);
// Give the TabLayout the ViewPager
TabLayout tabLayout = root.findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
switch (position) {
case POS_TAGS:
tab.setText(R.string.gpodnet_taglist_header);
break;
case POS_TOPLIST:
tab.setText(R.string.gpodnet_toplist_header);
break;
default:
case POS_SUGGESTIONS:
tab.setText(R.string.gpodnet_suggestions_header);
break;
}
}).attach();
return root;
}
public class GpodnetPagerAdapter extends FragmentPagerAdapter {
private static final int NUM_PAGES = 2;
private static final int POS_TOPLIST = 0;
private static final int POS_TAGS = 1;
private static final int POS_SUGGESTIONS = 2;
public static class GpodnetPagerAdapter extends FragmentStateAdapter {
public GpodnetPagerAdapter(FragmentManager fm) {
super(fm);
GpodnetPagerAdapter(@NonNull Fragment fragment) {
super(fragment);
}
@NonNull
@Override
public Fragment getItem(int i) {
public Fragment createFragment(int position) {
Bundle arguments = new Bundle();
arguments.putBoolean(PodcastListFragment.ARGUMENT_HIDE_TOOLBAR, true);
switch (i) {
switch (position) {
case POS_TAGS:
return new TagListFragment();
case POS_TOPLIST:
PodcastListFragment topListFragment = new PodcastTopListFragment();
topListFragment.setArguments(arguments);
return topListFragment;
default:
case POS_SUGGESTIONS:
PodcastListFragment suggestionsFragment = new SuggestionListFragment();
suggestionsFragment.setArguments(arguments);
return suggestionsFragment;
default:
return null;
}
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case POS_TAGS:
return getString(R.string.gpodnet_taglist_header);
case POS_TOPLIST:
return getString(R.string.gpodnet_toplist_header);
case POS_SUGGESTIONS:
return getString(R.string.gpodnet_suggestions_header);
default:
return super.getPageTitle(position);
}
}
@Override
public int getCount() {
public int getItemCount() {
return NUM_PAGES;
}
}

View File

@ -49,11 +49,10 @@ public class StatisticsFragment extends Fragment {
new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
switch (position) {
case POS_LISTENED_HOURS:
tab.setText(getString(R.string.playback_statistics_label));
tab.setText(R.string.playback_statistics_label);
break;
default:
case POS_SPACE_TAKEN:
tab.setText(getString(R.string.download_statistics_label));
tab.setText(R.string.download_statistics_label);
break;
}
}).attach();

View File

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