【问题标题】:Android Compound Custom View restore save stateAndroid 复合自定义视图恢复保存状态
【发布时间】:2017-01-04 14:28:19
【问题描述】:

我创建了包含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。

我还尝试在保存状态this.getId()+"editText" 期间将自定义视图的id 添加为键的一部分,但仍然是同样的问题。

编辑 genrateViewId 用于 api

修改后的代码

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;
            }
        }
    }
}

【问题讨论】:

  • 我不确切知道您尝试过的其他事情,但是您在此处发布的代码肯定会返回这些结果。您继续将所有值保存到同一个 editText 键,因此它继续被覆盖,因此您保留最后一个,即 5。您说您尝试了其他方法,发布该代码。
  • 我会的,但要考虑到并非每个视图都将其状态存储在不同的包中。
  • 你为什么要处理保存状态。操作系统应该保存editText的基本状态。您仅为自定义变量保存状态。
  • 同样的问题会发生,否则我根本不会考虑保存状态

标签: android android-fragments android-custom-view


【解决方案1】:

您的问题是您有多个 ViewGroups (LinearLayout),并且孩子具有相同的 id。因此,在保存状态时,它们都以相同的状态保存,最后一个覆盖所有。
为了解决这个问题,你必须在你膨胀时给每个视图一个唯一的 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 时。

【讨论】:

  • 经过深入调查,每次调用该自定义视图的restoreState时,都会返回相同的editText对象。我设法使用您的部分代码来帮助我正确恢复状态
  • 2 cmets。你最好调用你的 myGenerateViewId,因为现在的方式,不清楚你正在使用哪个,并且可能会产生编译器问题。 2. 您可以从键名中删除 getId(),因为这不是问题。 3.我不确定恢复后会发生什么,可能是构造函数被调用。如果是这样,您将获得 editText 的新 ID,那么它如何知道要恢复什么。如果这听起来令人困惑,那是因为我有点困惑如何在复合视图上进行恢复。
  • 如果这样做,如何保证restore过程中创建的id与create过程中生成的id相同?
  • @kalinga 我会简短地说。活动和片段之间有很大的区别。在 Fragments 中,操作系统会恢复实际的视图,因此除了确保每个视图都有唯一的 id 之外,不需要任何进一步的操作。在活动中,应用程序必须重新创建视图,然后操作系统通过调用 restoreState 将其恢复为状态。为此,应用程序有责任确保 id 与第一次相同,否则 restoreState 无法恢复它。这就是我建议使用 staticID 的原因,这样您就可以使用相同的逻辑并使用相同的 id。
  • > 您是否在示例中验证了此语句?我自己做了测试,它不起作用。 Android 对视图一无所知,因为新生成的 id 与旧的不同。即使视图不是动态的。请使用简单的自定义视图创建一个简单的片段,您使用generateId 以编程方式设置其 id 并查看视图的状态是否已恢复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
  • 2012-05-26
  • 1970-01-01
  • 1970-01-01
  • 2013-04-04
相关资源
最近更新 更多