[英]Android Compound Custom View restore save state
我創建了復合自定義視圖,其中包含名為LabledEditText
TextView
和EditText
,因為我在片段中會有很多EditText字段。
我創建了一個包含以下內容的XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayoutLabeledEditText"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/label_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/value_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:freezesText="true"
android:saveEnabled="true"/>
</LinearLayout>
在View類中如下
public class LabeledEditText extends LinearLayout {
private EditText editText;
private TextView label;
public LabeledEditText(Context context, AttributeSet attrs) {
super(context, attrs);
inflate(context,R.layout.labeled_edit_text, this);
label = (TextView) this.findViewById(R.id.label_textView);
label.setText("some label");
editText = (EditText) this.findViewById(R.id.value_editText);
}
protected void onRestoreInstanceState(Parcelable state) {
String id= this.getId()+" ";
if (state instanceof Bundle) // implicit null check
{
Bundle bundle = (Bundle) state;
state = bundle.getParcelable(id+"super");
super.onRestoreInstanceState(state);
editText.setText(bundle.getString(id+"editText"));
}
}
protected Parcelable onSaveInstanceState() {
String id= this.getId()+" ";
Bundle bundle = new Bundle();
bundle.putParcelable(id+"super",super.onSaveInstanceState());
bundle.putString(id+"editText",editText.getText().toString());
return bundle;
}
}
然后我在代表3個步驟的3個片段中使用它。 當我在前一步/片段中插入值時
然后切換到其他片段並再次返回1步/片段我發現以下內容
是什么導致了這個問題?
我已經調試了至少5天,請記住,在片段布局中使用時,每個自定義視圖都有不同的ID。
我還試圖在保存狀態時添加自定義視圖的id作為鍵的一部分this.getId()+"editText"
仍然是同樣的問題。
編輯 genrateViewId for api <17
改變后的代碼
import java.util.concurrent.atomic.AtomicInteger;
public class LabeledEditText extends LinearLayout {
private EditText editText;
private TextView label;
public LabeledEditText(Context context, AttributeSet attrs) {
super(context, attrs);
inflate(context,R.layout.labeled_edit_text, this);
label = (TextView) this.findViewById(R.id.label_textView);
editText = (EditText) this.findViewById(R.id.value_editText);
editText.setId(generateViewId());
applyAttr(context,attrs);
}
@Override
protected Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
//adding the id of the parent view as part of the key so that
//editText state won't get overwritten by other editText
//holding the same id
bundle.putParcelable("super",super.onSaveInstanceState());
bundle.putString("editText",editText.getText().toString());
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) // implicit null check
{
Bundle bundle = (Bundle) state;
state = bundle.getParcelable("super");
super.onRestoreInstanceState(state);
editText.setText(bundle.getString("editText"));
}
}
private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
public static int generateViewId() {
for (;;) {
final int result = sNextGeneratedId.get();
// aapt-generated IDs have the high byte nonzero; clamp to the range under that.
int newValue = result + 1;
if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
if (sNextGeneratedId.compareAndSet(result, newValue)) {
return result;
}
}
}
}
您的問題是您有多個ViewGroups(LinearLayout),其子項具有相同的ID。 因此,在保存狀態時,所有這些狀態都將保存在同一狀態,而最后一個狀態將覆蓋所有狀態。
要解決這個問題,你必須給每個視圖一個獨特的,當你膨脹。 在v17及更高版本中,您可以使用View.generateViewId();
,在舊版本中,您必須在ids文件中手動創建靜態ID。
你的代碼應該是這樣的;
public LabeledEditText(Context context, AttributeSet attrs) {
super(context, attrs); inflate(context,R.layout.labeled_edit_text, this);
label = (TextView) this.findViewById(R.id.label_textView); label.setText("some label");
editText = (EditText) this.findViewById(R.id.value_editText);
label.setId(View.generateViewId());
editText.setId(View.generateViewId());
}
在任何情況下,使用靜態ID可能更好,因為以后更容易引用它們。 你可能甚至不需要覆蓋onSave和onRestore,特別是如果你使用靜態id。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.