mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-11-07 11:17:25 +01:00
Migrate to ViewPager2 in SearchResultActivity.
This commit is contained in:
parent
58af3557e8
commit
31c32148bc
@ -8,20 +8,22 @@ 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.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;
|
||||||
|
|
||||||
@ -72,7 +74,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
|
|||||||
@BindView(R.id.tab_layout_search_result_activity)
|
@BindView(R.id.tab_layout_search_result_activity)
|
||||||
TabLayout tabLayout;
|
TabLayout tabLayout;
|
||||||
@BindView(R.id.view_pager_search_result_activity)
|
@BindView(R.id.view_pager_search_result_activity)
|
||||||
ViewPager viewPager;
|
ViewPager2 viewPager2;
|
||||||
@Inject
|
@Inject
|
||||||
RedditDataRoomDatabase mRedditDataRoomDatabase;
|
RedditDataRoomDatabase mRedditDataRoomDatabase;
|
||||||
@Inject
|
@Inject
|
||||||
@ -91,6 +93,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
|
|||||||
private String mAccountName;
|
private String mAccountName;
|
||||||
private String mQuery;
|
private String mQuery;
|
||||||
private String mSubredditName;
|
private String mSubredditName;
|
||||||
|
private FragmentManager fragmentManager;
|
||||||
private SectionsPagerAdapter sectionsPagerAdapter;
|
private SectionsPagerAdapter sectionsPagerAdapter;
|
||||||
private SearchPostSortTypeBottomSheetFragment searchPostSortTypeBottomSheetFragment;
|
private SearchPostSortTypeBottomSheetFragment searchPostSortTypeBottomSheetFragment;
|
||||||
private SortTimeBottomSheetFragment sortTimeBottomSheetFragment;
|
private SortTimeBottomSheetFragment sortTimeBottomSheetFragment;
|
||||||
@ -139,6 +142,8 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
|
|||||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||||
setToolbarGoToTop(toolbar);
|
setToolbarGoToTop(toolbar);
|
||||||
|
|
||||||
|
fragmentManager = getSupportFragmentManager();
|
||||||
|
|
||||||
if (savedInstanceState == null) {
|
if (savedInstanceState == null) {
|
||||||
getCurrentAccountAndInitializeViewPager();
|
getCurrentAccountAndInitializeViewPager();
|
||||||
} else {
|
} else {
|
||||||
@ -212,10 +217,11 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void initializeViewPager() {
|
private void initializeViewPager() {
|
||||||
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
|
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), getLifecycle());
|
||||||
viewPager.setAdapter(sectionsPagerAdapter);
|
viewPager2.setAdapter(sectionsPagerAdapter);
|
||||||
viewPager.setOffscreenPageLimit(2);
|
viewPager2.setOffscreenPageLimit(3);
|
||||||
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
|
viewPager2.setUserInputEnabled(!mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_SWIPING_BETWEEN_TABS, false));
|
||||||
|
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onPageSelected(int position) {
|
public void onPageSelected(int position) {
|
||||||
sectionsPagerAdapter.displaySortTypeInToolbar();
|
sectionsPagerAdapter.displaySortTypeInToolbar();
|
||||||
@ -226,7 +232,19 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
tabLayout.setupWithViewPager(viewPager);
|
new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> {
|
||||||
|
switch (position) {
|
||||||
|
case 0:
|
||||||
|
tab.setText("Posts");
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
tab.setText("Comments");
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
tab.setText("Users");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}).attach();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -243,7 +261,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
|
|||||||
onBackPressed();
|
onBackPressed();
|
||||||
return true;
|
return true;
|
||||||
case R.id.action_sort_search_result_activity:
|
case R.id.action_sort_search_result_activity:
|
||||||
switch (viewPager.getCurrentItem()) {
|
switch (viewPager2.getCurrentItem()) {
|
||||||
case 0: {
|
case 0: {
|
||||||
searchPostSortTypeBottomSheetFragment.show(getSupportFragmentManager(), searchPostSortTypeBottomSheetFragment.getTag());
|
searchPostSortTypeBottomSheetFragment.show(getSupportFragmentManager(), searchPostSortTypeBottomSheetFragment.getTag());
|
||||||
break;
|
break;
|
||||||
@ -251,7 +269,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
|
|||||||
case 1:
|
case 1:
|
||||||
case 2:
|
case 2:
|
||||||
Bundle bundle = new Bundle();
|
Bundle bundle = new Bundle();
|
||||||
bundle.putInt(SearchUserAndSubredditSortTypeBottomSheetFragment.EXTRA_FRAGMENT_POSITION, viewPager.getCurrentItem());
|
bundle.putInt(SearchUserAndSubredditSortTypeBottomSheetFragment.EXTRA_FRAGMENT_POSITION, viewPager2.getCurrentItem());
|
||||||
searchUserAndSubredditSortTypeBottomSheetFragment.setArguments(bundle);
|
searchUserAndSubredditSortTypeBottomSheetFragment.setArguments(bundle);
|
||||||
searchUserAndSubredditSortTypeBottomSheetFragment.show(getSupportFragmentManager(), searchUserAndSubredditSortTypeBottomSheetFragment.getTag());
|
searchUserAndSubredditSortTypeBottomSheetFragment.show(getSupportFragmentManager(), searchUserAndSubredditSortTypeBottomSheetFragment.getTag());
|
||||||
break;
|
break;
|
||||||
@ -337,19 +355,15 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SectionsPagerAdapter extends FragmentPagerAdapter {
|
private class SectionsPagerAdapter extends FragmentStateAdapter {
|
||||||
|
|
||||||
private PostFragment postFragment;
|
public SectionsPagerAdapter(FragmentManager fm, Lifecycle lifecycle) {
|
||||||
private SubredditListingFragment subredditListingFragment;
|
super(fm, lifecycle);
|
||||||
private UserListingFragment userListingFragment;
|
|
||||||
|
|
||||||
public SectionsPagerAdapter(FragmentManager fm) {
|
|
||||||
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public Fragment getItem(int position) {
|
public Fragment createFragment(int position) {
|
||||||
switch (position) {
|
switch (position) {
|
||||||
case 0: {
|
case 0: {
|
||||||
PostFragment mFragment = new PostFragment();
|
PostFragment mFragment = new PostFragment();
|
||||||
@ -384,124 +398,93 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Nullable
|
||||||
public int getCount() {
|
private Fragment getCurrentFragment() {
|
||||||
return 3;
|
if (viewPager2 == null || fragmentManager == null) {
|
||||||
}
|
return null;
|
||||||
|
|
||||||
@Override
|
|
||||||
public CharSequence getPageTitle(int position) {
|
|
||||||
switch (position) {
|
|
||||||
case 0:
|
|
||||||
return "Posts";
|
|
||||||
case 1:
|
|
||||||
return "Subreddits";
|
|
||||||
case 2:
|
|
||||||
return "Users";
|
|
||||||
}
|
}
|
||||||
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:
|
|
||||||
subredditListingFragment = (SubredditListingFragment) fragment;
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
userListingFragment = (UserListingFragment) fragment;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
displaySortTypeInToolbar();
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
void changeSortType(SortType sortType) {
|
void changeSortType(SortType sortType) {
|
||||||
postFragment.changeSortType(sortType);
|
Fragment fragment = getCurrentFragment();
|
||||||
displaySortTypeInToolbar();
|
if (fragment instanceof PostFragment) {
|
||||||
|
((PostFragment) fragment).changeSortType(sortType);
|
||||||
|
displaySortTypeInToolbar();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void changeSortType(SortType sortType, int fragmentPosition) {
|
void changeSortType(SortType sortType, int fragmentPosition) {
|
||||||
switch (fragmentPosition) {
|
Fragment fragment = fragmentManager.findFragmentByTag("f" + fragmentPosition);
|
||||||
case 1:
|
if (fragment instanceof SubredditListingFragment) {
|
||||||
mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_SEARCH_SUBREDDIT, sortType.getType().name()).apply();
|
((SubredditListingFragment) fragment).changeSortType(sortType);
|
||||||
subredditListingFragment.changeSortType(sortType);
|
} else if (fragment instanceof UserListingFragment) {
|
||||||
break;
|
((UserListingFragment) fragment).changeSortType(sortType);
|
||||||
case 2:
|
|
||||||
mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_SEARCH_USER, sortType.getType().name()).apply();
|
|
||||||
userListingFragment.changeSortType(sortType);
|
|
||||||
}
|
}
|
||||||
displaySortTypeInToolbar();
|
displaySortTypeInToolbar();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void refresh() {
|
public void refresh() {
|
||||||
switch (viewPager.getCurrentItem()) {
|
Fragment fragment = getCurrentFragment();
|
||||||
case 0:
|
if (fragment instanceof FragmentCommunicator) {
|
||||||
((FragmentCommunicator) postFragment).refresh();
|
((FragmentCommunicator) fragment).refresh();
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
((FragmentCommunicator) subredditListingFragment).refresh();
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
((FragmentCommunicator) userListingFragment).refresh();
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void changeNSFW(boolean nsfw) {
|
void changeNSFW(boolean nsfw) {
|
||||||
if (postFragment != null) {
|
Fragment fragment = getCurrentFragment();
|
||||||
postFragment.changeNSFW(nsfw);
|
if (fragment instanceof PostFragment) {
|
||||||
|
((PostFragment) fragment).changeNSFW(nsfw);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void changePostLayout(int postLayout) {
|
void changePostLayout(int postLayout) {
|
||||||
if (postFragment != null) {
|
Fragment fragment = getCurrentFragment();
|
||||||
mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_SEARCH_POST, postLayout).apply();
|
if (fragment instanceof PostFragment) {
|
||||||
((FragmentCommunicator) postFragment).changePostLayout(postLayout);
|
((PostFragment) fragment).changePostLayout(postLayout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void goBackToTop() {
|
void goBackToTop() {
|
||||||
if (viewPager.getCurrentItem() == 0) {
|
Fragment fragment = getCurrentFragment();
|
||||||
postFragment.goBackToTop();
|
if (fragment instanceof PostFragment) {
|
||||||
} else if (viewPager.getCurrentItem() == 1) {
|
((PostFragment) fragment).goBackToTop();
|
||||||
subredditListingFragment.goBackToTop();
|
} else if (fragment instanceof SubredditListingFragment) {
|
||||||
} else {
|
((SubredditListingFragment) fragment).goBackToTop();
|
||||||
userListingFragment.goBackToTop();
|
} else if (fragment instanceof UserListingFragment) {
|
||||||
|
((UserListingFragment) fragment).goBackToTop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void displaySortTypeInToolbar() {
|
void displaySortTypeInToolbar() {
|
||||||
switch (viewPager.getCurrentItem()) {
|
Fragment fragment = getCurrentFragment();
|
||||||
case 0:
|
if (fragment instanceof PostFragment) {
|
||||||
if (postFragment != null) {
|
SortType sortType = ((PostFragment) fragment).getSortType();
|
||||||
SortType sortType = postFragment.getSortType();
|
Utils.displaySortTypeInToolbar(sortType, toolbar);
|
||||||
Utils.displaySortTypeInToolbar(sortType, toolbar);
|
} else if (fragment instanceof SubredditListingFragment) {
|
||||||
}
|
SortType sortType = ((SubredditListingFragment) fragment).getSortType();
|
||||||
break;
|
Utils.displaySortTypeInToolbar(sortType, toolbar);
|
||||||
case 1:
|
} else if (fragment instanceof UserListingFragment) {
|
||||||
if (subredditListingFragment != null) {
|
SortType sortType = ((UserListingFragment) fragment).getSortType();
|
||||||
SortType sortType = subredditListingFragment.getSortType();
|
Utils.displaySortTypeInToolbar(sortType, toolbar);
|
||||||
Utils.displaySortTypeInToolbar(sortType, toolbar);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
if (userListingFragment != null) {
|
|
||||||
SortType sortType = userListingFragment.getSortType();
|
|
||||||
Utils.displaySortTypeInToolbar(sortType, toolbar);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getItemCount() {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void lockSwipeRightToGoBack() {
|
private void lockSwipeRightToGoBack() {
|
||||||
|
@ -205,6 +205,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void changeSortType(SortType sortType) {
|
public void changeSortType(SortType sortType) {
|
||||||
|
mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_SEARCH_SUBREDDIT, sortType.getType().name()).apply();
|
||||||
mSubredditListingViewModel.changeSortType(sortType);
|
mSubredditListingViewModel.changeSortType(sortType);
|
||||||
this.sortType = sortType;
|
this.sortType = sortType;
|
||||||
}
|
}
|
||||||
|
@ -185,6 +185,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void changeSortType(SortType sortType) {
|
public void changeSortType(SortType sortType) {
|
||||||
|
mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_SEARCH_USER, sortType.getType().name()).apply();
|
||||||
mUserListingViewModel.changeSortType(sortType);
|
mUserListingViewModel.changeSortType(sortType);
|
||||||
this.sortType = sortType;
|
this.sortType = sortType;
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:id="@+id/coordinator_layout_search_result_activity">
|
android:id="@+id/coordinator_layout_search_result_activity">
|
||||||
@ -42,11 +41,10 @@
|
|||||||
|
|
||||||
</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_search_result_activity"
|
android:id="@+id/view_pager_search_result_activity"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
app:layout_behavior="@string/appbar_scrolling_view_behavior"
|
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
|
||||||
tools:application=".SearchResultActivity" />
|
|
||||||
|
|
||||||
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
Loading…
Reference in New Issue
Block a user