Switch back to viewpager

This commit is contained in:
M66B
2020-05-19 11:10:59 +02:00
parent 5d8964703e
commit caf7983d90
5 changed files with 172 additions and 165 deletions

View File

@@ -27,16 +27,16 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
public class FragmentLegend extends FragmentBase {
private int layout = -1;
private ViewPager2 pager;
private FragmentStateAdapter adapter;
private ViewPager pager;
private PagerAdapter adapter;
private FragmentLegend setLayout(int layout) {
this.layout = layout;
@@ -56,33 +56,7 @@ public class FragmentLegend extends FragmentBase {
view = inflater.inflate(R.layout.fragment_legend, container, false);
pager = view.findViewById(R.id.pager);
adapter = new FragmentStateAdapter(this) {
@Override
public int getItemCount() {
return 5;
}
@NonNull
@Override
public Fragment createFragment(int position) {
switch (position) {
case 0:
return new FragmentLegend().setLayout(R.layout.fragment_legend_synchronization);
case 1:
return new FragmentLegend().setLayout(R.layout.fragment_legend_folders);
case 2:
return new FragmentLegend().setLayout(R.layout.fragment_legend_messages);
case 3:
return new FragmentLegend().setLayout(R.layout.fragment_legend_compose);
case 4:
return new FragmentLegend().setLayout(R.layout.fragment_legend_keyboard);
default:
throw new IllegalArgumentException();
}
}
};
adapter = new PagerAdapter(getChildFragmentManager());
pager.setAdapter(adapter);
} else
view = inflater.inflate(layout, container, false);
@@ -97,30 +71,7 @@ public class FragmentLegend extends FragmentBase {
if (layout < 0) {
TabLayout tabLayout = view.findViewById(R.id.tab_layout);
new TabLayoutMediator(tabLayout, pager, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
switch (position) {
case 0:
tab.setText(R.string.title_legend_section_synchronize);
break;
case 1:
tab.setText(R.string.title_legend_section_folders);
break;
case 2:
tab.setText(R.string.title_legend_section_messages);
break;
case 3:
tab.setText(R.string.title_legend_section_compose);
break;
case 4:
tab.setText(R.string.title_legend_section_keyboard);
break;
default:
throw new IllegalArgumentException("Position=" + position);
}
}
}).attach();
tabLayout.setupWithViewPager(pager);
Bundle args = getArguments();
if (args != null) {
@@ -139,4 +90,51 @@ public class FragmentLegend extends FragmentBase {
outState.putInt("fair:layout", layout);
super.onSaveInstanceState(outState);
}
private class PagerAdapter extends FragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 5;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentLegend().setLayout(R.layout.fragment_legend_synchronization);
case 1:
return new FragmentLegend().setLayout(R.layout.fragment_legend_folders);
case 2:
return new FragmentLegend().setLayout(R.layout.fragment_legend_messages);
case 3:
return new FragmentLegend().setLayout(R.layout.fragment_legend_compose);
case 4:
return new FragmentLegend().setLayout(R.layout.fragment_legend_keyboard);
default:
throw new IllegalArgumentException();
}
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.title_legend_section_synchronize);
case 1:
return getString(R.string.title_legend_section_folders);
case 2:
return getString(R.string.title_legend_section_messages);
case 3:
return getString(R.string.title_legend_section_compose);
case 4:
return getString(R.string.title_legend_section_keyboard);
default:
throw new IllegalArgumentException();
}
}
}
}