这是一个Android模块。当我在另一个布局上使用它时:
<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的onSaveInstanceState
和onRestoreInstanceState
已经过了。但是在没有调用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 :(得分:0)
您可以覆盖Car
方法并通过这种方式处理更改
Car
答案 1 :(得分: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);
}
答案 2 :(得分:0)
如果扩展LinearLayout,则需要设置Id,因为android不会调用onsave和onrestore。 所以调用setID(uniqeId ++)