diff --git a/app/build.gradle b/app/build.gradle index f3b3a3b14f..76c1977d53 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -272,7 +272,7 @@ dependencies { implementation "androidx.fragment:fragment:$fragment_version" // https://mvnrepository.com/artifact/androidx.viewpager2/viewpager2 - implementation "androidx.viewpager2:viewpager2:1.1.0-alpha01" + //implementation "androidx.viewpager2:viewpager2:1.1.0-alpha01" // https://mvnrepository.com/artifact/androidx.recyclerview/recyclerview // https://mvnrepository.com/artifact/androidx.recyclerview/recyclerview-selection diff --git a/app/src/main/java/eu/faircode/email/FragmentLegend.java b/app/src/main/java/eu/faircode/email/FragmentLegend.java index dbe028a7ae..01109f0de9 100644 --- a/app/src/main/java/eu/faircode/email/FragmentLegend.java +++ b/app/src/main/java/eu/faircode/email/FragmentLegend.java @@ -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(); + } + } + } } diff --git a/app/src/main/java/eu/faircode/email/FragmentOptions.java b/app/src/main/java/eu/faircode/email/FragmentOptions.java index 72d6e70f04..defac2c9ec 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptions.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptions.java @@ -38,21 +38,18 @@ import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.constraintlayout.widget.Group; import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentStatePagerAdapter; import androidx.lifecycle.Lifecycle; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.preference.PreferenceManager; -import androidx.viewpager2.adapter.FragmentStateAdapter; -import androidx.viewpager2.adapter.FragmentViewHolder; -import androidx.viewpager2.widget.ViewPager2; +import androidx.viewpager.widget.ViewPager; import com.google.android.material.tabs.TabLayout; -import com.google.android.material.tabs.TabLayoutMediator; - -import java.util.List; public class FragmentOptions extends FragmentBase { - private ViewPager2 pager; - private FragmentStateAdapter adapter; + private ViewPager pager; + private PagerAdapter adapter; static String[] OPTIONS_RESTART = new String[]{ "first", "app_support", "notify_archive", "message_swipe", "message_select", "folder_actions", "folder_sync", @@ -79,46 +76,17 @@ 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); - adapter = new FragmentStateAdapter(this) { - @NonNull - @Override - 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(); - } - } - + pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override - public int getItemCount() { - return 10; + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + // Do nothing } @Override - public void onBindViewHolder(@NonNull FragmentViewHolder holder, int position, @NonNull List payloads) { - super.onBindViewHolder(holder, position, payloads); - + public void onPageSelected(int position) { if (position > 0) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); boolean setup_advanced = prefs.getBoolean("setup_advanced", false); @@ -129,9 +97,12 @@ public class FragmentOptions extends FragmentBase { } } } - }; - pager.setAdapter(adapter); + @Override + public void onPageScrollStateChanged(int state) { + // Do nothing + } + }); addKeyPressedListener(new ActivityBase.IKeyPressedListener() { @Override @@ -156,45 +127,7 @@ public class FragmentOptions extends FragmentBase { @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { 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_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(); + tabLayout.setupWithViewPager(pager); String tab = getActivity().getIntent().getStringExtra("tab"); if ("connection".equals(tab)) @@ -281,4 +214,76 @@ public class FragmentOptions extends FragmentBase { .create(); } } + + 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 + } + } } diff --git a/app/src/main/res/layout/fragment_legend.xml b/app/src/main/res/layout/fragment_legend.xml index 3d79bef426..2a9a04b6ca 100644 --- a/app/src/main/res/layout/fragment_legend.xml +++ b/app/src/main/res/layout/fragment_legend.xml @@ -1,19 +1,22 @@ - + tools:context="eu.faircode.email.ActivityView"> - - - - + android:layout_height="match_parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent"> + + + + diff --git a/app/src/main/res/layout/fragment_options.xml b/app/src/main/res/layout/fragment_options.xml index 2645f95182..32e7b0d9e7 100644 --- a/app/src/main/res/layout/fragment_options.xml +++ b/app/src/main/res/layout/fragment_options.xml @@ -1,21 +1,22 @@ - - - - - + android:layout_height="match_parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent"> + + + +