Android Compound Custom View恢复保存状态

约翰·克罗斯

我创建了包含TextViewEditText称为的复合自定义视图LabledEditText,因为在一个片段中将有很多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个步骤/片段中插入值时

第一步之后的第一次

然后切换到其他片段,然后再次返回到第1步/片段,我发现以下内容

第二次访问第一步

是什么引起了这个问题?

我已经调试了至少5天,请记住,在片段布局中使用这些自定义视图时,每个视图都有不同的ID。

在保存状态的过程中,我还尝试过将自定义视图的ID作为键的一部分添加,this.getId()+"editText"仍然存在相同的问题。

编辑api <17genrateViewId

修改后的代码

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的情况下。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

唯一识别android Compound View内部的元素

来自分类Dev

在 Android 中使用 RecyclerView 保存/恢复 Fragment 状态

来自分类Dev

android防止视图恢复状态

来自分类Dev

保存开关状态android

来自分类Dev

Android Exoplayer 保存状态

来自分类Dev

Android活动导航保存状态

来自分类Dev

Android保存多个CheckBoxes的状态

来自分类Dev

Android searchview 保存错误状态

来自分类Dev

在 Android 中保存 UI 状态

来自分类Dev

Custom Contacts List in Android

来自分类Dev

Custom Square相机-Android

来自分类Dev

Android Custom Image Buttons

来自分类Dev

Android Lenovo custom ui

来自分类Dev

Custom Square相机-Android

来自分类Dev

如何使用react-compound-timer getTime()方法更新组件状态

来自分类Dev

从保存状态恢复递归函数

来自分类Dev

保存和恢复片段状态?

来自分类Dev

如何在iOS和Android上获取应用恢复状态?

来自分类Dev

Android将已解析的数据恢复为活动状态

来自分类Dev

恢复方向更改android的当前活动状态

来自分类Dev

防止Android(和MVVMCross)中的片段状态自动恢复

来自分类Dev

Android ExpandableListView如何保存展开/折叠状态?

来自分类Dev

在Android中保存状态无法正常工作

来自分类Dev

Android如何保存状态数组列表

来自分类Dev

Android不会自动保存视图状态

来自分类Dev

将活动状态保存在android中?

来自分类Dev

在Android中保存和还原片段状态

来自分类Dev

Android如何保存状态数组列表

来自分类Dev

在Android应用中保存全局状态