From 13107b919333092c4e1b48c6c0da50be3d31acc2 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 8 Dec 2020 18:40:19 +0800 Subject: Fix lazy mode after hiding read posts. --- .../adapters/PostRecyclerViewAdapter.java | 16 ++++++++++++++++ .../infinityforreddit/fragments/PostFragment.java | 8 ++++---- .../ml/docilealligator/infinityforreddit/post/Post.java | 11 +++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java index 9d9e890d..8a8eef99 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -396,6 +396,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= 0 && temp < super.getItemCount()) { + Post post = getItem(temp); + if (post != null && post.isHiddenInRecyclerView()) { + temp++; + } else { + break; + } + } + + return temp; + } + private boolean hasExtraRow() { return networkState != null && networkState.getStatus() != NetworkState.Status.SUCCESS; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java index c682e181..948bd0d9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -331,20 +331,20 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public void run() { - if (isInLazyMode && !isLazyModePaused) { + if (isInLazyMode && !isLazyModePaused && mAdapter != null) { int nPosts = mAdapter.getItemCount(); if (getCurrentPosition() == -1) { if (mLinearLayoutManager != null) { - setCurrentPosition(mLinearLayoutManager.findFirstVisibleItemPosition()); + setCurrentPosition(mAdapter.getNextItemPositionWithoutBeingHidden(mLinearLayoutManager.findFirstVisibleItemPosition())); } else { int[] into = new int[2]; - setCurrentPosition(mStaggeredGridLayoutManager.findFirstVisibleItemPositions(into)[1]); + setCurrentPosition(mAdapter.getNextItemPositionWithoutBeingHidden(mStaggeredGridLayoutManager.findFirstVisibleItemPositions(into)[1])); } } if (getCurrentPosition() != RecyclerView.NO_POSITION && nPosts > getCurrentPosition()) { incrementCurrentPosition(); - smoothScroller.setTargetPosition(getCurrentPosition()); + smoothScroller.setTargetPosition(mAdapter.getNextItemPositionWithoutBeingHidden(getCurrentPosition())); if (mLinearLayoutManager != null) { mLinearLayoutManager.startSmoothScroll(smoothScroller); } else { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java index 582c398d..c96c325a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java @@ -72,6 +72,7 @@ public class Post implements Parcelable { private boolean saved; private boolean isCrosspost; private boolean isRead; + private boolean isHiddenInRecyclerView = false; private String crosspostParentId; private ArrayList previews = new ArrayList<>(); private ArrayList gallery = new ArrayList<>(); @@ -187,6 +188,7 @@ public class Post implements Parcelable { saved = in.readByte() != 0; isCrosspost = in.readByte() != 0; isRead = in.readByte() != 0; + isHiddenInRecyclerView = in.readByte() != 0; crosspostParentId = in.readString(); in.readTypedList(previews, Preview.CREATOR); in.readTypedList(gallery, Gallery.CREATOR); @@ -458,6 +460,14 @@ public class Post implements Parcelable { return isRead; } + public boolean isHiddenInRecyclerView() { + return isHiddenInRecyclerView; + } + + public void hidePostInRecyclerView() { + isHiddenInRecyclerView = true; + } + public String getCrosspostParentId() { return crosspostParentId; } @@ -523,6 +533,7 @@ public class Post implements Parcelable { parcel.writeByte((byte) (saved ? 1 : 0)); parcel.writeByte((byte) (isCrosspost ? 1 : 0)); parcel.writeByte((byte) (isRead ? 1 : 0)); + parcel.writeByte((byte) (isHiddenInRecyclerView ? 1 : 0)); parcel.writeString(crosspostParentId); parcel.writeTypedList(previews); parcel.writeTypedList(gallery); -- cgit v1.2.3