From e9cec91a5b53995ca6134d8e8bf9cc2c30e16735 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 23 Jun 2019 16:28:08 +0800 Subject: Calculate the comment placeholder position if the position was changed. --- .../infinityforreddit/CommentData.java | 3 +- .../CommentRecyclerViewAdapter.java | 117 +++++++++++++++------ .../infinityforreddit/ParseComment.java | 11 +- 3 files changed, 92 insertions(+), 39 deletions(-) (limited to 'app/src/main/java/ml/docilealligator') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java index 7812a6c8..516253a1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java @@ -50,7 +50,8 @@ class CommentData implements Parcelable { isPlaceHolder = false; } - CommentData(int depth) { + CommentData(String parentFullName, int depth) { + this.fullName = parentFullName; this.depth = depth; isPlaceHolder = true; isLoadingMoreChildren = false; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java index 5b704cdb..8d4752d7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java @@ -9,7 +9,6 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; -import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; @@ -309,27 +308,54 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter childrenStartingIndex) { - mVisibleComments.get(position).setLoadingMoreChildren(false); - mVisibleComments.get(position).setLoadMoreChildrenFailed(false); - notifyItemChanged(position); - mVisibleComments.get(parentPosition).setMoreChildrenStartingIndex(childrenStartingIndex); mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1) .setLoadingMoreChildren(false); mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1) .setLoadMoreChildrenFailed(false); - mVisibleComments.addAll(position, expandedComments); - notifyItemRangeInserted(position, expandedComments.size()); + int placeholderPosition = position; + if(mVisibleComments.get(position).getFullName().equals(parentComment.getFullName())) { + for(int i = parentPosition + 1; i < mVisibleComments.size(); i++) { + if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) { + placeholderPosition = i; + break; + } + } + } + + mVisibleComments.get(placeholderPosition).setLoadingMoreChildren(false); + mVisibleComments.get(placeholderPosition).setLoadMoreChildrenFailed(false); + notifyItemChanged(placeholderPosition); + + mVisibleComments.addAll(placeholderPosition, expandedComments); + notifyItemRangeInserted(placeholderPosition, expandedComments.size()); } else { mVisibleComments.get(parentPosition).getChildren() .remove(mVisibleComments.get(parentPosition).getChildren().size() - 1); - mVisibleComments.remove(position); - notifyItemRemoved(position); mVisibleComments.get(parentPosition).removeMoreChildrenFullnames(); - mVisibleComments.addAll(position, expandedComments); - notifyItemRangeInserted(position, expandedComments.size()); + int placeholderPosition = position; + if(mVisibleComments.get(position).getFullName().equals(parentComment.getFullName())) { + for(int i = parentPosition + 1; i < mVisibleComments.size(); i++) { + if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) { + placeholderPosition = i; + break; + } + } + } + + mVisibleComments.remove(placeholderPosition); + notifyItemRemoved(placeholderPosition); + + mVisibleComments.addAll(placeholderPosition, expandedComments); + notifyItemRangeInserted(placeholderPosition, expandedComments.size()); + } + } else { + if(mVisibleComments.get(parentPosition).hasReply() && mVisibleComments.get(parentPosition).getChildren().size() <= childrenStartingIndex) { + mVisibleComments.get(parentPosition).getChildren() + .remove(mVisibleComments.get(parentPosition).getChildren().size() - 1); + mVisibleComments.get(parentPosition).removeMoreChildrenFullnames(); } } @@ -337,17 +363,24 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter comments, ArrayList visibleComments) { + private static void expandChildren(ArrayList comments, ArrayList visibleComments) { for(CommentData c : comments) { visibleComments.add(c); if(c.hasReply()) { c.setExpanded(true); - makeChildrenVisible(c.getChildren(), visibleComments); + expandChildren(c.getChildren(), visibleComments); } if(c.hasMoreChildrenFullnames() && c.getMoreChildrenFullnames().size() > c.getMoreChildrenStartingIndex()) { //Add a load more placeholder - visibleComments.add(new CommentData(c.getDepth() + 1)); - c.addChild(new CommentData(c.getDepth() + 1), c.getChildren().size()); + CommentData placeholder = new CommentData(c.getFullName(), c.getDepth() + 1); + visibleComments.add(placeholder); + c.addChild(placeholder, c.getChildren().size()); } } } -- cgit v1.2.3