在开发过程中,有这样一个录入数据的需求,如下图,item中既包括EditText,也包括RadioGroup,在EditText输入数据或者RadioGroup中选中条目,在RecyclerView滑动过程中会导致数据的错乱,废话不多说,有图有真相。
其中EditText也类似
解决后的效果:
解决方法:
先贴出数据的model
public class CheckItemModel {
public String CheckTypeId;
public String CheckTypeName;
public boolean Flag;//正常异常表示,false:为正常异常,true为手动自动
public String isNormal;//正常异常
public String isAuto;//手动自动
public String exceptionInfo = "";//备注异常信息
}
在onBindItemHolder中
@Override
public void onBindItemHolder(SuperViewHolder holder, final int position) {
// holder.setIsRecyclable(false);
final CheckItemModel checkItemModel = mDataList.get(position);
TextView mNameTextView = holder.getView(R.id.tv_item_name);
RadioGroup mStatusRadioGroup = holder.getView(R.id.rg_status);
RadioButton mNormalRadioButton = holder.getView(R.id.rb_normal);
RadioButton mErrorRadioButton = holder.getView(R.id.rb_error);
EditText mExceptionEditText = holder.getView(R.id.et_exception);
TextWatcher watcher = (TextWatcher) mExceptionEditText.getTag();
if(watcher != null){
mExceptionEditText.removeTextChangedListener(watcher);
}
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
checkItemModel.exceptionInfo = s.toString();
}
};
mExceptionEditText.addTextChangedListener(textWatcher);
//RadioGroup
mStatusRadioGroup.setOnCheckedChangeListener(null);
if(checkItemModel.Flag){//true为手动自动
mNormalRadioButton.setText("手动");
mErrorRadioButton.setText("自动");
if("自动".equals(checkItemModel.isAuto)){
mStatusRadioGroup.check(R.id.rb_error);
}else if("手动".equals(checkItemModel.isAuto)){
mStatusRadioGroup.check(R.id.rb_normal);
}else {
mStatusRadioGroup.clearCheck();
}
}else {
mNormalRadioButton.setText("正常");
mErrorRadioButton.setText("异常");
if("正常".equals(checkItemModel.isNormal)){
mStatusRadioGroup.check(R.id.rb_normal);
}else if("异常".equals(checkItemModel.isNormal)){
mStatusRadioGroup.check(R.id.rb_error);
}else {
mStatusRadioGroup.clearCheck();
}
}
mStatusRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
switch (checkedId) {
case R.id.rb_normal://正常 手动
if (checkItemModel.Flag) {
checkItemModel.isAuto = "手动";
} else {
checkItemModel.isNormal = "正常";
}
break;
case R.id.rb_error://异常 自动
if (checkItemModel.Flag) {
checkItemModel.isAuto = "自动";
} else {
checkItemModel.isNormal = "异常";
}
break;
}
}
});
Log.e("399","position: " + position + " isNormal: " + checkItemModel.isNormal + " CheckTypeName: " + checkItemModel.CheckTypeName + " exceptionInfo: " + checkItemModel.exceptionInfo);
mNameTextView.setText(checkItemModel.CheckTypeName);
mExceptionEditText.setText(checkItemModel.exceptionInfo);
mExceptionEditText.setTag(textWatcher);
}