Migrate to ViewPager2 in AccountSavedThingActivity.

This commit is contained in:
Alex Ning 2020-09-23 09:41:51 +08:00
parent 31c32148bc
commit 3aa36ff74f
6 changed files with 83 additions and 94 deletions

View File

@ -7,21 +7,23 @@ import android.view.KeyEvent;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.view.Window; import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter; import androidx.lifecycle.Lifecycle;
import androidx.viewpager.widget.ViewPager; import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.r0adkll.slidr.Slidr; import com.r0adkll.slidr.Slidr;
import com.r0adkll.slidr.model.SlidrInterface; import com.r0adkll.slidr.model.SlidrInterface;
@ -68,7 +70,7 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT
@BindView(R.id.tab_layout_tab_layout_account_saved_thing_activity_activity) @BindView(R.id.tab_layout_tab_layout_account_saved_thing_activity_activity)
TabLayout tabLayout; TabLayout tabLayout;
@BindView(R.id.view_pager_account_saved_thing_activity) @BindView(R.id.view_pager_account_saved_thing_activity)
ViewPager viewPager; ViewPager2 viewPager2;
@Inject @Inject
@Named("oauth") @Named("oauth")
Retrofit mOauthRetrofit; Retrofit mOauthRetrofit;
@ -82,6 +84,7 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT
SharedPreferences mPostLayoutSharedPreferences; SharedPreferences mPostLayoutSharedPreferences;
@Inject @Inject
CustomThemeWrapper mCustomThemeWrapper; CustomThemeWrapper mCustomThemeWrapper;
private FragmentManager fragmentManager;
private SectionsPagerAdapter sectionsPagerAdapter; private SectionsPagerAdapter sectionsPagerAdapter;
private SlidrInterface mSlidrInterface; private SlidrInterface mSlidrInterface;
private Menu mMenu; private Menu mMenu;
@ -137,6 +140,8 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT
postLayoutBottomSheetFragment = new PostLayoutBottomSheetFragment(); postLayoutBottomSheetFragment = new PostLayoutBottomSheetFragment();
fragmentManager = getSupportFragmentManager();
if (savedInstanceState != null) { if (savedInstanceState != null) {
mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE);
mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE);
@ -191,22 +196,24 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT
} }
private void initializeViewPager() { private void initializeViewPager() {
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); sectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager, getLifecycle());
viewPager.setAdapter(sectionsPagerAdapter); viewPager2.setAdapter(sectionsPagerAdapter);
viewPager.setOffscreenPageLimit(2); viewPager2.setOffscreenPageLimit(2);
tabLayout.setupWithViewPager(viewPager); viewPager2.setUserInputEnabled(!mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_SWIPING_BETWEEN_TABS, false));
new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> {
switch (position) {
case 0:
tab.setText(R.string.posts);
break;
case 1:
tab.setText(R.string.comments);
break;
}
}).attach();
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override @Override
public void onPageSelected(int position) { public void onPageSelected(int position) {
if (isInLazyMode) {
if (viewPager.getCurrentItem() == 0) {
sectionsPagerAdapter.resumeLazyMode();
} else {
sectionsPagerAdapter.pauseLazyMode();
}
}
if (position == 0) { if (position == 0) {
unlockSwipeRightToGoBack(); unlockSwipeRightToGoBack();
} else { } else {
@ -326,17 +333,15 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT
} }
} }
private class SectionsPagerAdapter extends FragmentPagerAdapter { private class SectionsPagerAdapter extends FragmentStateAdapter {
private PostFragment postFragment;
private CommentsListingFragment commentsListingFragment;
SectionsPagerAdapter(FragmentManager fm) { SectionsPagerAdapter(FragmentManager fm, Lifecycle lifecycle) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); super(fm, lifecycle);
} }
@NonNull @NonNull
@Override @Override
public Fragment getItem(int position) { public Fragment createFragment(int position) {
if (position == 0) { if (position == 0) {
PostFragment fragment = new PostFragment(); PostFragment fragment = new PostFragment();
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
@ -359,96 +364,75 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT
return fragment; return fragment;
} }
@Override @Nullable
public int getCount() { private Fragment getCurrentFragment() {
return 2; if (viewPager2 == null || fragmentManager == null) {
} return null;
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Posts";
case 1:
return "Comments";
} }
return null; return fragmentManager.findFragmentByTag("f" + viewPager2.getCurrentItem());
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
switch (position) {
case 0:
postFragment = (PostFragment) fragment;
break;
case 1:
commentsListingFragment = (CommentsListingFragment) fragment;
}
return fragment;
} }
public boolean handleKeyDown(int keyCode) { public boolean handleKeyDown(int keyCode) {
return viewPager.getCurrentItem() == 0 && postFragment.handleKeyDown(keyCode); if (viewPager2.getCurrentItem() == 0) {
Fragment fragment = getCurrentFragment();
if (fragment instanceof PostFragment) {
return ((PostFragment) fragment).handleKeyDown(keyCode);
}
}
return false;
} }
public void refresh() { public void refresh() {
if (viewPager.getCurrentItem() == 0) { Fragment fragment = getCurrentFragment();
if (postFragment != null) { if (fragment instanceof PostFragment) {
postFragment.refresh(); ((PostFragment) fragment).refresh();
} } else if (fragment instanceof CommentsListingFragment) {
} else { ((CommentsListingFragment) fragment).refresh();
if (commentsListingFragment != null) {
commentsListingFragment.refresh();
}
} }
} }
boolean startLazyMode() { boolean startLazyMode() {
if (postFragment != null) { Fragment fragment = getCurrentFragment();
return ((FragmentCommunicator) postFragment).startLazyMode(); if (fragment instanceof FragmentCommunicator) {
return ((FragmentCommunicator) fragment).startLazyMode();
} }
return false; return false;
} }
void stopLazyMode() { void stopLazyMode() {
if (postFragment != null) { Fragment fragment = getCurrentFragment();
((FragmentCommunicator) postFragment).stopLazyMode(); if (fragment instanceof FragmentCommunicator) {
} ((FragmentCommunicator) fragment).stopLazyMode();
}
void resumeLazyMode() {
if (postFragment != null) {
((FragmentCommunicator) postFragment).resumeLazyMode(false);
}
}
void pauseLazyMode() {
if (postFragment != null) {
((FragmentCommunicator) postFragment).pauseLazyMode(false);
} }
} }
public void changeNSFW(boolean nsfw) { public void changeNSFW(boolean nsfw) {
if (postFragment != null) { Fragment fragment = getCurrentFragment();
postFragment.changeNSFW(nsfw); if (fragment instanceof PostFragment) {
((PostFragment) fragment).changeNSFW(nsfw);
} }
} }
public void changePostLayout(int postLayout) { public void changePostLayout(int postLayout) {
if (postFragment != null) { Fragment fragment = getCurrentFragment();
((FragmentCommunicator) postFragment).changePostLayout(postLayout); if (fragment instanceof PostFragment) {
((PostFragment) fragment).changePostLayout(postLayout);
} }
} }
public void goBackToTop() { public void goBackToTop() {
if (viewPager.getCurrentItem() == 0) { Fragment fragment = getCurrentFragment();
postFragment.goBackToTop(); if (fragment instanceof PostFragment) {
} else { ((PostFragment) fragment).goBackToTop();
commentsListingFragment.goBackToTop(); } else if (fragment instanceof CommentsListingFragment) {
((CommentsListingFragment) fragment).goBackToTop();
} }
} }
@Override
public int getItemCount() {
return 2;
}
} }
} }

