异常描述:
<include
android:id="@+id/item_ll1"
layout="@layout/base_input_layout"/>
<include
android:id="@+id/item_ll2"
layout="@layout/base_input_layout"/>
<include
android:id="@+id/item_ll3"
layout="@layout/base_input_layout"/>
<include
android:id="@+id/item_ll4"
layout="@layout/base_input_layout"/>
base_input_layout代码如下
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="@dimen/title_ll_max_width"
android:layout_height="45dp"
android:layout_marginEnd="20dp"
android:orientation="horizontal">
<TextView
android:id="@+id/input_text_title_tv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:maxWidth="@dimen/title_max_width"
android:gravity="center_vertical"
android:textColor="@color/color999999"
android:textSize="15sp" />
<TextView
android:id="@+id/text_must_tv"
android:layout_width="10dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="*"
android:textColor="@color/colorFF0000"
android:textSize="20sp"/>
</LinearLayout>
<EditText
android:id="@+id/input_text_content_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="@color/transparent"
android:gravity="center_vertical"
android:inputType="text"
android:textColor="@color/color333333"
android:textSize="15sp" />
</LinearLayout>
fragment布局代码中大量引入此种复用布局,fragment show时上次输入内容全部展示最后一个EditText有值的内容,且初始化时setTest("")无效
原因分析:
具体原因我已找到,布局复用,
参考文章来源,详情请查看
https://blog.csdn.net/u011494050/article/details/45678305
解决方案:
//如用到 include 方式,父布局请用此View作为根布局,防止View状态错乱
public class BaseLinearLayout extends LinearLayout {
public BaseLinearLayout(Context context) {
super(context);
}
public BaseLinearLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public BaseLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.childrenStates = new SparseArray();
for (int i = 0; i < getChildCount(); i++) {
getChildAt(i).saveHierarchyState(ss.childrenStates);
}
return ss;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
for (int i = 0; i < getChildCount(); i++) {
getChildAt(i).restoreHierarchyState(ss.childrenStates);
}
}
@Override
protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
dispatchFreezeSelfOnly(container);
}
@Override
protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
dispatchThawSelfOnly(container);
}
static class SavedState extends BaseSavedState {
SparseArray childrenStates;
SavedState(Parcelable superState) {
super(superState);
}
private SavedState(Parcel in, ClassLoader classLoader) {
super(in);
childrenStates = in.readSparseArray(classLoader);
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeSparseArray(childrenStates);
}
public static final ClassLoaderCreator<SavedState> CREATOR = new ClassLoaderCreator<SavedState>() {
@Override
public SavedState createFromParcel(Parcel source, ClassLoader loader) {
return new SavedState(source, loader);
}
@Override
public SavedState createFromParcel(Parcel source) {
return createFromParcel(null);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
}
至此完美解决,欢迎大佬留言指正不足之处!