2

我遇到了一个问题,我知道根本原因,但没有看到解决问题的方法。如果在活动中多次使用自定义复合组件,则从视图保存的值将相互覆盖。为了更容易解释,我做了下面的例子。在屏幕上保存状态在多次使用复合组件时旋转

新组件的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,保存的值将相互覆盖。

我在寻找任何有关如何实际应用的建议。现在,我看不出有什么办法来改变这些标识符。

回答

2

似乎我有这个问题的一些解决方案。我试图找到它一段时间。

1.首先你必须创建内部类,它扩展了你的CustomView中的BaseSavedState。

CustomView{ 
    String value; //some text value from edittext 

    EditText edittext; 
     ... 

    private static class Save extends BaseSavedState{ 

    String savedValue; 

    public Save(Parcel incoming) { 
     super(incoming); 
     savedValue = incoming.readString(); 
     Log.i("Save", "Parcel"); 
    } 

    public Save(Parcelable parcelable) { 
     super(parcelable); 
     Log.i("Save", "Parcelable"); 
    } 

    @Override 
    public void writeToParcel(Parcel outcoming, int flags) { 
     super.writeToParcel(outcoming, flags); 
     outcoming.writeString(savedValue); 
     Log.i("Save", "writeToParcel"); 
    } 

    public static final Parcelable.Creator<Save> CREATOR = 
       new Creator<CustomView.Save>() { 

     @Override 
     public Save[] newArray(int size) { 
      Log.i("Parcelable.Creator<Save>", "newArray"); 
      return new Save[size]; 
     } 

     @Override 
     public Save createFromParcel(Parcel incoming) { 
      Log.i("Parcelable.Creator<Save>", "createFromParcel"); 
      return new Save(incoming); 
     } 
    }; 
} 

} 

2.然后重写此两个方法中CustomView

CustomView{ 
    String value; //some text value from edittext 

    EditText edittext; 

    ... 

    @Override 
protected Parcelable onSaveInstanceState() { 
    Log.i("CustomView", "onSaveInstanceState"); 

    Parcelable p = super.onSaveInstanceState(); 

    Save save = new Save(p); 
    save.savedValue = value; // value is from CustomView class 

    return save; 
} 

@Override 
protected void onRestoreInstanceState(Parcelable state) { 
    Log.i("CustomView", "onRestoreInstanceState"); 
    if(!(state instanceof Save)){ 
     super.onRestoreInstanceState(state); 
     return; 
    } 

    Save save = (Save) state; 
    value = save.savedValue; 
    //setting in this place value to edittext will not do anything. 
      //instead, you have to do this in step 3 
    super.onRestoreInstanceState(save.getSuperState()); 
} 

    ... 
} 

3.override onAttachedToWindow()和设置为EDITTEXT “值”。

CustomView{ 
    String value; //some text value from edittext 

    EditText edittext; 
    ... 
    @Override 
    protected void onAttachedToWindow() { 
      edittext.setText(value); 
      super.onAttachedToWindow(); 
    } 
    ... 
} 

,现在你可以有拒绝改变方向自定义视图的多个实例 - 他们将拥有正确values.I没有测试在100%这个解决方案,但它似乎也不错。

+0

谢谢你,你的代码正在工作:)我花了一段时间才明白为什么,但最终我得到了它。不要担心你的英语,这很好。欢迎来到StackOverflow :) – azertiti 2012-04-03 22:20:51