Add a ViewPager in MainActivity to display popular posts.

This commit is contained in:
Alex Ning 2019-07-29 22:02:37 +08:00
parent 111ba8e5fc
commit 453501b1ba
5 changed files with 181 additions and 57 deletions

View File

@ -8,6 +8,7 @@ import android.os.Bundle;
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.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
@ -19,6 +20,9 @@ import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat; import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout; import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager; import com.bumptech.glide.RequestManager;
@ -26,6 +30,7 @@ import com.bumptech.glide.request.RequestOptions;
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.floatingactionbutton.FloatingActionButton; import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -39,29 +44,32 @@ import retrofit2.Retrofit;
public class MainActivity extends AppCompatActivity implements SortTypeBottomSheetFragment.SortTypeSelectionCallback, public class MainActivity extends AppCompatActivity implements SortTypeBottomSheetFragment.SortTypeSelectionCallback,
PostTypeBottomSheetFragment.PostTypeSelectionCallback { PostTypeBottomSheetFragment.PostTypeSelectionCallback {
private static final String FRAGMENT_OUT_STATE = "FOS";
private static final String FETCH_USER_INFO_STATE = "FUIS"; private static final String FETCH_USER_INFO_STATE = "FUIS";
private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS";
private static final int LOGIN_ACTIVITY_REQUEST_CODE = 0; private static final int LOGIN_ACTIVITY_REQUEST_CODE = 0;
@BindView(R.id.drawer_layout) DrawerLayout drawer; @BindView(R.id.drawer_layout) DrawerLayout drawer;
@BindView(R.id.view_pager_main_activity) ViewPager viewPager;
@BindView(R.id.collapsing_toolbar_layout_main_activity) CollapsingToolbarLayout collapsingToolbarLayout; @BindView(R.id.collapsing_toolbar_layout_main_activity) CollapsingToolbarLayout collapsingToolbarLayout;
@BindView(R.id.profile_linear_layout_main_activity) LinearLayout profileLinearLayout; @BindView(R.id.profile_linear_layout_main_activity) LinearLayout profileLinearLayout;
@BindView(R.id.subscriptions_linear_layout_main_activity) LinearLayout subscriptionLinearLayout; @BindView(R.id.subscriptions_linear_layout_main_activity) LinearLayout subscriptionLinearLayout;
@BindView(R.id.settings_linear_layout_main_activity) LinearLayout settingsLinearLayout; @BindView(R.id.settings_linear_layout_main_activity) LinearLayout settingsLinearLayout;
@BindView(R.id.tab_layout_main_activity) TabLayout tabLayout;
@BindView(R.id.fab_main_activity) FloatingActionButton fab; @BindView(R.id.fab_main_activity) FloatingActionButton fab;
private SectionsPagerAdapter sectionsPagerAdapter;
private TextView mNameTextView; private TextView mNameTextView;
private TextView mKarmaTextView; private TextView mKarmaTextView;
private GifImageView mProfileImageView; private GifImageView mProfileImageView;
private ImageView mBannerImageView; private ImageView mBannerImageView;
private Fragment mFragment;
private RequestManager glide; private RequestManager glide;
private AppBarLayout.LayoutParams params; private AppBarLayout.LayoutParams params;
private PostTypeBottomSheetFragment postTypeBottomSheetFragment; private PostTypeBottomSheetFragment postTypeBottomSheetFragment;
private SortTypeBottomSheetFragment sortTypeBottomSheetFragment; private SortTypeBottomSheetFragment bestSortTypeBottomSheetFragment;
private SortTypeBottomSheetFragment popularSortTypeBottomSheetFragment;
private String mName; private String mName;
private String mProfileImageUrl; private String mProfileImageUrl;
@ -95,7 +103,16 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
((Infinity) getApplication()).getmAppComponent().inject(this); ((Infinity) getApplication()).getmAppComponent().inject(this);
postTypeBottomSheetFragment = new PostTypeBottomSheetFragment(); postTypeBottomSheetFragment = new PostTypeBottomSheetFragment();
sortTypeBottomSheetFragment = new SortTypeBottomSheetFragment();
bestSortTypeBottomSheetFragment = new SortTypeBottomSheetFragment();
Bundle bestBundle = new Bundle();
bestBundle.putBoolean(SortTypeBottomSheetFragment.EXTRA_NO_BEST_TYPE, false);
bestSortTypeBottomSheetFragment.setArguments(bestBundle);
popularSortTypeBottomSheetFragment = new SortTypeBottomSheetFragment();
Bundle popularBundle = new Bundle();
popularBundle.putBoolean(SortTypeBottomSheetFragment.EXTRA_NO_BEST_TYPE, true);
popularSortTypeBottomSheetFragment.setArguments(popularBundle);
Toolbar toolbar = findViewById(R.id.toolbar); Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
@ -108,22 +125,17 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams(); params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams();
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(sectionsPagerAdapter);
viewPager.setOffscreenPageLimit(2);
tabLayout.setupWithViewPager(viewPager);
String accessToken = getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); String accessToken = getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
if (accessToken.equals("")) { if (accessToken.equals("")) {
Intent loginIntent = new Intent(this, LoginActivity.class); Intent loginIntent = new Intent(this, LoginActivity.class);
startActivityForResult(loginIntent, LOGIN_ACTIVITY_REQUEST_CODE); startActivityForResult(loginIntent, LOGIN_ACTIVITY_REQUEST_CODE);
} else { } else {
if (savedInstanceState == null) { if (savedInstanceState != null) {
mFragment = new PostFragment();
Bundle bundle = new Bundle();
bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_FRONT_PAGE);
bundle.putString(PostFragment.EXTRA_SORT_TYPE, PostDataSource.SORT_TYPE_BEST);
mFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit();
} else {
mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit();
mFetchUserInfoSuccess = savedInstanceState.getBoolean(FETCH_USER_INFO_STATE); mFetchUserInfoSuccess = savedInstanceState.getBoolean(FETCH_USER_INFO_STATE);
isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE);
} }
@ -268,37 +280,39 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
@Override @Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) { public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (mFragment instanceof FragmentCommunicator) { switch (item.getItemId()) {
switch (item.getItemId()) { case R.id.action_sort_main_activity:
case R.id.action_sort_main_activity: if(viewPager.getCurrentItem() == 1) {
sortTypeBottomSheetFragment.show(getSupportFragmentManager(), sortTypeBottomSheetFragment.getTag()); popularSortTypeBottomSheetFragment.show(getSupportFragmentManager(), popularSortTypeBottomSheetFragment.getTag());
return true; } else {
case R.id.action_search_main_activity: bestSortTypeBottomSheetFragment.show(getSupportFragmentManager(), bestSortTypeBottomSheetFragment.getTag());
Intent intent = new Intent(this, SearchActivity.class); }
startActivity(intent); return true;
return true; case R.id.action_search_main_activity:
case R.id.action_refresh_main_activity: Intent intent = new Intent(this, SearchActivity.class);
((FragmentCommunicator) mFragment).refresh(); startActivity(intent);
mFetchUserInfoSuccess = false; return true;
loadUserData(); case R.id.action_refresh_main_activity:
return true; sectionsPagerAdapter.refresh(viewPager.getCurrentItem());
case R.id.action_lazy_mode_main_activity: mFetchUserInfoSuccess = false;
MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_main_activity); loadUserData();
if(isInLazyMode) { return true;
isInLazyMode = false; case R.id.action_lazy_mode_main_activity:
((FragmentCommunicator) mFragment).stopLazyMode(); MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_main_activity);
lazyModeItem.setTitle(R.string.action_start_lazy_mode); /*if(isInLazyMode) {
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS); isInLazyMode = false;
collapsingToolbarLayout.setLayoutParams(params); ((FragmentCommunicator) mFragment).stopLazyMode();
} else { lazyModeItem.setTitle(R.string.action_start_lazy_mode);
isInLazyMode = true; params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
((FragmentCommunicator) mFragment).startLazyMode(); collapsingToolbarLayout.setLayoutParams(params);
lazyModeItem.setTitle(R.string.action_stop_lazy_mode); } else {
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL); isInLazyMode = true;
collapsingToolbarLayout.setLayoutParams(params); ((FragmentCommunicator) mFragment).startLazyMode();
} lazyModeItem.setTitle(R.string.action_stop_lazy_mode);
return true; params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL);
} collapsingToolbarLayout.setLayoutParams(params);
}*/
return true;
} }
return false; return false;
} }
@ -316,17 +330,13 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
@Override @Override
protected void onSaveInstanceState(@NonNull Bundle outState) { protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
if (mFragment != null) {
getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment);
}
outState.putBoolean(FETCH_USER_INFO_STATE, mFetchUserInfoSuccess); outState.putBoolean(FETCH_USER_INFO_STATE, mFetchUserInfoSuccess);
outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode);
} }
@Override @Override
public void sortTypeSelected(String sortType) { public void sortTypeSelected(String sortType) {
((PostFragment) mFragment).changeSortType(sortType); sectionsPagerAdapter.changeSortType(sortType, viewPager.getCurrentItem());
} }
@Override @Override
@ -350,4 +360,87 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
startActivity(intent); startActivity(intent);
} }
} }
private class SectionsPagerAdapter extends FragmentPagerAdapter {
private PostFragment frontPagePostFragment;
private PostFragment popularPostFragment;
SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@NonNull
@Override
public Fragment getItem(int position) {
if (position == 0) {
PostFragment fragment = new PostFragment();
Bundle bundle = new Bundle();
bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_FRONT_PAGE);
bundle.putString(PostFragment.EXTRA_SORT_TYPE, PostDataSource.SORT_TYPE_BEST);
fragment.setArguments(bundle);
return fragment;
}
PostFragment fragment = new PostFragment();
Bundle bundle = new Bundle();
bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT);
bundle.putString(PostFragment.EXTRA_SUBREDDIT_NAME, "popular");
bundle.putString(PostFragment.EXTRA_SORT_TYPE, PostDataSource.SORT_TYPE_HOT);
fragment.setArguments(bundle);
return fragment;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Best";
case 1:
return "Popular";
}
return null;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
switch (position) {
case 0:
frontPagePostFragment = (PostFragment) fragment;
break;
case 1:
popularPostFragment = (PostFragment) fragment;
break;
}
return fragment;
}
void changeSortType(String sortType, int fragmentPosition) {
switch (fragmentPosition) {
case 0:
frontPagePostFragment.changeSortType(sortType);
break;
case 1:
popularPostFragment.changeSortType(sortType);
break;
}
}
public void refresh(int fragmentPosition) {
if(fragmentPosition == 0) {
if(frontPagePostFragment != null) {
((FragmentCommunicator) frontPagePostFragment).refresh();
}
} else {
if(popularPostFragment != null) {
((FragmentCommunicator) popularPostFragment).refresh();
}
}
}
}
} }