View File

@ -217,7 +217,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
} }
private void initializeViewPager() { private void initializeViewPager() {
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), getLifecycle()); sectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager, getLifecycle());
viewPager2.setAdapter(sectionsPagerAdapter); viewPager2.setAdapter(sectionsPagerAdapter);
viewPager2.setOffscreenPageLimit(3); viewPager2.setOffscreenPageLimit(3);
viewPager2.setUserInputEnabled(!mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_SWIPING_BETWEEN_TABS, false)); viewPager2.setUserInputEnabled(!mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_SWIPING_BETWEEN_TABS, false));
@ -235,13 +235,13 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> { new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> {
switch (position) { switch (position) {
case 0: case 0:
tab.setText("Posts"); tab.setText(R.string.posts);
break; break;
case 1: case 1:
tab.setText("Comments"); tab.setText(R.string.comments);
break; break;
case 2: case 2:
tab.setText("Users"); tab.setText(R.string.users);
break; break;
} }
}).attach(); }).attach();

View File

@ -644,7 +644,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
} }
}).execute(); }).execute();
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), getLifecycle()); sectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager, getLifecycle());
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override @Override
public void onPageSelected(int position) { public void onPageSelected(int position) {
@ -670,9 +670,9 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> { new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> {
switch (position) { switch (position) {
case 0: case 0:
tab.setText("Posts"); tab.setText(R.string.posts);
case 1: case 1:
tab.setText("Sidebar"); tab.setText(R.string.sidebar);
} }
}).attach(); }).attach();

View File

@ -514,16 +514,18 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
} }
private void initializeViewPager() { private void initializeViewPager() {
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), getLifecycle()); sectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager, getLifecycle());
viewPager2.setAdapter(sectionsPagerAdapter); viewPager2.setAdapter(sectionsPagerAdapter);
viewPager2.setOffscreenPageLimit(2); viewPager2.setOffscreenPageLimit(2);
viewPager2.setUserInputEnabled(!mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_SWIPING_BETWEEN_TABS, false)); viewPager2.setUserInputEnabled(!mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_SWIPING_BETWEEN_TABS, false));
new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> { new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> {
switch (position) { switch (position) {
case 0: case 0:
tab.setText("Posts"); tab.setText(R.string.posts);
break;
case 1: case 1:
tab.setText("Comments"); tab.setText(R.string.comments);
break;
} }
}).attach(); }).attach();

View File

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

View File

@ -187,6 +187,9 @@
<string name="subreddits">Subreddits</string> <string name="subreddits">Subreddits</string>
<string name="users">Users</string> <string name="users">Users</string>
<string name="multi_reddits">MultiReddits</string> <string name="multi_reddits">MultiReddits</string>
<string name="posts">Posts</string>
<string name="comments">Comments</string>
<string name="sidebar">Sidebar</string>
<string name="bottom_sheet_post_text">Text</string> <string name="bottom_sheet_post_text">Text</string>
<string name="bottom_sheet_post_link">Link</string> <string name="bottom_sheet_post_link">Link</string>