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

View File

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

View File

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

View File

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

View File

@ -187,6 +187,9 @@
<string name="subreddits">Subreddits</string>
<string name="users">Users</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_link">Link</string>