Android 自定义组件不保留状态权

发布于 2024-12-01 17:51:23 字数 1753 浏览 1 评论 0原文

我的布局中有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文