Skip to content

Fix: View not maintaining expanded state between recreations. #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
Expand Down Expand Up @@ -401,4 +403,74 @@ public void onCollapse(@NonNull final ExpandableTextView view)
}

//endregion

@Override
public Parcelable onSaveInstanceState() {
// Boilerplate code that allows parent classes to save state
Parcelable superState = super.onSaveInstanceState();

SavedState ss = new SavedState(superState);
//end

ss.isExpanded = this.isExpanded();

return ss;
}

@Override
public void onRestoreInstanceState(Parcelable state) {
//begin boilerplate code so parent classes can restore state
if(!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}

SavedState ss = (SavedState)state;
super.onRestoreInstanceState(ss.getSuperState());
//end

if (ss.isExpanded) {
// Temporarily setting animation duration to 0 to make allow for a smoother UI experience.
setAnimationDuration(0);
expand();
setAnimationDuration(animationDuration);
}
}

/**
* User interface state stored by {@link ExpandableTextView} for saving View state on
* {@code configChanges}.
*
* @see #onSaveInstanceState()
* @see #onRestoreInstanceState(Parcelable)
* */
public static class SavedState extends BaseSavedState {
boolean isExpanded;

SavedState(Parcelable superState) {
super(superState);
}

private SavedState(Parcel in) {
super(in);
this.isExpanded = in.readInt() == 1;
}

@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(this.isExpanded ? 1 : 0);
}

// required field that makes Parcelables from a Parcel
public static final Parcelable.Creator<SavedState> CREATOR =
new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
}