ViewPager2 - end

This commit is contained in:
Thomas 2022-06-22 15:07:42 +02:00
parent 91c0f0de75
commit 5ddc756440
18 changed files with 101 additions and 175 deletions

View file

@ -21,7 +21,6 @@ import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Point; import android.graphics.Point;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
@ -34,20 +33,15 @@ import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.view.Window; import android.view.Window;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentStatePagerAdapter; import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.preference.PreferenceManager; import androidx.viewpager2.widget.ViewPager2;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
@ -99,7 +93,6 @@ public class MediaActivity extends BaseActivity implements OnDownloadInterface {
private int minTouch, maxTouch; private int minTouch, maxTouch;
private float startX; private float startX;
private float startY; private float startY;
private FragmentMedia mCurrentFragment;
private ActivityMediaPagerBinding binding; private ActivityMediaPagerBinding binding;
@Override @Override
@ -113,8 +106,6 @@ public class MediaActivity extends BaseActivity implements OnDownloadInterface {
fullscreen = false; fullscreen = false;
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(MediaActivity.this);
final int med_desc_timeout = sharedpreferences.getInt(getString(R.string.SET_MED_DESC_TIMEOUT), 3) * 1000;
flags = getWindow().getDecorView().getSystemUiVisibility(); flags = getWindow().getDecorView().getSystemUiVisibility();
Bundle b = getIntent().getExtras(); Bundle b = getIntent().getExtras();
if (b != null) { if (b != null) {
@ -132,11 +123,10 @@ public class MediaActivity extends BaseActivity implements OnDownloadInterface {
setTitle(""); setTitle("");
PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); ScreenSlidePagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(MediaActivity.this);
binding.mediaViewpager.setAdapter(mPagerAdapter); binding.mediaViewpager.setAdapter(mPagerAdapter);
binding.mediaViewpager.setCurrentItem(mediaPosition - 1); binding.mediaViewpager.setCurrentItem(mediaPosition - 1);
binding.mediaViewpager.setOffscreenPageLimit(0);
binding.haulerView.setOnDragDismissedListener(dragDirection -> ActivityCompat.finishAfterTransition(MediaActivity.this)); binding.haulerView.setOnDragDismissedListener(dragDirection -> ActivityCompat.finishAfterTransition(MediaActivity.this));
registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
String description = attachments.get(mediaPosition - 1).description; String description = attachments.get(mediaPosition - 1).description;
@ -145,14 +135,15 @@ public class MediaActivity extends BaseActivity implements OnDownloadInterface {
binding.mediaDescription.setText(description); binding.mediaDescription.setText(description);
} }
binding.mediaViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { binding.mediaViewpager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
public void onPageScrollStateChanged(int state) { @Override
}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
} }
@Override
public void onPageSelected(int position) { public void onPageSelected(int position) {
super.onPageSelected(position);
String description = attachments.get(position).description; String description = attachments.get(position).description;
if (handler != null) { if (handler != null) {
handler.removeCallbacksAndMessages(null); handler.removeCallbacksAndMessages(null);
@ -162,8 +153,12 @@ public class MediaActivity extends BaseActivity implements OnDownloadInterface {
binding.mediaDescription.setText(description); binding.mediaDescription.setText(description);
} }
} }
});
@Override
public void onPageScrollStateChanged(int state) {
super.onPageScrollStateChanged(state);
}
});
setFullscreen(true); setFullscreen(true);
Display display = getWindowManager().getDefaultDisplay(); Display display = getWindowManager().getDefaultDisplay();
@ -258,8 +253,6 @@ public class MediaActivity extends BaseActivity implements OnDownloadInterface {
@Override @Override
public boolean dispatchTouchEvent(MotionEvent event) { public boolean dispatchTouchEvent(MotionEvent event) {
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(MediaActivity.this);
final int med_desc_timeout = sharedpreferences.getInt(getString(R.string.SET_MED_DESC_TIMEOUT), 3) * 1000;
switch (event.getAction()) { switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
startX = event.getX(); startX = event.getX();
@ -307,10 +300,6 @@ public class MediaActivity extends BaseActivity implements OnDownloadInterface {
super.onDestroy(); super.onDestroy();
} }
public FragmentMedia getCurrentFragment() {
return mCurrentFragment;
}
@Override @Override
public void onDownloaded(String saveFilePath, String downloadUrl, Error error) { public void onDownloaded(String saveFilePath, String downloadUrl, Error error) {
@ -372,15 +361,15 @@ public class MediaActivity extends BaseActivity implements OnDownloadInterface {
/** /**
* Media Pager * Media Pager
*/ */
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { private class ScreenSlidePagerAdapter extends FragmentStateAdapter {
ScreenSlidePagerAdapter(FragmentManager fm) { ScreenSlidePagerAdapter(FragmentActivity fa) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); super(fa);
} }
@NotNull @NonNull
@Override @Override
public Fragment getItem(int position) { public Fragment createFragment(int position) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
FragmentMedia mediaSliderFragment = new FragmentMedia(); FragmentMedia mediaSliderFragment = new FragmentMedia();
bundle.putInt(Helper.ARG_MEDIA_POSITION, position); bundle.putInt(Helper.ARG_MEDIA_POSITION, position);
@ -390,15 +379,7 @@ public class MediaActivity extends BaseActivity implements OnDownloadInterface {
} }
@Override @Override
public void setPrimaryItem(@NotNull ViewGroup container, int position, @NotNull Object object) { public int getItemCount() {
if (getCurrentFragment() != object) {
mCurrentFragment = ((FragmentMedia) object);
}
super.setPrimaryItem(container, position, object);
}
@Override
public int getCount() {
return attachments.size(); return attachments.size();
} }
} }

View file

@ -59,6 +59,7 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget; import com.bumptech.glide.request.target.CustomTarget;
@ -125,7 +126,7 @@ public class ProfileActivity extends BaseActivity {
if (b != null) { if (b != null) {
Account accountReceived = (Account) b.getSerializable(Helper.ARG_ACCOUNT); Account accountReceived = (Account) b.getSerializable(Helper.ARG_ACCOUNT);
if (b.getBoolean(Helper.RECEIVE_REDRAW_PROFILE, false) && accountReceived != null) { if (b.getBoolean(Helper.RECEIVE_REDRAW_PROFILE, false) && accountReceived != null) {
if (account != null && accountReceived.id.equalsIgnoreCase(account.id)) { if (account != null && accountReceived.id != null && account.id != null && accountReceived.id.equalsIgnoreCase(account.id)) {
initializeView(accountReceived); initializeView(accountReceived);
} }
} }
@ -246,15 +247,20 @@ public class ProfileActivity extends BaseActivity {
} }
binding.accountTabLayout.clearOnTabSelectedListeners(); binding.accountTabLayout.clearOnTabSelectedListeners();
binding.accountTabLayout.removeAllTabs(); binding.accountTabLayout.removeAllTabs();
binding.accountViewpager.clearOnPageChangeListeners();
//Tablayout for timelines/following/followers //Tablayout for timelines/following/followers
FedilabProfileTLPageAdapter fedilabProfileTLPageAdapter = new FedilabProfileTLPageAdapter(getSupportFragmentManager(), account); FedilabProfileTLPageAdapter fedilabProfileTLPageAdapter = new FedilabProfileTLPageAdapter(ProfileActivity.this, account);
binding.accountTabLayout.addTab(binding.accountTabLayout.newTab().setText(getString(R.string.status_cnt, Helper.withSuffix(account.statuses_count)))); binding.accountTabLayout.addTab(binding.accountTabLayout.newTab().setText(getString(R.string.status_cnt, Helper.withSuffix(account.statuses_count))));
binding.accountTabLayout.addTab(binding.accountTabLayout.newTab().setText(getString(R.string.following_cnt, Helper.withSuffix(account.following_count)))); binding.accountTabLayout.addTab(binding.accountTabLayout.newTab().setText(getString(R.string.following_cnt, Helper.withSuffix(account.following_count))));
binding.accountTabLayout.addTab(binding.accountTabLayout.newTab().setText(getString(R.string.followers_cnt, Helper.withSuffix(account.followers_count)))); binding.accountTabLayout.addTab(binding.accountTabLayout.newTab().setText(getString(R.string.followers_cnt, Helper.withSuffix(account.followers_count))));
binding.accountViewpager.setAdapter(fedilabProfileTLPageAdapter); binding.accountViewpager.setAdapter(fedilabProfileTLPageAdapter);
binding.accountViewpager.setOffscreenPageLimit(3); binding.accountViewpager.setOffscreenPageLimit(3);
binding.accountViewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(binding.accountTabLayout)); binding.accountViewpager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
binding.accountTabLayout.selectTab(binding.accountTabLayout.getTabAt(position));
}
});
binding.accountTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { binding.accountTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override @Override
public void onTabSelected(TabLayout.Tab tab) { public void onTabSelected(TabLayout.Tab tab) {

View file

@ -60,10 +60,8 @@ public class ScheduledActivity extends BaseActivity {
binding.scheduleTablayout.addTab(binding.scheduleTablayout.newTab().setText(getString(R.string.toots_client))); binding.scheduleTablayout.addTab(binding.scheduleTablayout.newTab().setText(getString(R.string.toots_client)));
binding.scheduleTablayout.addTab(binding.scheduleTablayout.newTab().setText(getString(R.string.reblog))); binding.scheduleTablayout.addTab(binding.scheduleTablayout.newTab().setText(getString(R.string.reblog)));
binding.scheduleViewpager.setAdapter(new FedilabScheduledPageAdapter(getSupportFragmentManager())); binding.scheduleViewpager.setAdapter(new FedilabScheduledPageAdapter(ScheduledActivity.this));
binding.scheduleViewpager.setOffscreenPageLimit(3); binding.scheduleViewpager.setOffscreenPageLimit(3);
binding.scheduleViewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(binding.scheduleTablayout));
binding.scheduleTablayout.setTabTextColors(ThemeHelper.getAttColor(ScheduledActivity.this, R.attr.mTextColor), ContextCompat.getColor(ScheduledActivity.this, R.color.cyanea_accent_dark_reference)); binding.scheduleTablayout.setTabTextColors(ThemeHelper.getAttColor(ScheduledActivity.this, R.attr.mTextColor), ContextCompat.getColor(ScheduledActivity.this, R.color.cyanea_accent_dark_reference));
binding.scheduleTablayout.setTabIconTint(ThemeHelper.getColorStateList(ScheduledActivity.this)); binding.scheduleTablayout.setTabIconTint(ThemeHelper.getColorStateList(ScheduledActivity.this));

View file

@ -21,7 +21,6 @@ import android.os.Bundle;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.Toast; import android.widget.Toast;
@ -29,10 +28,9 @@ import androidx.annotation.NonNull;
import androidx.appcompat.widget.SearchView; import androidx.appcompat.widget.SearchView;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentStatePagerAdapter; import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager.widget.PagerAdapter; import androidx.viewpager2.widget.ViewPager2;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayout;
@ -99,7 +97,7 @@ public class SearchResultTabActivity extends BaseActivity {
public void onTabReselected(TabLayout.Tab tab) { public void onTabReselected(TabLayout.Tab tab) {
Fragment fragment; Fragment fragment;
if (binding.searchViewpager.getAdapter() != null) { if (binding.searchViewpager.getAdapter() != null) {
fragment = (Fragment) binding.searchViewpager.getAdapter().instantiateItem(binding.searchViewpager, tab.getPosition()); fragment = (Fragment) getSupportFragmentManager().findFragmentByTag("f" + binding.searchViewpager.getCurrentItem());
if (fragment instanceof FragmentMastodonAccount) { if (fragment instanceof FragmentMastodonAccount) {
FragmentMastodonAccount fragmentMastodonAccount = ((FragmentMastodonAccount) fragment); FragmentMastodonAccount fragmentMastodonAccount = ((FragmentMastodonAccount) fragment);
fragmentMastodonAccount.scrollToTop(); fragmentMastodonAccount.scrollToTop();
@ -134,7 +132,7 @@ public class SearchResultTabActivity extends BaseActivity {
imm.hideSoftInputFromWindow(binding.searchTabLayout.getWindowToken(), 0); imm.hideSoftInputFromWindow(binding.searchTabLayout.getWindowToken(), 0);
query = query.replaceAll("^#+", ""); query = query.replaceAll("^#+", "");
search = query.trim(); search = query.trim();
PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); ScreenSlidePagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(SearchResultTabActivity.this);
binding.searchViewpager.setAdapter(mPagerAdapter); binding.searchViewpager.setAdapter(mPagerAdapter);
searchView.clearFocus(); searchView.clearFocus();
setTitle(search); setTitle(search);
@ -158,16 +156,19 @@ public class SearchResultTabActivity extends BaseActivity {
}); });
PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); ScreenSlidePagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(SearchResultTabActivity.this);
binding.searchViewpager.setAdapter(mPagerAdapter); binding.searchViewpager.setAdapter(mPagerAdapter);
binding.searchViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { binding.searchViewpager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override @Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
binding.searchTabLayout.selectTab(binding.searchTabLayout.getTabAt(position));
} }
@Override @Override
public void onPageSelected(int position) { public void onPageSelected(int position) {
super.onPageSelected(position);
TabLayout.Tab tab = binding.searchTabLayout.getTabAt(position); TabLayout.Tab tab = binding.searchTabLayout.getTabAt(position);
if (tab != null) if (tab != null)
tab.select(); tab.select();
@ -175,6 +176,7 @@ public class SearchResultTabActivity extends BaseActivity {
@Override @Override
public void onPageScrollStateChanged(int state) { public void onPageScrollStateChanged(int state) {
super.onPageScrollStateChanged(state);
} }
}); });
return true; return true;
@ -196,15 +198,16 @@ public class SearchResultTabActivity extends BaseActivity {
/** /**
* Pager adapter for the 4 fragments * Pager adapter for the 4 fragments
*/ */
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { private class ScreenSlidePagerAdapter extends FragmentStateAdapter {
ScreenSlidePagerAdapter(FragmentManager fm) { ScreenSlidePagerAdapter(FragmentActivity fa) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); super(fa);
} }
@NotNull
@NonNull
@Override @Override
public Fragment getItem(int position) { public Fragment createFragment(int position) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
switch (position) { switch (position) {
case 0: case 0:
@ -231,12 +234,7 @@ public class SearchResultTabActivity extends BaseActivity {
} }
@Override @Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { public int getItemCount() {
}
@Override
public int getCount() {
return 4; return 4;
} }
} }

View file

@ -209,6 +209,7 @@ public class PinnedTimelineHelper {
@Override @Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels); super.onPageScrolled(position, positionOffset, positionOffsetPixels);
activityMainBinding.tabLayout.selectTab(activityMainBinding.tabLayout.getTabAt(position));
} }
@Override @Override

View file

@ -98,7 +98,7 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter.
} else if (delete_statuses_for_user != null && statusAdapter != null) { } else if (delete_statuses_for_user != null && statusAdapter != null) {
List<Status> statusesToRemove = new ArrayList<>(); List<Status> statusesToRemove = new ArrayList<>();
for (Status status : statuses) { for (Status status : statuses) {
if (status.account.id.equals(delete_statuses_for_user)) { if (status != null && status.account != null && status.account.id != null && status.account.id.equals(delete_statuses_for_user)) {
statusesToRemove.add(status); statusesToRemove.add(status);
} }
} }

View file

@ -63,7 +63,7 @@ public class FragmentNotificationContainer extends Fragment {
binding.tabLayout.addTab(binding.tabLayout.newTab().setText(getString(R.string.all))); binding.tabLayout.addTab(binding.tabLayout.newTab().setText(getString(R.string.all)));
binding.tabLayout.addTab(binding.tabLayout.newTab().setText(getString(R.string.mention))); binding.tabLayout.addTab(binding.tabLayout.newTab().setText(getString(R.string.mention)));
binding.tabLayout.setTabMode(TabLayout.MODE_FIXED); binding.tabLayout.setTabMode(TabLayout.MODE_FIXED);
binding.viewpager.setAdapter(new FedilabNotificationPageAdapter(getChildFragmentManager(), false)); binding.viewpager.setAdapter(new FedilabNotificationPageAdapter(requireActivity(), false));
} else { } else {
binding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); binding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
binding.tabLayout.addTab(binding.tabLayout.newTab().setText(getString(R.string.all))); binding.tabLayout.addTab(binding.tabLayout.newTab().setText(getString(R.string.all)));
@ -73,7 +73,7 @@ public class FragmentNotificationContainer extends Fragment {
binding.tabLayout.addTab(binding.tabLayout.newTab().setIcon(R.drawable.ic_baseline_poll_24)); binding.tabLayout.addTab(binding.tabLayout.newTab().setIcon(R.drawable.ic_baseline_poll_24));
binding.tabLayout.addTab(binding.tabLayout.newTab().setIcon(R.drawable.ic_baseline_home_24)); binding.tabLayout.addTab(binding.tabLayout.newTab().setIcon(R.drawable.ic_baseline_home_24));
binding.tabLayout.addTab(binding.tabLayout.newTab().setIcon(R.drawable.ic_baseline_person_add_alt_1_24)); binding.tabLayout.addTab(binding.tabLayout.newTab().setIcon(R.drawable.ic_baseline_person_add_alt_1_24));
binding.viewpager.setAdapter(new FedilabNotificationPageAdapter(getChildFragmentManager(), true)); binding.viewpager.setAdapter(new FedilabNotificationPageAdapter(requireActivity(), true));
} }
AtomicBoolean changes = new AtomicBoolean(false); AtomicBoolean changes = new AtomicBoolean(false);
binding.settings.setOnClickListener(v -> { binding.settings.setOnClickListener(v -> {
@ -214,7 +214,6 @@ public class FragmentNotificationContainer extends Fragment {
binding.tabLayout.setTabTextColors(ThemeHelper.getAttColor(requireActivity(), R.attr.mTextColor), ContextCompat.getColor(requireActivity(), R.color.cyanea_accent_dark_reference)); binding.tabLayout.setTabTextColors(ThemeHelper.getAttColor(requireActivity(), R.attr.mTextColor), ContextCompat.getColor(requireActivity(), R.color.cyanea_accent_dark_reference));
binding.tabLayout.setTabIconTint(ThemeHelper.getColorStateList(requireActivity())); binding.tabLayout.setTabIconTint(ThemeHelper.getColorStateList(requireActivity()));
binding.viewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(binding.tabLayout));
binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override @Override
public void onTabSelected(TabLayout.Tab tab) { public void onTabSelected(TabLayout.Tab tab) {
@ -230,7 +229,7 @@ public class FragmentNotificationContainer extends Fragment {
public void onTabReselected(TabLayout.Tab tab) { public void onTabReselected(TabLayout.Tab tab) {
Fragment fragment; Fragment fragment;
if (binding.viewpager.getAdapter() != null) { if (binding.viewpager.getAdapter() != null) {
fragment = (Fragment) binding.viewpager.getAdapter().instantiateItem(binding.viewpager, tab.getPosition()); fragment = (Fragment) requireActivity().getSupportFragmentManager().findFragmentByTag("f" + binding.viewpager.getCurrentItem());
if (fragment instanceof FragmentMastodonNotification) { if (fragment instanceof FragmentMastodonNotification) {
FragmentMastodonNotification fragmentMastodonNotification = ((FragmentMastodonNotification) fragment); FragmentMastodonNotification fragmentMastodonNotification = ((FragmentMastodonNotification) fragment);
fragmentMastodonNotification.scrollToTop(); fragmentMastodonNotification.scrollToTop();
@ -245,15 +244,12 @@ public class FragmentNotificationContainer extends Fragment {
public void scrollToTop() { public void scrollToTop() {
if (binding != null) { if (binding != null) {
FedilabNotificationPageAdapter fedilabNotificationPageAdapter = ((FedilabNotificationPageAdapter) binding.viewpager.getAdapter()); FragmentMastodonNotification fragmentMastodonNotification = (FragmentMastodonNotification) requireActivity().getSupportFragmentManager().findFragmentByTag("f" + binding.viewpager.getCurrentItem());
if (fedilabNotificationPageAdapter != null) {
FragmentMastodonNotification fragmentMastodonNotification = (FragmentMastodonNotification) fedilabNotificationPageAdapter.getCurrentFragment();
if (fragmentMastodonNotification != null) { if (fragmentMastodonNotification != null) {
fragmentMastodonNotification.scrollToTop(); fragmentMastodonNotification.scrollToTop();
} }
} }
} }
}
@SuppressLint("ApplySharedPref") @SuppressLint("ApplySharedPref")
@Override @Override

View file

@ -57,10 +57,8 @@ public class FragmentProfileTimeline extends Fragment {
binding.tabLayout.addTab(binding.tabLayout.newTab().setText(getString(R.string.media))); binding.tabLayout.addTab(binding.tabLayout.newTab().setText(getString(R.string.media)));
binding.tabLayout.setTabTextColors(ThemeHelper.getAttColor(requireActivity(), R.attr.mTextColor), ContextCompat.getColor(requireActivity(), R.color.cyanea_accent_dark_reference)); binding.tabLayout.setTabTextColors(ThemeHelper.getAttColor(requireActivity(), R.attr.mTextColor), ContextCompat.getColor(requireActivity(), R.color.cyanea_accent_dark_reference));
binding.tabLayout.setTabIconTint(ThemeHelper.getColorStateList(requireActivity())); binding.tabLayout.setTabIconTint(ThemeHelper.getColorStateList(requireActivity()));
binding.viewpager.setAdapter(new FedilabProfilePageAdapter( binding.viewpager.setAdapter(new FedilabProfilePageAdapter(requireActivity(), account));
getChildFragmentManager(), account));
binding.viewpager.setOffscreenPageLimit(3); binding.viewpager.setOffscreenPageLimit(3);
binding.viewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(binding.tabLayout));
binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override @Override
public void onTabSelected(TabLayout.Tab tab) { public void onTabSelected(TabLayout.Tab tab) {
@ -74,7 +72,6 @@ public class FragmentProfileTimeline extends Fragment {
@Override @Override
public void onTabReselected(TabLayout.Tab tab) { public void onTabReselected(TabLayout.Tab tab) {
} }
}); });

View file

@ -16,43 +16,33 @@ package app.fedilab.android.ui.pageadapter;
import android.os.Bundle; import android.os.Bundle;
import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentStatePagerAdapter; import androidx.viewpager2.adapter.FragmentStateAdapter;
import app.fedilab.android.helper.Helper; import app.fedilab.android.helper.Helper;
import app.fedilab.android.ui.fragment.timeline.FragmentMastodonNotification; import app.fedilab.android.ui.fragment.timeline.FragmentMastodonNotification;
public class FedilabNotificationPageAdapter extends FragmentStatePagerAdapter { public class FedilabNotificationPageAdapter extends FragmentStateAdapter {
private final boolean extended; private final boolean extended;
private Fragment mCurrentFragment;
public FedilabNotificationPageAdapter(FragmentManager fm, boolean extended) { public FedilabNotificationPageAdapter(FragmentActivity fa, boolean extended) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); super(fa);
this.extended = extended; this.extended = extended;
} }
public Fragment getCurrentFragment() {
return mCurrentFragment;
}
@Override @Override
public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) { public int getItemCount() {
if (getCurrentFragment() != object) { return extended ? 7 : 2;
mCurrentFragment = ((Fragment) object);
}
super.setPrimaryItem(container, position, object);
} }
@NonNull @NonNull
@Override @Override
public Fragment getItem(int position) { public Fragment createFragment(int position) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
FragmentMastodonNotification fragmentMastodonNotification = new FragmentMastodonNotification(); FragmentMastodonNotification fragmentMastodonNotification = new FragmentMastodonNotification();
if (!extended) { if (!extended) {
@ -93,8 +83,5 @@ public class FedilabNotificationPageAdapter extends FragmentStatePagerAdapter {
return fragmentMastodonNotification; return fragmentMastodonNotification;
} }
@Override
public int getCount() {
return extended ? 7 : 2;
}
} }

View file

@ -15,43 +15,35 @@ package app.fedilab.android.ui.pageadapter;
* see <http://www.gnu.org/licenses>. */ * see <http://www.gnu.org/licenses>. */
import android.os.Bundle; import android.os.Bundle;
import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentStatePagerAdapter; import androidx.viewpager2.adapter.FragmentStateAdapter;
import app.fedilab.android.client.entities.api.Account; import app.fedilab.android.client.entities.api.Account;
import app.fedilab.android.client.entities.app.Timeline; import app.fedilab.android.client.entities.app.Timeline;
import app.fedilab.android.helper.Helper; import app.fedilab.android.helper.Helper;
import app.fedilab.android.ui.fragment.timeline.FragmentMastodonTimeline; import app.fedilab.android.ui.fragment.timeline.FragmentMastodonTimeline;
public class FedilabProfilePageAdapter extends FragmentStatePagerAdapter { public class FedilabProfilePageAdapter extends FragmentStateAdapter {
private final Account account; private final Account account;
private Fragment mCurrentFragment;
public FedilabProfilePageAdapter(FragmentManager fm, Account account) { public FedilabProfilePageAdapter(FragmentActivity fa, Account account) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); super(fa);
this.account = account; this.account = account;
} }
public Fragment getCurrentFragment() {
return mCurrentFragment;
}
@Override @Override
public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) { public int getItemCount() {
if (getCurrentFragment() != object) { return 3;
mCurrentFragment = ((Fragment) object);
}
super.setPrimaryItem(container, position, object);
} }
@NonNull @NonNull
@Override @Override
public Fragment getItem(int position) { public Fragment createFragment(int position) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString(Helper.ARG_VIEW_MODEL_KEY, "FEDILAB_" + position); bundle.putString(Helper.ARG_VIEW_MODEL_KEY, "FEDILAB_" + position);
switch (position) { switch (position) {
@ -85,8 +77,5 @@ public class FedilabProfilePageAdapter extends FragmentStatePagerAdapter {
} }
} }
@Override
public int getCount() {
return 3;
}
} }

View file

@ -15,12 +15,11 @@ package app.fedilab.android.ui.pageadapter;
* see <http://www.gnu.org/licenses>. */ * see <http://www.gnu.org/licenses>. */
import android.os.Bundle; import android.os.Bundle;
import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentStatePagerAdapter; import androidx.viewpager2.adapter.FragmentStateAdapter;
import app.fedilab.android.client.entities.api.Account; import app.fedilab.android.client.entities.api.Account;
import app.fedilab.android.helper.Helper; import app.fedilab.android.helper.Helper;
@ -28,31 +27,19 @@ import app.fedilab.android.ui.fragment.timeline.FragmentMastodonAccount;
import app.fedilab.android.ui.fragment.timeline.FragmentMastodonTimeline; import app.fedilab.android.ui.fragment.timeline.FragmentMastodonTimeline;
import app.fedilab.android.ui.fragment.timeline.FragmentProfileTimeline; import app.fedilab.android.ui.fragment.timeline.FragmentProfileTimeline;
public class FedilabProfileTLPageAdapter extends FragmentStatePagerAdapter { public class FedilabProfileTLPageAdapter extends FragmentStateAdapter {
private final Account account; private final Account account;
private Fragment mCurrentFragment;
public FedilabProfileTLPageAdapter(FragmentManager fm, Account account) { public FedilabProfileTLPageAdapter(FragmentActivity fa, Account account) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); super(fa);
this.account = account; this.account = account;
} }
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);
}
@NonNull @NonNull
@Override @Override
public Fragment getItem(int position) { public Fragment createFragment(int position) {
switch (position) { switch (position) {
case 0: case 0:
@ -76,7 +63,7 @@ public class FedilabProfileTLPageAdapter extends FragmentStatePagerAdapter {
} }
@Override @Override
public int getCount() { public int getItemCount() {
return 3; return 3;
} }

View file

@ -15,40 +15,26 @@ package app.fedilab.android.ui.pageadapter;
* see <http://www.gnu.org/licenses>. */ * see <http://www.gnu.org/licenses>. */
import android.os.Bundle; import android.os.Bundle;
import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentStatePagerAdapter; import androidx.viewpager2.adapter.FragmentStateAdapter;
import app.fedilab.android.client.entities.app.Timeline; import app.fedilab.android.client.entities.app.Timeline;
import app.fedilab.android.helper.Helper; import app.fedilab.android.helper.Helper;
import app.fedilab.android.ui.fragment.timeline.FragmentScheduled; import app.fedilab.android.ui.fragment.timeline.FragmentScheduled;
public class FedilabScheduledPageAdapter extends FragmentStatePagerAdapter { public class FedilabScheduledPageAdapter extends FragmentStateAdapter {
private Fragment mCurrentFragment;
public FedilabScheduledPageAdapter(FragmentManager fm) { public FedilabScheduledPageAdapter(FragmentActivity fa) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); super(fa);
}
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);
} }
@NonNull @NonNull
@Override @Override
public Fragment getItem(int position) { public Fragment createFragment(int position) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString(Helper.ARG_VIEW_MODEL_KEY, "FEDILAB_" + position); bundle.putString(Helper.ARG_VIEW_MODEL_KEY, "FEDILAB_" + position);
FragmentScheduled fragmentScheduled = new FragmentScheduled(); FragmentScheduled fragmentScheduled = new FragmentScheduled();
@ -67,7 +53,7 @@ public class FedilabScheduledPageAdapter extends FragmentStatePagerAdapter {
} }
@Override @Override
public int getCount() { public int getItemCount() {
return 3; return 3;
} }
} }

View file

@ -29,7 +29,7 @@
android:animateLayoutChanges="true" android:animateLayoutChanges="true"
android:background="@android:color/transparent"> android:background="@android:color/transparent">
<androidx.viewpager.widget.ViewPager <androidx.viewpager2.widget.ViewPager2
android:id="@+id/media_viewpager" android:id="@+id/media_viewpager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"

View file

@ -541,7 +541,7 @@
app:tabMode="fixed" /> app:tabMode="fixed" />
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager <androidx.viewpager2.widget.ViewPager2
android:id="@+id/account_viewpager" android:id="@+id/account_viewpager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View file

@ -63,7 +63,7 @@
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager <androidx.viewpager2.widget.ViewPager2
android:id="@+id/schedule_viewpager" android:id="@+id/schedule_viewpager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View file

@ -30,7 +30,7 @@
app:tabIndicatorColor="@color/cyanea_accent_dark_reference" app:tabIndicatorColor="@color/cyanea_accent_dark_reference"
app:tabMode="scrollable" /> app:tabMode="scrollable" />
<androidx.viewpager.widget.ViewPager <androidx.viewpager2.widget.ViewPager2
android:id="@+id/search_viewpager" android:id="@+id/search_viewpager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View file

@ -47,7 +47,7 @@
</androidx.appcompat.widget.LinearLayoutCompat> </androidx.appcompat.widget.LinearLayoutCompat>
<androidx.viewpager.widget.ViewPager <androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager" android:id="@+id/viewpager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />

View file

@ -27,7 +27,7 @@
app:tabGravity="fill" app:tabGravity="fill"
app:tabMaxWidth="0dp" /> app:tabMaxWidth="0dp" />
<androidx.viewpager.widget.ViewPager <androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager" android:id="@+id/viewpager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />