Switched options to ViewPager2

This commit is contained in:
M66B 2020-04-15 10:08:13 +02:00
parent 7fd6a83c68
commit f23cef1279
2 changed files with 96 additions and 102 deletions

View File

@ -29,17 +29,20 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.adapter.FragmentViewHolder;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import java.util.List;
public class FragmentOptions extends FragmentBase {
private ViewPager pager;
private PagerAdapter adapter;
private ViewPager2 pager;
private FragmentStateAdapter adapter;
static String[] OPTIONS_RESTART = new String[]{
"first", "app_support", "notify_archive", "message_swipe", "message_select", "folder_actions", "folder_sync",
@ -66,17 +69,46 @@ public class FragmentOptions extends FragmentBase {
View view = inflater.inflate(R.layout.fragment_options, container, false);
pager = view.findViewById(R.id.pager);
adapter = new PagerAdapter(getChildFragmentManager());
pager.setAdapter(adapter);
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
adapter = new FragmentStateAdapter(this) {
@NonNull
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// Do nothing
public Fragment createFragment(int position) {
switch (position) {
case 0:
return new FragmentSetup();
case 1:
return new FragmentOptionsSynchronize();
case 2:
return new FragmentOptionsSend();
case 3:
return new FragmentOptionsConnection();
case 4:
return new FragmentOptionsDisplay();
case 5:
return new FragmentOptionsBehavior();
case 6:
return new FragmentOptionsPrivacy();
case 7:
return new FragmentOptionsEncryption();
case 8:
return new FragmentOptionsNotifications();
case 9:
return new FragmentOptionsMisc();
default:
throw new IllegalArgumentException();
}
}
@Override
public void onPageSelected(int position) {
public int getItemCount() {
return 10;
}
@Override
public void onBindViewHolder(@NonNull FragmentViewHolder holder, int position, @NonNull List<Object> payloads) {
super.onBindViewHolder(holder, position, payloads);
if (position > 0) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean setup_advanced = prefs.getBoolean("setup_advanced", false);
@ -87,12 +119,9 @@ public class FragmentOptions extends FragmentBase {
}
}
}
};
@Override
public void onPageScrollStateChanged(int state) {
// Do nothing
}
});
pager.setAdapter(adapter);
return view;
}
@ -100,7 +129,45 @@ public class FragmentOptions extends FragmentBase {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
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_advanced_section_main);
break;
case 1:
tab.setText(R.string.title_advanced_section_synchronize);
break;
case 2:
tab.setText(R.string.title_advanced_section_send);
break;
case 3:
tab.setText(R.string.title_advanced_section_connection);
break;
case 4:
tab.setText(R.string.title_advanced_section_display);
break;
case 5:
tab.setText(R.string.title_advanced_section_behavior);
break;
case 6:
tab.setText(R.string.title_advanced_section_privacy);
break;
case 7:
tab.setText(R.string.title_advanced_section_encryption);
break;
case 8:
tab.setText(R.string.title_advanced_section_notifications);
break;
case 9:
tab.setText(R.string.title_advanced_section_misc);
break;
default:
throw new IllegalArgumentException();
}
}
}).attach();
String tab = getActivity().getIntent().getStringExtra("tab");
if ("connection".equals(tab))
@ -115,76 +182,4 @@ public class FragmentOptions extends FragmentBase {
boolean isVisible(int position) {
return (pager.getCurrentItem() == position);
}
private class PagerAdapter extends FragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 10;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentSetup();
case 1:
return new FragmentOptionsSynchronize();
case 2:
return new FragmentOptionsSend();
case 3:
return new FragmentOptionsConnection();
case 4:
return new FragmentOptionsDisplay();
case 5:
return new FragmentOptionsBehavior();
case 6:
return new FragmentOptionsPrivacy();
case 7:
return new FragmentOptionsEncryption();
case 8:
return new FragmentOptionsNotifications();
case 9:
return new FragmentOptionsMisc();
default:
throw new IllegalArgumentException();
}
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.title_advanced_section_main);
case 1:
return getString(R.string.title_advanced_section_synchronize);
case 2:
return getString(R.string.title_advanced_section_send);
case 3:
return getString(R.string.title_advanced_section_connection);
case 4:
return getString(R.string.title_advanced_section_display);
case 5:
return getString(R.string.title_advanced_section_behavior);
case 6:
return getString(R.string.title_advanced_section_privacy);
case 7:
return getString(R.string.title_advanced_section_encryption);
case 8:
return getString(R.string.title_advanced_section_notifications);
case 9:
return getString(R.string.title_advanced_section_misc);
default:
throw new IllegalArgumentException();
}
}
@Override
public int getItemPosition(@NonNull Object object) {
return POSITION_NONE; // always recreate fragment
}
}
}

View File

@ -1,22 +1,21 @@
<?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:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="eu.faircode.email.ActivitySetup">
<androidx.viewpager.widget.ViewPager
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
</androidx.viewpager.widget.ViewPager>
</androidx.constraintlayout.widget.ConstraintLayout>
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>