aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2020-09-23 01:41:51 +0000
committerAlex Ning <chineseperson5@gmail.com>2020-09-23 01:41:51 +0000
commit3aa36ff74f38191efb8f4a04234974a57eccaf71 (patch)
tree400b3d900b657b24faad6c09bc87a4b1248fe4cc
parent31c32148bcd52250885e400ffd90fff1e08b384c (diff)
downloadinfinity-for-reddit-3aa36ff74f38191efb8f4a04234974a57eccaf71.tar
infinity-for-reddit-3aa36ff74f38191efb8f4a04234974a57eccaf71.tar.gz
infinity-for-reddit-3aa36ff74f38191efb8f4a04234974a57eccaf71.tar.bz2
infinity-for-reddit-3aa36ff74f38191efb8f4a04234974a57eccaf71.tar.lz
infinity-for-reddit-3aa36ff74f38191efb8f4a04234974a57eccaf71.tar.xz
infinity-for-reddit-3aa36ff74f38191efb8f4a04234974a57eccaf71.tar.zst
infinity-for-reddit-3aa36ff74f38191efb8f4a04234974a57eccaf71.zip
Migrate to ViewPager2 in AccountSavedThingActivity.
Diffstat (limited to '')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountSavedThingActivity.java150
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java8
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java6
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java8
-rw-r--r--app/src/main/res/layout/activity_account_saved_thing.xml2
-rw-r--r--app/src/main/res/values/strings.xml3
6 files changed, 83 insertions, 94 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountSavedThingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountSavedThingActivity.java
index 00416019..6da1325d 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountSavedThingActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountSavedThingActivity.java
@@ -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;
+ }
}
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java
index fede5b48..78f39e19 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java
@@ -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();
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java
index b8686ad3..8089a65b 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java
@@ -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();
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java
index e56345a2..b793ad8b 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java
@@ -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();
diff --git a/app/src/main/res/layout/activity_account_saved_thing.xml b/app/src/main/res/layout/activity_account_saved_thing.xml
index 57f61d2c..d297b727 100644
--- a/app/src/main/res/layout/activity_account_saved_thing.xml
+++ b/app/src/main/res/layout/activity_account_saved_thing.xml
@@ -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"
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 6020561e..b1c6893c 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -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>