Fix RecyclerView go to top after changing post type.

This commit is contained in:
Alex Ning 2021-07-21 20:05:10 +08:00
parent f2df7e94b1
commit eaa24b4858
2 changed files with 12 additions and 1 deletions

View File

@ -1460,6 +1460,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
break; break;
} }
int previousPosition = -1;
if (mLinearLayoutManager != null) {
previousPosition = mLinearLayoutManager.findFirstVisibleItemPosition();
} else if (mStaggeredGridLayoutManager != null) {
int[] into = new int[mStaggeredGridLayoutManager.getSpanCount()];
previousPosition = mStaggeredGridLayoutManager.findFirstVisibleItemPositions(into)[0];
}
int nColumns = getNColumns(getResources()); int nColumns = getNColumns(getResources());
if (nColumns == 1) { if (nColumns == 1) {
mLinearLayoutManager = new LinearLayoutManager(activity); mLinearLayoutManager = new LinearLayoutManager(activity);
@ -1480,6 +1487,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mLinearLayoutManager = null; mLinearLayoutManager = null;
} }
if (previousPosition > 0) {
mPostRecyclerView.scrollToPosition(previousPosition);
}
if (mAdapter != null) { if (mAdapter != null) {
mAdapter.setPostLayout(postLayout); mAdapter.setPostLayout(postLayout);
refreshAdapter(); refreshAdapter();
@ -1925,7 +1936,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mPostRecyclerView.setLayoutManager(null); mPostRecyclerView.setLayoutManager(null);
mPostRecyclerView.setAdapter(mAdapter); mPostRecyclerView.setAdapter(mAdapter);
mPostRecyclerView.setLayoutManager(layoutManager); mPostRecyclerView.setLayoutManager(layoutManager);
if (previousPosition > 0) { if (previousPosition > 0) {
mPostRecyclerView.scrollToPosition(previousPosition); mPostRecyclerView.scrollToPosition(previousPosition);
} }

View File

@ -224,6 +224,7 @@
android:layout_height="36dp" android:layout_height="36dp"
android:layout_margin="16dp" android:layout_margin="16dp"
android:layout_gravity="start" android:layout_gravity="start"
android:scaleType="center"
android:background="@drawable/play_button_round_background" android:background="@drawable/play_button_round_background"
android:src="@drawable/ic_gallery_reverse_color_24dp" /> android:src="@drawable/ic_gallery_reverse_color_24dp" />