2017-07-18 50 views
0

我有看法是這樣的: enter image description hereAndroid的方向保存佈局子狀態

這是一個機器人模塊。當我在另一個佈局上使用它時:

<com.example.myapp.MyModuleView 
     android:id="+id/test" ... 

當我得到引用時,我可以調用MyModuleView類的方法。

MyModuleView v = (MyModuleView)findViewById(R.id.test) 

當我創建MySeekBarLayout:

MySeekBarLayout l = new MySeekBarLayout(); 
l.setProgress(10) 

這種佈局是:

MySeekBarLayout擴展的LinearLayout

當我加入這個觀點mymoduleview:

v.getLayout().addView(l.getLayout()) 

所以佈局被添加到父佈局。 它的工作很好。

但是,當我旋轉我的手機,比MyModuleView的onSaveInstanceStateonRestoreInstanceState runned。但附加的佈局不需要撥打電話onRestoreInstanceState。我怎樣才能保存附加視圖的狀態? 謝謝!

public class MySeekBarView<T> extends TableRow { 

    private SeekBar seekBar; 
    private TextView seekBarValue; 

    public MySeekBarView(Context context, String title) { 
     super(context); 
     inflate(getContext(), R.layout.layout_seekbar, this); 
    } 

    @Override 
    protected void onViewCreated() { 
     seekBar = findViewById(R.id.my_seekbar_input); 
     seekBarValue = findViewById(R.id.my_seekbar_value); 
     seekBarValue.setText(String.valueOf(seekBar.getProgress())); 
    } 


} 

MyModuleView附上孩子:

view是MySeekBarView istance!

TableRow tr = new TableRow(getContext()); 
TextView textView = (TextView) factory.inflate(R.layout.textview_layout, null); 
textView.setText("Test:"); 
tr.addView(textView); 
tr.addView(view, new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1f)); 
tr.setGravity(Gravity.CENTER_VERTICAL); 
table.addView(tr); 
+0

您是否已爲[MyModuleView]執行[BaseSavedState](https://stackoverflow.com/a/3542895/1083957)? – azizbekian

+0

不,我沒有。但意見不分離。它被重新創建。我需要保存實例。刪除所有視圖,並重新附加? – ketom

+0

你可以發佈你的onRestoreInstanceState代碼嗎? –

回答

0

如果擴展LinearLayout,則需要設置Id,因爲android不會調用onsave和onrestore。 so call setID(uniqeId ++)

0

您可以覆蓋onConfigurationChanged方法,並通過這種方式

@Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { 

      //Your Code Here 
     } 
     if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
      //Your Code Here 
     } 
    } 
0

處理這些變化的方法添加到您的自定義視圖代碼。

static class SavedState extends BaseSavedState { 
     int progress; 

     SavedState(Parcelable superState) { 
      super(superState); 
     } 

     private SavedState(Parcel in) { 
      super(in); 
      progress = in.readInt(); 
     } 

     @Override 
     public void writeToParcel(Parcel out, int flags) { 
      super.writeToParcel(out, flags); 
      out.writeInt(progress); 
     } 

     public static final Parcelable.Creator<SavedState> CREATOR 
       = new Parcelable.Creator<SavedState>() { 
      public SavedState createFromParcel(Parcel in) { 
       return new SavedState(in); 
      } 

      public SavedState[] newArray(int size) { 
       return new SavedState[size]; 
      } 
     }; 
    } 

    @Override 
    public Parcelable onSaveInstanceState() { 
     Parcelable superState = super.onSaveInstanceState(); 
     SavedState state = new SavedState(superState); 

     //save your view states 
     state.progress = mProgress; 

     return ss; 
    } 

    @Override 
    public void onRestoreInstanceState(Parcelable state) { 
     SavedState ss= (SavedState) state; 
     super.onRestoreInstanceState(ss.getSuperState()); 

     //restore states. 
     setProgress(ss.progress); 
    } 
+0

但是,附加的孩子的onSaveInstanceState未運行。它只是重新創建 – ketom

+1

你可以發佈你的自定義視圖的代碼? –