ViewPager2 - MainActivity

This commit is contained in:
Thomas 2022-06-22 11:01:39 +02:00
parent eb162ba845
commit 91c0f0de75
5 changed files with 76 additions and 96 deletions

View file

@ -101,6 +101,7 @@ dependencies {
implementation 'com.github.penfeizhou.android.animation:apng:2.22.0'
implementation 'com.github.penfeizhou.android.animation:gif:2.22.0'
implementation 'com.google.android.exoplayer:exoplayer:2.16.1'
implementation "androidx.viewpager2:viewpager2:1.0.0"
implementation 'com.github.piasy:rxandroidaudio:1.7.0'
implementation 'com.github.piasy:AudioProcessor:1.7.0'
implementation "androidx.work:work-runtime:2.7.1"

View file

@ -748,7 +748,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt
}
popup.setOnDismissListener(menu1 -> {
if (binding.viewPager.getAdapter() != null) {
Fragment fragment = (Fragment) binding.viewPager.getAdapter().instantiateItem(binding.viewPager, binding.tabLayout.getSelectedTabPosition());
Fragment fragment = (Fragment) getSupportFragmentManager().findFragmentByTag("f" + binding.viewPager.getCurrentItem());
if (fragment instanceof FragmentMastodonTimeline && fragment.isVisible()) {
FragmentMastodonTimeline fragmentMastodonTimeline = ((FragmentMastodonTimeline) fragment);
fragmentMastodonTimeline.refreshAllAdapters();
@ -879,7 +879,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt
*/
private void scrollToTop() {
if (binding.viewPager.getAdapter() != null) {
Fragment fragment = (Fragment) binding.viewPager.getAdapter().instantiateItem(binding.viewPager, binding.tabLayout.getSelectedTabPosition());
Fragment fragment = (Fragment) getSupportFragmentManager().findFragmentByTag("f" + binding.viewPager.getCurrentItem());
if (fragment instanceof FragmentMastodonTimeline) {
FragmentMastodonTimeline fragmentMastodonTimeline = ((FragmentMastodonTimeline) fragment);
fragmentMastodonTimeline.scrollToTop();

View file

@ -17,7 +17,6 @@ package app.fedilab.android.helper;
import static app.fedilab.android.BaseMainActivity.currentAccount;
import static app.fedilab.android.ui.pageadapter.FedilabPageAdapter.BOTTOM_TIMELINE_COUNT;
import android.content.Context;
import android.os.Bundle;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
@ -33,7 +32,7 @@ import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.tabs.TabLayout;
@ -202,20 +201,19 @@ public class PinnedTimelineHelper {
});
}
activityMainBinding.viewPager.setAdapter(null);
activityMainBinding.viewPager.clearOnPageChangeListeners();
activityMainBinding.tabLayout.clearOnTabSelectedListeners();
FedilabPageAdapter fedilabPageAdapter = new FedilabPageAdapter(activity, activity.getSupportFragmentManager(), pinned, bottomMenu);
FedilabPageAdapter fedilabPageAdapter = new FedilabPageAdapter(activity, activity, pinned, bottomMenu);
activityMainBinding.viewPager.setAdapter(fedilabPageAdapter);
activityMainBinding.viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(activityMainBinding.tabLayout));
activityMainBinding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
activityMainBinding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
if (position < BOTTOM_TIMELINE_COUNT - toRemove) {
activityMainBinding.bottomNavView.getMenu().getItem(position).setChecked(true);
} else {
@ -229,10 +227,11 @@ public class PinnedTimelineHelper {
@Override
public void onPageScrollStateChanged(int state) {
super.onPageScrollStateChanged(state);
}
});
activityMainBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
@ -245,7 +244,7 @@ public class PinnedTimelineHelper {
@Override
public void onTabReselected(TabLayout.Tab tab) {
Fragment fragment = fedilabPageAdapter.getCurrentFragment();
Fragment fragment = (Fragment) activity.getSupportFragmentManager().findFragmentByTag("f" + activityMainBinding.viewPager.getCurrentItem());
if (fragment instanceof FragmentMastodonTimeline) {
((FragmentMastodonTimeline) fragment).scrollToTop();
} else if (fragment instanceof FragmentMastodonConversation) {
@ -256,13 +255,13 @@ public class PinnedTimelineHelper {
}
public static int itemToRemoveInBottomMenu(Context context) {
public static int itemToRemoveInBottomMenu(BaseMainActivity activity) {
//Small hack to hide first tabs (they represent the item of the bottom menu)
BottomMenu bottomMenuDb;
int toRemove = 0;
try {
//If some menu items have been hidden we should not create tab for them
bottomMenuDb = new BottomMenu(context).getAllBottomMenu(currentAccount);
bottomMenuDb = new BottomMenu(activity).getAllBottomMenu(currentAccount);
if (bottomMenuDb != null) {
List<BottomMenu.MenuItem> menuItemList = bottomMenuDb.bottom_menu;
if (menuItemList != null) {
@ -282,14 +281,14 @@ public class PinnedTimelineHelper {
/**
* Manage long clicks on Tag timelines
*
* @param context - Context of the activity
* @param activity - BaseMainActivity activity
* @param pinned - {@link Pinned}
* @param view - View
* @param position - int position of the tab
*/
public static void tagClick(Context context, Pinned pinned, View view, ActivityMainBinding activityMainBinding, int position) {
int toRemove = itemToRemoveInBottomMenu(context);
PopupMenu popup = new PopupMenu(new ContextThemeWrapper(context, Helper.popupStyle()), view);
public static void tagClick(BaseMainActivity activity, Pinned pinned, View view, ActivityMainBinding activityMainBinding, int position) {
int toRemove = itemToRemoveInBottomMenu(activity);
PopupMenu popup = new PopupMenu(new ContextThemeWrapper(activity, Helper.popupStyle()), view);
int offSetPosition = position - (BOTTOM_TIMELINE_COUNT - toRemove);
String tag;
TagTimeline tagTimeline = pinned.pinnedTimelines.get(offSetPosition).tagTimeline;
@ -319,16 +318,16 @@ public class PinnedTimelineHelper {
if (changes[0]) {
FragmentMastodonTimeline fragmentMastodonTimeline;
if (activityMainBinding.viewPager.getAdapter() != null) {
Fragment fragment = (Fragment) activityMainBinding.viewPager.getAdapter().instantiateItem(activityMainBinding.viewPager, activityMainBinding.tabLayout.getSelectedTabPosition());
Fragment fragment = (Fragment) activity.getSupportFragmentManager().findFragmentByTag("f" + activityMainBinding.viewPager.getCurrentItem());
if (fragment instanceof FragmentMastodonTimeline && fragment.isVisible()) {
fragmentMastodonTimeline = ((FragmentMastodonTimeline) fragment);
FragmentTransaction fragTransaction = ((BaseMainActivity) context).getSupportFragmentManager().beginTransaction();
FragmentTransaction fragTransaction = activity.getSupportFragmentManager().beginTransaction();
fragTransaction.detach(fragmentMastodonTimeline).commit();
Bundle bundle = new Bundle();
bundle.putSerializable(Helper.ARG_TIMELINE_TYPE, Timeline.TimeLineEnum.TAG);
bundle.putSerializable(Helper.ARG_TAG_TIMELINE, tagTimeline);
fragmentMastodonTimeline.setArguments(bundle);
FragmentTransaction fragTransaction2 = ((BaseMainActivity) context).getSupportFragmentManager().beginTransaction();
FragmentTransaction fragTransaction2 = activity.getSupportFragmentManager().beginTransaction();
fragTransaction2.attach(fragmentMastodonTimeline);
fragTransaction2.commit();
}
@ -340,7 +339,7 @@ public class PinnedTimelineHelper {
popup.setOnMenuItemClickListener(item -> {
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
item.setActionView(new View(context));
item.setActionView(new View(activity));
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
@ -360,7 +359,7 @@ public class PinnedTimelineHelper {
pinned.pinnedTimelines.get(offSetPosition).tagTimeline = tagTimeline;
itemMediaOnly.setChecked(mediaOnly[0]);
try {
new Pinned(context).updatePinned(pinned);
new Pinned(activity).updatePinned(pinned);
} catch (DBException e) {
e.printStackTrace();
}
@ -370,14 +369,14 @@ public class PinnedTimelineHelper {
pinned.pinnedTimelines.get(offSetPosition).tagTimeline = tagTimeline;
itemShowNSFW.setChecked(showNSFW[0]);
try {
new Pinned(context).updatePinned(pinned);
new Pinned(activity).updatePinned(pinned);
} catch (DBException e) {
e.printStackTrace();
}
} else if (itemId == R.id.action_any) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, Helper.dialogStyle());
LayoutInflater inflater = ((BaseMainActivity) context).getLayoutInflater();
View dialogView = inflater.inflate(R.layout.tags_any, new LinearLayout(context), false);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity, Helper.dialogStyle());
LayoutInflater inflater = ((BaseMainActivity) activity).getLayoutInflater();
View dialogView = inflater.inflate(R.layout.tags_any, new LinearLayout(activity), false);
dialogBuilder.setView(dialogView);
final EditText editText = dialogView.findViewById(R.id.filter_any);
if (tagTimeline.any != null) {
@ -392,7 +391,7 @@ public class PinnedTimelineHelper {
tagTimeline.any = new ArrayList<>(Arrays.asList(values));
pinned.pinnedTimelines.get(offSetPosition).tagTimeline = tagTimeline;
try {
new Pinned(context).updatePinned(pinned);
new Pinned(activity).updatePinned(pinned);
} catch (DBException e) {
e.printStackTrace();
}
@ -404,9 +403,9 @@ public class PinnedTimelineHelper {
LayoutInflater inflater;
View dialogView;
AlertDialog alertDialog;
dialogBuilder = new AlertDialog.Builder(context, Helper.dialogStyle());
inflater = ((BaseMainActivity) context).getLayoutInflater();
dialogView = inflater.inflate(R.layout.tags_all, new LinearLayout(context), false);
dialogBuilder = new AlertDialog.Builder(activity, Helper.dialogStyle());
inflater = ((BaseMainActivity) activity).getLayoutInflater();
dialogView = inflater.inflate(R.layout.tags_all, new LinearLayout(activity), false);
dialogBuilder.setView(dialogView);
final EditText editTextAll = dialogView.findViewById(R.id.filter_all);
if (tagTimeline.all != null) {
@ -421,7 +420,7 @@ public class PinnedTimelineHelper {
tagTimeline.all = new ArrayList<>(Arrays.asList(values));
pinned.pinnedTimelines.get(offSetPosition).tagTimeline = tagTimeline;
try {
new Pinned(context).updatePinned(pinned);
new Pinned(activity).updatePinned(pinned);
} catch (DBException e) {
e.printStackTrace();
}
@ -433,9 +432,9 @@ public class PinnedTimelineHelper {
LayoutInflater inflater;
View dialogView;
AlertDialog alertDialog;
dialogBuilder = new AlertDialog.Builder(context, Helper.dialogStyle());
inflater = ((BaseMainActivity) context).getLayoutInflater();
dialogView = inflater.inflate(R.layout.tags_all, new LinearLayout(context), false);
dialogBuilder = new AlertDialog.Builder(activity, Helper.dialogStyle());
inflater = ((BaseMainActivity) activity).getLayoutInflater();
dialogView = inflater.inflate(R.layout.tags_all, new LinearLayout(activity), false);
dialogBuilder.setView(dialogView);
final EditText editTextNone = dialogView.findViewById(R.id.filter_all);
if (tagTimeline.none != null) {
@ -450,7 +449,7 @@ public class PinnedTimelineHelper {
tagTimeline.none = new ArrayList<>(Arrays.asList(values));
pinned.pinnedTimelines.get(offSetPosition).tagTimeline = tagTimeline;
try {
new Pinned(context).updatePinned(pinned);
new Pinned(activity).updatePinned(pinned);
} catch (DBException e) {
e.printStackTrace();
}
@ -462,9 +461,9 @@ public class PinnedTimelineHelper {
LayoutInflater inflater;
View dialogView;
AlertDialog alertDialog;
dialogBuilder = new AlertDialog.Builder(context, Helper.dialogStyle());
inflater = ((BaseMainActivity) context).getLayoutInflater();
dialogView = inflater.inflate(R.layout.tags_name, new LinearLayout(context), false);
dialogBuilder = new AlertDialog.Builder(activity, Helper.dialogStyle());
inflater = ((BaseMainActivity) activity).getLayoutInflater();
dialogView = inflater.inflate(R.layout.tags_name, new LinearLayout(activity), false);
dialogBuilder.setView(dialogView);
final EditText editTextName = dialogView.findViewById(R.id.column_name);
if (tagTimeline.displayName != null) {
@ -478,7 +477,7 @@ public class PinnedTimelineHelper {
tagTimeline.displayName = values;
pinned.pinnedTimelines.get(offSetPosition).tagTimeline = tagTimeline;
try {
new Pinned(context).updatePinned(pinned);
new Pinned(activity).updatePinned(pinned);
} catch (DBException e) {
e.printStackTrace();
}
@ -495,15 +494,15 @@ public class PinnedTimelineHelper {
/**
* Manage long clicks on followed instances
*
* @param context - Context of the activity
* @param activity - BaseMainActivity activity
* @param pinned - {@link Pinned}
* @param view - View
* @param position - int position of the tab
*/
public static void instanceClick(Context context, Pinned pinned, View view, ActivityMainBinding activityMainBinding, int position) {
public static void instanceClick(BaseMainActivity activity, Pinned pinned, View view, ActivityMainBinding activityMainBinding, int position) {
PopupMenu popup = new PopupMenu(new ContextThemeWrapper(context, Helper.popupStyle()), view);
int toRemove = itemToRemoveInBottomMenu(context);
PopupMenu popup = new PopupMenu(new ContextThemeWrapper(activity, Helper.popupStyle()), view);
int toRemove = itemToRemoveInBottomMenu(activity);
int offSetPosition = position - (BOTTOM_TIMELINE_COUNT - toRemove);
RemoteInstance remoteInstance = pinned.pinnedTimelines.get(offSetPosition).remoteInstance;
if (remoteInstance == null)
@ -512,16 +511,16 @@ public class PinnedTimelineHelper {
final boolean[] changes = {false};
String title;
if (currentFilter[0] == null) {
title = "" + context.getString(R.string.all);
title = "" + activity.getString(R.string.all);
} else {
title = context.getString(R.string.all);
title = activity.getString(R.string.all);
}
MenuItem itemAll = popup.getMenu().add(0, 0, Menu.NONE, title);
itemAll.setOnMenuItemClickListener(item -> {
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
item.setActionView(new View(context));
item.setActionView(new View(activity));
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
@ -536,21 +535,21 @@ public class PinnedTimelineHelper {
changes[0] = true;
FragmentMastodonTimeline fragmentMastodonTimeline = null;
if (activityMainBinding.viewPager.getAdapter() != null) {
Fragment fragment = (Fragment) activityMainBinding.viewPager.getAdapter().instantiateItem(activityMainBinding.viewPager, activityMainBinding.tabLayout.getSelectedTabPosition());
Fragment fragment = (Fragment) activity.getSupportFragmentManager().findFragmentByTag("f" + activityMainBinding.viewPager.getCurrentItem());
if (fragment instanceof FragmentMastodonTimeline && fragment.isVisible()) {
fragmentMastodonTimeline = ((FragmentMastodonTimeline) fragment);
}
}
if (fragmentMastodonTimeline == null)
return false;
FragmentTransaction fragTransaction1 = ((BaseMainActivity) context).getSupportFragmentManager().beginTransaction();
FragmentTransaction fragTransaction1 = ((BaseMainActivity) activity).getSupportFragmentManager().beginTransaction();
pinned.pinnedTimelines.get(offSetPosition).remoteInstance.filteredWith = null;
remoteInstance.filteredWith = null;
currentFilter[0] = null;
pinned.pinnedTimelines.get(offSetPosition).remoteInstance = remoteInstance;
try {
new Pinned(context).updatePinned(pinned);
new Pinned(activity).updatePinned(pinned);
} catch (DBException e) {
e.printStackTrace();
}
@ -561,7 +560,7 @@ public class PinnedTimelineHelper {
bundle.putString("timelineId", remoteInstance.id);
bundle.putSerializable(Helper.ARG_TIMELINE_TYPE, Timeline.TimeLineEnum.REMOTE);
fragmentMastodonTimeline.setArguments(bundle);
FragmentTransaction fragTransaction2 = ((BaseMainActivity) context).getSupportFragmentManager().beginTransaction();
FragmentTransaction fragTransaction2 = ((BaseMainActivity) activity).getSupportFragmentManager().beginTransaction();
fragTransaction2.attach(fragmentMastodonTimeline);
fragTransaction2.commit();
popup.getMenu().close();
@ -583,19 +582,19 @@ public class PinnedTimelineHelper {
item.setOnMenuItemClickListener(item1 -> {
FragmentMastodonTimeline fragmentMastodonTimeline = null;
if (activityMainBinding.viewPager.getAdapter() != null) {
Fragment fragment = (Fragment) activityMainBinding.viewPager.getAdapter().instantiateItem(activityMainBinding.viewPager, activityMainBinding.tabLayout.getSelectedTabPosition());
Fragment fragment = (Fragment) activity.getSupportFragmentManager().findFragmentByTag("f" + activityMainBinding.viewPager.getCurrentItem());
if (fragment instanceof FragmentMastodonTimeline && fragment.isVisible()) {
fragmentMastodonTimeline = ((FragmentMastodonTimeline) fragment);
fragmentMastodonTimeline.refreshAllAdapters();
}
}
FragmentTransaction fragTransaction1 = ((BaseMainActivity) context).getSupportFragmentManager().beginTransaction();
FragmentTransaction fragTransaction1 = ((BaseMainActivity) activity).getSupportFragmentManager().beginTransaction();
if (fragmentMastodonTimeline == null)
return false;
pinned.pinnedTimelines.get(offSetPosition).remoteInstance.filteredWith = tag;
remoteInstance.filteredWith = tag;
try {
new Pinned(context).updatePinned(pinned);
new Pinned(activity).updatePinned(pinned);
} catch (DBException e) {
e.printStackTrace();
}
@ -608,7 +607,7 @@ public class PinnedTimelineHelper {
bundle.putString("currentfilter", remoteInstance.filteredWith);
bundle.putSerializable(Helper.ARG_TIMELINE_TYPE, Timeline.TimeLineEnum.REMOTE);
fragmentMastodonTimeline.setArguments(bundle);
FragmentTransaction fragTransaction2 = ((BaseMainActivity) context).getSupportFragmentManager().beginTransaction();
FragmentTransaction fragTransaction2 = ((BaseMainActivity) activity).getSupportFragmentManager().beginTransaction();
fragTransaction2.attach(fragmentMastodonTimeline);
fragTransaction2.commit();
return false;
@ -617,10 +616,10 @@ public class PinnedTimelineHelper {
}
MenuItem itemadd = popup.getMenu().add(0, 0, Menu.NONE, context.getString(R.string.add_tags));
MenuItem itemadd = popup.getMenu().add(0, 0, Menu.NONE, activity.getString(R.string.add_tags));
itemadd.setOnMenuItemClickListener(item -> {
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
item.setActionView(new View(context));
item.setActionView(new View(activity));
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
@ -633,9 +632,9 @@ public class PinnedTimelineHelper {
}
});
changes[0] = true;
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, Helper.dialogStyle());
LayoutInflater inflater = ((BaseMainActivity) context).getLayoutInflater();
View dialogView = inflater.inflate(R.layout.tags_instance, new LinearLayout(context), false);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity, Helper.dialogStyle());
LayoutInflater inflater = ((BaseMainActivity) activity).getLayoutInflater();
View dialogView = inflater.inflate(R.layout.tags_instance, new LinearLayout(activity), false);
dialogBuilder.setView(dialogView);
final EditText editText = dialogView.findViewById(R.id.filter_words);
if (remoteInstance.tags != null) {
@ -649,13 +648,13 @@ public class PinnedTimelineHelper {
String[] values = editText.getText().toString().trim().split("\\s+");
remoteInstance.tags = new ArrayList<>(Arrays.asList(values));
try {
new Pinned(context).updatePinned(pinned);
new Pinned(activity).updatePinned(pinned);
} catch (DBException e) {
e.printStackTrace();
}
popup.getMenu().clear();
popup.getMenu().close();
instanceClick(context, pinned, view, activityMainBinding, position);
instanceClick(activity, pinned, view, activityMainBinding, position);
});
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
@ -666,13 +665,13 @@ public class PinnedTimelineHelper {
if (changes[0]) {
FragmentMastodonTimeline fragmentMastodonTimeline = null;
if (activityMainBinding.viewPager.getAdapter() != null) {
Fragment fragment = (Fragment) activityMainBinding.viewPager.getAdapter().instantiateItem(activityMainBinding.viewPager, activityMainBinding.tabLayout.getSelectedTabPosition());
Fragment fragment = (Fragment) activity.getSupportFragmentManager().findFragmentByTag("f" + activityMainBinding.viewPager.getCurrentItem());
if (fragment instanceof FragmentMastodonTimeline && fragment.isVisible()) {
fragmentMastodonTimeline = ((FragmentMastodonTimeline) fragment);
fragmentMastodonTimeline.refreshAllAdapters();
}
}
FragmentTransaction fragTransaction1 = ((BaseMainActivity) context).getSupportFragmentManager().beginTransaction();
FragmentTransaction fragTransaction1 = ((BaseMainActivity) activity).getSupportFragmentManager().beginTransaction();
if (fragmentMastodonTimeline == null)
return;
fragTransaction1.detach(fragmentMastodonTimeline).commit();
@ -685,7 +684,7 @@ public class PinnedTimelineHelper {
}
bundle.putSerializable(Helper.ARG_TIMELINE_TYPE, Timeline.TimeLineEnum.REMOTE);
fragmentMastodonTimeline.setArguments(bundle);
FragmentTransaction fragTransaction2 = ((BaseMainActivity) context).getSupportFragmentManager().beginTransaction();
FragmentTransaction fragTransaction2 = ((BaseMainActivity) activity).getSupportFragmentManager().beginTransaction();
fragTransaction2.attach(fragmentMastodonTimeline);
fragTransaction2.commit();
}

View file

@ -14,15 +14,15 @@ package app.fedilab.android.ui.pageadapter;
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import app.fedilab.android.BaseMainActivity;
import app.fedilab.android.client.entities.app.BottomMenu;
import app.fedilab.android.client.entities.app.Pinned;
import app.fedilab.android.client.entities.app.PinnedTimeline;
@ -33,44 +33,24 @@ import app.fedilab.android.ui.fragment.timeline.FragmentMastodonConversation;
import app.fedilab.android.ui.fragment.timeline.FragmentMastodonTimeline;
import app.fedilab.android.ui.fragment.timeline.FragmentNotificationContainer;
public class FedilabPageAdapter extends FragmentStatePagerAdapter {
public class FedilabPageAdapter extends FragmentStateAdapter {
public static final int BOTTOM_TIMELINE_COUNT = 5; //home, local, public, notification, DM
private final Pinned pinned;
private final BottomMenu bottomMenu;
private final Context context;
private final int toRemove;
private Fragment mCurrentFragment;
public FedilabPageAdapter(Context context, FragmentManager fm, Pinned pinned, BottomMenu bottomMenu) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
public FedilabPageAdapter(BaseMainActivity activity, FragmentActivity fa, Pinned pinned, BottomMenu bottomMenu) {
super(fa);
this.pinned = pinned;
this.bottomMenu = bottomMenu;
this.context = context;
toRemove = PinnedTimelineHelper.itemToRemoveInBottomMenu(context);
toRemove = PinnedTimelineHelper.itemToRemoveInBottomMenu(activity);
}
public Fragment getCurrentFragment() {
return mCurrentFragment;
}
@Override
public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
if (getCurrentFragment() != object) {
mCurrentFragment = ((Fragment) object);
}
super.setPrimaryItem(container, position, object);
}
@Override
public int getItemPosition(@NonNull Object object) {
// POSITION_NONE makes it possible to reload the PagerAdapter
return POSITION_NONE;
}
@NonNull
@Override
public Fragment getItem(int position) {
public Fragment createFragment(int position) {
FragmentMastodonTimeline fragment = new FragmentMastodonTimeline();
Bundle bundle = new Bundle();
//Position 3 is for notifications
@ -116,11 +96,12 @@ public class FedilabPageAdapter extends FragmentStatePagerAdapter {
}
@Override
public int getCount() {
public int getItemCount() {
if (pinned != null && pinned.pinnedTimelines != null) {
return pinned.pinnedTimelines.size() + BOTTOM_TIMELINE_COUNT - toRemove;
} else {
return BOTTOM_TIMELINE_COUNT - toRemove;
}
}
}

View file

@ -75,9 +75,8 @@
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"