Switched legend to ViewPager2

pull/178/head
M66B 5 years ago
parent 19345bd7fd
commit 7fd6a83c68

@ -27,18 +27,18 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter; import androidx.viewpager2.widget.ViewPager2;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
public class FragmentLegend extends FragmentBase { public class FragmentLegend extends FragmentBase {
private int layout = -1; private int layout = -1;
private ViewPager pager; private ViewPager2 pager;
private PagerAdapter adapter; private FragmentStateAdapter adapter;
FragmentLegend setLayout(int layout) { private FragmentLegend setLayout(int layout) {
this.layout = layout; this.layout = layout;
return this; return this;
} }
@ -56,7 +56,33 @@ public class FragmentLegend extends FragmentBase {
view = inflater.inflate(R.layout.fragment_legend, container, false); view = inflater.inflate(R.layout.fragment_legend, container, false);
pager = view.findViewById(R.id.pager); pager = view.findViewById(R.id.pager);
adapter = new PagerAdapter(getChildFragmentManager());
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();
}
}
};
pager.setAdapter(adapter); pager.setAdapter(adapter);
} else } else
view = inflater.inflate(layout, container, false); view = inflater.inflate(layout, container, false);
@ -71,7 +97,30 @@ public class FragmentLegend extends FragmentBase {
if (layout < 0) { if (layout < 0) {
TabLayout tabLayout = view.findViewById(R.id.tab_layout); TabLayout tabLayout = view.findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(pager); 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();
Bundle args = getArguments(); Bundle args = getArguments();
if (args != null) { if (args != null) {
@ -90,51 +139,4 @@ public class FragmentLegend extends FragmentBase {
outState.putInt("fair:layout", layout); outState.putInt("fair:layout", layout);
super.onSaveInstanceState(outState); 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();
}
}
}
} }

@ -1,22 +1,19 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context="eu.faircode.email.ActivityView"> android:orientation="vertical">
<androidx.viewpager.widget.ViewPager <com.google.android.material.tabs.TabLayout
android:id="@+id/pager" android:id="@+id/tab_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent" app:tabMode="scrollable" />
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabLayout <androidx.viewpager2.widget.ViewPager2
android:id="@+id/tab_layout" android:id="@+id/pager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="0dp"
app:tabMode="scrollable" /> android:layout_weight="1" />
</androidx.viewpager.widget.ViewPager> </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Loading…
Cancel
Save