我遇到了一个问题,我知道根本原因,但没有看到解决问题的方法。如果在活动中多次使用自定义复合组件,则从视图保存的值将相互覆盖。为了更容易解释,我做了下面的例子。在屏幕上保存状态在多次使用复合组件时旋转
新组件的xml,只有一个EditText使它更短。
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<EditText
android:id="@+id/custom_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
</EditText>
</merge>
实现新行为的类只是使布局膨胀。
public class CustomView extends LinearLayout {
public CustomView(Context context) {
this(context, null);
}
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_view, this, true);
}
}
并使用其中的2个布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<test.customview.CustomView
android:id="@+id/customView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</test.customview.CustomView>
<test.customview.CustomView
android:id="@+id/customView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</test.customview.CustomView>
</LinearLayout>
当屏幕旋转时,第二个视图的值也会在第一个视图中恢复。
深入研究框架的代码我发现从View类中定义的onSaveInstanceState返回的Parcelable对象与关键对象的ID放在SparseArray中。因为我多次包含CustomView,因此ID为“custom_text”的EditText也会多次添加。具有相同的ID,保存的值将相互覆盖。
我在寻找任何有关如何实际应用的建议。现在,我看不出有什么办法来改变这些标识符。
谢谢你,你的代码正在工作:)我花了一段时间才明白为什么,但最终我得到了它。不要担心你的英语,这很好。欢迎来到StackOverflow :) – azertiti 2012-04-03 22:20:51