View File

@ -26,6 +26,8 @@ public class SortTypeBottomSheetFragment extends RoundedBottomSheetDialogFragmen
void sortTypeSelected(String sortType); void sortTypeSelected(String sortType);
} }
static final String EXTRA_NO_BEST_TYPE = "ENBT";
@BindView(R.id.best_type_text_view_sort_type_bottom_sheet_fragment) TextView bestTypeTextView; @BindView(R.id.best_type_text_view_sort_type_bottom_sheet_fragment) TextView bestTypeTextView;
@BindView(R.id.hot_type_text_view_sort_type_bottom_sheet_fragment) TextView hotTypeTextView; @BindView(R.id.hot_type_text_view_sort_type_bottom_sheet_fragment) TextView hotTypeTextView;
@BindView(R.id.new_type_text_view_sort_type_bottom_sheet_fragment) TextView newTypeTextView; @BindView(R.id.new_type_text_view_sort_type_bottom_sheet_fragment) TextView newTypeTextView;
@ -47,10 +49,14 @@ public class SortTypeBottomSheetFragment extends RoundedBottomSheetDialogFragmen
Activity activity = getActivity(); Activity activity = getActivity();
bestTypeTextView.setOnClickListener(view -> { if(getArguments().getBoolean(EXTRA_NO_BEST_TYPE)) {
((SortTypeSelectionCallback) activity).sortTypeSelected(PostDataSource.SORT_TYPE_BEST); bestTypeTextView.setVisibility(View.GONE);
dismiss(); } else {
}); bestTypeTextView.setOnClickListener(view -> {
((SortTypeSelectionCallback) activity).sortTypeSelected(PostDataSource.SORT_TYPE_BEST);
dismiss();
});
}
hotTypeTextView.setOnClickListener(view -> { hotTypeTextView.setOnClickListener(view -> {
((SortTypeSelectionCallback) activity).sortTypeSelected(PostDataSource.SORT_TYPE_HOT); ((SortTypeSelectionCallback) activity).sortTypeSelected(PostDataSource.SORT_TYPE_HOT);

View File

@ -98,7 +98,11 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
((Infinity) getApplication()).getmAppComponent().inject(this); ((Infinity) getApplication()).getmAppComponent().inject(this);
postTypeBottomSheetFragment = new PostTypeBottomSheetFragment(); postTypeBottomSheetFragment = new PostTypeBottomSheetFragment();
sortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); sortTypeBottomSheetFragment = new SortTypeBottomSheetFragment();
Bundle bottomSheetBundle = new Bundle();
bottomSheetBundle.putBoolean(SortTypeBottomSheetFragment.EXTRA_NO_BEST_TYPE, true);
sortTypeBottomSheetFragment.setArguments(bottomSheetBundle);
params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams(); params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams();

View File

@ -48,7 +48,6 @@ public class ViewUserDetailActivity extends AppCompatActivity {
public static final String EXTRA_USER_NAME_KEY = "EUNK"; public static final String EXTRA_USER_NAME_KEY = "EUNK";
private static final String FRAGMENT_OUT_STATE_KEY = "FOSK";
private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS";
@BindView(R.id.coordinator_layout_view_user_detail_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.coordinator_layout_view_user_detail_activity) CoordinatorLayout coordinatorLayout;

View File

@ -50,6 +50,22 @@
</com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.CollapsingToolbarLayout>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout_main_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabIndicatorColor="@android:color/white"
app:tabIndicatorHeight="3dp"
app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@android:color/white"
app:tabRippleColor="?attr/colorControlHighlight"
app:tabUnboundedRipple="false" />
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>
<View <View
@ -60,7 +76,13 @@
android:elevation="1dp" android:elevation="1dp"
android:visibility="gone"/> android:visibility="gone"/>
<include layout="@layout/content_main" /> <!--<include layout="@layout/content_main" />-->
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager_main_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.floatingactionbutton.FloatingActionButton <com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_main_activity" android:id="@+id/fab_main_activity"