Fix lazy mode after hiding read posts.

This commit is contained in:
Alex Ning 2020-12-08 18:40:19 +08:00
parent 59baa9c51c
commit 13107b9193
3 changed files with 31 additions and 4 deletions

View File

@ -396,6 +396,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
if (post != null) { if (post != null) {
if (post.isRead()) { if (post.isRead()) {
if (position < mHideReadPostsIndex) { if (position < mHideReadPostsIndex) {
post.hidePostInRecyclerView();
holder.itemView.setVisibility(View.GONE); holder.itemView.setVisibility(View.GONE);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams(); RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
params.height = 0; params.height = 0;
@ -717,6 +718,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
if (post != null) { if (post != null) {
if (post.isRead()) { if (post.isRead()) {
if (position < mHideReadPostsIndex) { if (position < mHideReadPostsIndex) {
post.hidePostInRecyclerView();
holder.itemView.setVisibility(View.GONE); holder.itemView.setVisibility(View.GONE);
ViewGroup.LayoutParams params = holder.itemView.getLayoutParams(); ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
params.height = 0; params.height = 0;
@ -1218,6 +1220,20 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
mHideReadPostsIndex = getItemCount(); mHideReadPostsIndex = getItemCount();
} }
public int getNextItemPositionWithoutBeingHidden(int fromPosition) {
int temp = fromPosition;
while (temp >= 0 && temp < super.getItemCount()) {
Post post = getItem(temp);
if (post != null && post.isHiddenInRecyclerView()) {
temp++;
} else {
break;
}
}
return temp;
}
private boolean hasExtraRow() { private boolean hasExtraRow() {
return networkState != null && networkState.getStatus() != NetworkState.Status.SUCCESS; return networkState != null && networkState.getStatus() != NetworkState.Status.SUCCESS;
} }

View File

@ -331,20 +331,20 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
@Override @Override
public void run() { public void run() {
if (isInLazyMode && !isLazyModePaused) { if (isInLazyMode && !isLazyModePaused && mAdapter != null) {
int nPosts = mAdapter.getItemCount(); int nPosts = mAdapter.getItemCount();
if (getCurrentPosition() == -1) { if (getCurrentPosition() == -1) {
if (mLinearLayoutManager != null) { if (mLinearLayoutManager != null) {
setCurrentPosition(mLinearLayoutManager.findFirstVisibleItemPosition()); setCurrentPosition(mAdapter.getNextItemPositionWithoutBeingHidden(mLinearLayoutManager.findFirstVisibleItemPosition()));
} else { } else {
int[] into = new int[2]; int[] into = new int[2];
setCurrentPosition(mStaggeredGridLayoutManager.findFirstVisibleItemPositions(into)[1]); setCurrentPosition(mAdapter.getNextItemPositionWithoutBeingHidden(mStaggeredGridLayoutManager.findFirstVisibleItemPositions(into)[1]));
} }
} }
if (getCurrentPosition() != RecyclerView.NO_POSITION && nPosts > getCurrentPosition()) { if (getCurrentPosition() != RecyclerView.NO_POSITION && nPosts > getCurrentPosition()) {
incrementCurrentPosition(); incrementCurrentPosition();
smoothScroller.setTargetPosition(getCurrentPosition()); smoothScroller.setTargetPosition(mAdapter.getNextItemPositionWithoutBeingHidden(getCurrentPosition()));
if (mLinearLayoutManager != null) { if (mLinearLayoutManager != null) {
mLinearLayoutManager.startSmoothScroll(smoothScroller); mLinearLayoutManager.startSmoothScroll(smoothScroller);
} else { } else {

View File

@ -72,6 +72,7 @@ public class Post implements Parcelable {
private boolean saved; private boolean saved;
private boolean isCrosspost; private boolean isCrosspost;
private boolean isRead; private boolean isRead;
private boolean isHiddenInRecyclerView = false;
private String crosspostParentId; private String crosspostParentId;
private ArrayList<Preview> previews = new ArrayList<>(); private ArrayList<Preview> previews = new ArrayList<>();
private ArrayList<Gallery> gallery = new ArrayList<>(); private ArrayList<Gallery> gallery = new ArrayList<>();
@ -187,6 +188,7 @@ public class Post implements Parcelable {
saved = in.readByte() != 0; saved = in.readByte() != 0;
isCrosspost = in.readByte() != 0; isCrosspost = in.readByte() != 0;
isRead = in.readByte() != 0; isRead = in.readByte() != 0;
isHiddenInRecyclerView = in.readByte() != 0;
crosspostParentId = in.readString(); crosspostParentId = in.readString();
in.readTypedList(previews, Preview.CREATOR); in.readTypedList(previews, Preview.CREATOR);
in.readTypedList(gallery, Gallery.CREATOR); in.readTypedList(gallery, Gallery.CREATOR);
@ -458,6 +460,14 @@ public class Post implements Parcelable {
return isRead; return isRead;
} }
public boolean isHiddenInRecyclerView() {
return isHiddenInRecyclerView;
}
public void hidePostInRecyclerView() {
isHiddenInRecyclerView = true;
}
public String getCrosspostParentId() { public String getCrosspostParentId() {
return crosspostParentId; return crosspostParentId;
} }
@ -523,6 +533,7 @@ public class Post implements Parcelable {
parcel.writeByte((byte) (saved ? 1 : 0)); parcel.writeByte((byte) (saved ? 1 : 0));
parcel.writeByte((byte) (isCrosspost ? 1 : 0)); parcel.writeByte((byte) (isCrosspost ? 1 : 0));
parcel.writeByte((byte) (isRead ? 1 : 0)); parcel.writeByte((byte) (isRead ? 1 : 0));
parcel.writeByte((byte) (isHiddenInRecyclerView ? 1 : 0));
parcel.writeString(crosspostParentId); parcel.writeString(crosspostParentId);
parcel.writeTypedList(previews); parcel.writeTypedList(previews);
parcel.writeTypedList(gallery); parcel.writeTypedList(gallery);