From 16014a8807dd22bb64d583d97ef6e315c9e56d5b Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 10 Apr 2021 22:13:29 +0800 Subject: Fix CommentIndentationView losing state. --- .../CommentAndPostRecyclerViewAdapter.java | 8 +-- .../customtheme/CustomThemeWrapper.java | 14 ++--- .../customviews/CommentIndentationView.java | 64 +++++++++++++++++++++- 3 files changed, 71 insertions(+), 15 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java index 306b607a..f463ed31 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java @@ -238,7 +238,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter startXs; private final int spacing; private final int pathWidth; public CommentIndentationView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); + setSaveEnabled(true); setWillNotDraw(false); paint = new Paint(Paint.ANTI_ALIAS_FLAG); pathWidth = (int) Utils.convertDpToPixel(2, context); @@ -36,6 +39,7 @@ public class CommentIndentationView extends LinearLayout { protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); + startXs.clear(); for (int i = 0; i < level; i++) { startXs.add(spacing * (i + 1) + pathWidth); } @@ -49,13 +53,67 @@ public class CommentIndentationView extends LinearLayout { paint.setColor(colors[i % 7]); canvas.drawLine(startXs.get(i), 0, startXs.get(i), getHeight(), paint); } - startXs.clear(); } - public void setLevelAndColors(int level, int[] colors) { + @Nullable + @Override + protected Parcelable onSaveInstanceState() { + Parcelable parcelable = super.onSaveInstanceState(); + SavedState myState = new SavedState(parcelable); + myState.startXs = this.startXs; + myState.colors = this.colors; + return parcelable; + } + + @Override + protected void onRestoreInstanceState(Parcelable state) { + SavedState savedState = (SavedState) state; + + super.onRestoreInstanceState(savedState.getSuperState()); + + this.startXs = savedState.startXs; + this.colors = savedState.colors; + + invalidate(); + } + + public void setLevelAndColors(int level, Integer[] colors) { this.colors = colors; this.level = level; int indentationSpacing = (level * spacing + pathWidth); setPaddingRelative(indentationSpacing, 0, pathWidth, 0); + invalidate(); + } + + private static class SavedState extends BaseSavedState { + ArrayList startXs; + Integer[] colors; + + SavedState(Parcelable superState) { + super(superState); + } + + private SavedState(Parcel in) { + super(in); + startXs = in.readArrayList(SavedState.class.getClassLoader()); + colors = (Integer[]) in.readArray(SavedState.class.getClassLoader()); + } + + @Override + public void writeToParcel(Parcel out, int flags) { + super.writeToParcel(out, flags); + out.writeList(startXs); + out.writeArray(colors); + } + + public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { + public SavedState createFromParcel(Parcel in) { + return new SavedState(in); + } + + public SavedState[] newArray(int size) { + return new SavedState[size]; + } + }; } } -- cgit v1.2.3