我使用恢复/保存方法创建了一个简单的视图,其中包含一些字段:
public class FoodMainStepView extends WizardBaseStepView {
private EditText mNameEditText;
private EditText mCategoryEditText;
...
public FoodMainStepView(Context context) {
super(context);
}
public FoodMainStepView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FoodMainStepView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void init() {
...
mNameEditText = (EditText) findViewById(R.id.name);
...
}
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.mNameEditTextSaved = mNameEditText.getValue();
return ss;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
if(!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState ss = (SavedState)state;
super.onRestoreInstanceState(ss.getSuperState());
mNameEditText.setValue(ss.mNameEditTextSaved);
}
private static class SavedState extends BaseSavedState {
public String mNameEditTextSaved;
SavedState(Parcelable superState) {
super(superState);
}
private SavedState(Parcel in) {
super(in);
mNameEditTextSaved = in.readString();
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeString(mNameEditTextSaved);
}
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];
}
};
}
}
基类是:
public abstract class WizardBaseStepView extends FrameLayout {
@BindView(R.id.container)
FrameLayout mContentView;
public WizardBaseStepView(Context context) {
super(context);
init();
}
public WizardBaseStepView(Context context, AttributeSet attrs) {
super(context, attrs);
treatAttibutes(context, attrs);
init();
}
public WizardBaseStepView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
treatAttibutes(context, attrs);
init();
}
protected void treatAttibutes(Context context, AttributeSet attrs) {
// TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.WizardBaseStepView);
// final int N = a.getIndexCount();
// for (int i = 0; i < N; ++i) {
// int attr = a.getIndex(i);
// switch (attr) {
// case R.styleable.WizardBaseStepView_title:
// mTitleStep = a.getString(attr);
// break;
//
// case R.styleable.WizardBaseStepView_editorTitle:
// mEditorTitle = a.getString(attr);
// break;
// }
// }
// a.recycle();
}
protected void init() {
View view = inflate(getContext(), R.layout.wizard_base_step_view, this);
ButterKnife.bind(this, view);
inflate(getContext(), getContentLayoutResource(), mContentView);
setSaveEnabled(true);
}
}
所有字段都是ID。
我将此视图用于寻呼机适配器(使用viewpager)。这是一个标准的实现。
正确调用方法恢复和保存;但是我的mNameEditText
总是不适合;我无法理解为什么因为在恢复实例方法中给出的值是正确的(我用调试器验证了它):mNameEditText.setValue(ss.mNameEditTextSaved);
你能帮帮我们吗?
非常感谢!