Switch back to viewpager

pull/178/head
M66B 5 years ago
parent 5d8964703e
commit caf7983d90

@ -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

@ -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();
}
}
}
}

@ -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<Object> 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
}
}
}

@ -1,19 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout 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.ActivityView">
<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
<androidx.viewpager.widget.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
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>

@ -1,21 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout 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">
<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
<androidx.viewpager.widget.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
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>

Loading…
Cancel
Save