Android 自定义组件不保留状态权
我的布局中有 4 个自定义复合组件,每个组件都从relativelayout 扩展。从概念上讲,它们只是列出带有标题和复选框的行。我希望每个组件都保留其自己的检查状态。不幸的是,他们似乎在某种程度上共享他们的状态,即在配置更改后,所有四个的检查状态取决于是否仅检查布局中的最后一个。我做了一些日志记录,发现 onSaveInstanceState 和 onRestoreInstanceState 每个唯一 id 行都被调用两次。
我将不胜感激任何帮助,这令人困惑
private int NOT_CHECKED = 0;
private int IS_CHECKED = 1;
@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.checked = mCheckBox.isChecked() ? IS_CHECKED : NOT_CHECKED;
Log.e("Active", "Out - " + this.getId() + " - " + mCheckBox.isChecked());
return ss;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if(!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
mCheckBox.setChecked(ss.checked == IS_CHECKED ? true : false);
Log.e("Active", "In - " + this.getId() + " - " + mCheckBox.isChecked());
}
private static class SavedState extends BaseSavedState {
private int checked;
SavedState(Parcelable superState) {
super(superState);
}
private SavedState(Parcel in) {
super(in);
this.checked = in.readInt();
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(this.checked);
}
public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<FilterCheckBoxRow.SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
I have 4 custom compound components in a layout, each extends from RelativeLayout. Conceptually they're simply list rows with a title and a CheckBox. I want each component to preserve it's own checked state. Unfortunately they somehow seem to be sharing their state, i.e. after a config change the checked state of all four is dictated by whether or not only the last one in the layout was checked. I did some logging and found that onSaveInstanceState and onRestoreInstanceState are both being called twice per each uniquely id'd row.
I would appreciate any help, this is baffling
private int NOT_CHECKED = 0;
private int IS_CHECKED = 1;
@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.checked = mCheckBox.isChecked() ? IS_CHECKED : NOT_CHECKED;
Log.e("Active", "Out - " + this.getId() + " - " + mCheckBox.isChecked());
return ss;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if(!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
mCheckBox.setChecked(ss.checked == IS_CHECKED ? true : false);
Log.e("Active", "In - " + this.getId() + " - " + mCheckBox.isChecked());
}
private static class SavedState extends BaseSavedState {
private int checked;
SavedState(Parcelable superState) {
super(superState);
}
private SavedState(Parcel in) {
super(in);
this.checked = in.readInt();
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(this.checked);
}
public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<FilterCheckBoxRow.SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论