Add Gallery Layout to default post layout option. 3 columns is available in PostFragment.

This commit is contained in:
Alex Ning 2021-02-19 16:48:25 +08:00
parent 719a5a3a8e
commit 800b3c63a5
2 changed files with 32 additions and 16 deletions

View File

@ -733,10 +733,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mLinearLayoutManager = new LinearLayoutManager(activity); mLinearLayoutManager = new LinearLayoutManager(activity);
mPostRecyclerView.setLayoutManager(mLinearLayoutManager); mPostRecyclerView.setLayoutManager(mLinearLayoutManager);
} else { } else {
mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(nColumns, StaggeredGridLayoutManager.VERTICAL);
mPostRecyclerView.setLayoutManager(mStaggeredGridLayoutManager); mPostRecyclerView.setLayoutManager(mStaggeredGridLayoutManager);
StaggeredGridLayoutManagerItemOffsetDecoration itemDecoration = StaggeredGridLayoutManagerItemOffsetDecoration itemDecoration =
new StaggeredGridLayoutManagerItemOffsetDecoration(activity, R.dimen.staggeredLayoutManagerItemOffset); new StaggeredGridLayoutManagerItemOffsetDecoration(activity, R.dimen.staggeredLayoutManagerItemOffset, nColumns);
mPostRecyclerView.addItemDecoration(itemDecoration); mPostRecyclerView.addItemDecoration(itemDecoration);
windowWidth /= 2; windowWidth /= 2;
} }
@ -1117,7 +1117,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
if (mLinearLayoutManager != null) { if (mLinearLayoutManager != null) {
outState.putInt(RECYCLER_VIEW_POSITION_STATE, mLinearLayoutManager.findFirstVisibleItemPosition()); outState.putInt(RECYCLER_VIEW_POSITION_STATE, mLinearLayoutManager.findFirstVisibleItemPosition());
} else if (mStaggeredGridLayoutManager != null) { } else if (mStaggeredGridLayoutManager != null) {
int[] into = new int[2]; int[] into = new int[mStaggeredGridLayoutManager.getSpanCount()];
outState.putInt(RECYCLER_VIEW_POSITION_STATE, outState.putInt(RECYCLER_VIEW_POSITION_STATE,
mStaggeredGridLayoutManager.findFirstVisibleItemPositions(into)[0]); mStaggeredGridLayoutManager.findFirstVisibleItemPositions(into)[0]);
} }
@ -1275,16 +1275,19 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
int nColumns = getNColumns(getResources()); int nColumns = getNColumns(getResources());
if (nColumns == 1) { if (nColumns == 1) {
mLinearLayoutManager = new LinearLayoutManager(activity); mLinearLayoutManager = new LinearLayoutManager(activity);
mPostRecyclerView.setLayoutManager(mLinearLayoutManager);
mStaggeredGridLayoutManager = null;
if (mPostRecyclerView.getItemDecorationCount() > 0) { if (mPostRecyclerView.getItemDecorationCount() > 0) {
mPostRecyclerView.removeItemDecorationAt(0); mPostRecyclerView.removeItemDecorationAt(0);
} }
} else if (nColumns == 2) { mPostRecyclerView.setLayoutManager(mLinearLayoutManager);
mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); mStaggeredGridLayoutManager = null;
} else {
mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(nColumns, StaggeredGridLayoutManager.VERTICAL);
if (mPostRecyclerView.getItemDecorationCount() > 0) {
mPostRecyclerView.removeItemDecorationAt(0);
}
mPostRecyclerView.setLayoutManager(mStaggeredGridLayoutManager); mPostRecyclerView.setLayoutManager(mStaggeredGridLayoutManager);
StaggeredGridLayoutManagerItemOffsetDecoration itemDecoration = StaggeredGridLayoutManagerItemOffsetDecoration itemDecoration =
new StaggeredGridLayoutManagerItemOffsetDecoration(activity, R.dimen.staggeredLayoutManagerItemOffset); new StaggeredGridLayoutManagerItemOffsetDecoration(activity, R.dimen.staggeredLayoutManagerItemOffset, nColumns);
mPostRecyclerView.addItemDecoration(itemDecoration); mPostRecyclerView.addItemDecoration(itemDecoration);
mLinearLayoutManager = null; mLinearLayoutManager = null;
} }
@ -1641,7 +1644,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
if (mLinearLayoutManager != null) { if (mLinearLayoutManager != null) {
previousPosition = mLinearLayoutManager.findFirstVisibleItemPosition(); previousPosition = mLinearLayoutManager.findFirstVisibleItemPosition();
} else if (mStaggeredGridLayoutManager != null) { } else if (mStaggeredGridLayoutManager != null) {
int[] into = new int[2]; int[] into = new int[mStaggeredGridLayoutManager.getSpanCount()];
previousPosition = mStaggeredGridLayoutManager.findFirstVisibleItemPositions(into)[0]; previousPosition = mStaggeredGridLayoutManager.findFirstVisibleItemPositions(into)[0];
} }
@ -1724,13 +1727,15 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
private static class StaggeredGridLayoutManagerItemOffsetDecoration extends RecyclerView.ItemDecoration { private static class StaggeredGridLayoutManagerItemOffsetDecoration extends RecyclerView.ItemDecoration {
private int mItemOffset; private int mItemOffset;
private int mNColumns;
StaggeredGridLayoutManagerItemOffsetDecoration(int itemOffset) { StaggeredGridLayoutManagerItemOffsetDecoration(int itemOffset, int nColumns) {
mItemOffset = itemOffset; mItemOffset = itemOffset;
mNColumns = nColumns;
} }
StaggeredGridLayoutManagerItemOffsetDecoration(@NonNull Context context, @DimenRes int itemOffsetId) { StaggeredGridLayoutManagerItemOffsetDecoration(@NonNull Context context, @DimenRes int itemOffsetId, int nColumns) {
this(context.getResources().getDimensionPixelSize(itemOffsetId)); this(context.getResources().getDimensionPixelSize(itemOffsetId), nColumns);
} }
@Override @Override
@ -1744,10 +1749,20 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
int halfOffset = mItemOffset / 2; int halfOffset = mItemOffset / 2;
if (spanIndex == 0) { if (mNColumns == 2) {
outRect.set(halfOffset, 0, halfOffset / 2, 0); if (spanIndex == 0) {
} else { outRect.set(halfOffset, 0, halfOffset / 2, 0);
outRect.set(halfOffset / 2, 0, halfOffset, 0); } else {
outRect.set(halfOffset / 2, 0, halfOffset, 0);
}
} else if (mNColumns == 3) {
if (spanIndex == 0) {
outRect.set(halfOffset, 0, halfOffset / 2, 0);
} else if (spanIndex == 1) {
outRect.set(halfOffset / 2, 0, halfOffset / 2, 0);
} else {
outRect.set(halfOffset / 2, 0, halfOffset, 0);
}
} }
} }
} }

View File

@ -225,6 +225,7 @@
<string-array name="settings_number_of_columns_in_post_feed"> <string-array name="settings_number_of_columns_in_post_feed">
<item>1</item> <item>1</item>
<item>2</item> <item>2</item>
<item>3</item>
</string-array> </string-array>
<string-array name="settings_main_page_tab_count"> <string-array name="settings_main_page_tab_count">