在RecycleView列表的Item中若有EditText输入框时,因为ViewHolder复用,所以输入一个Item的数值后,滑动List会在下一个位置也重复出现此数值。
解决方法参考如下:
public class HaoLiaoAdapter extends RecyclerView.Adapter<HaoLiaoAdapter.ViewHolder> {
public void setDataList(ArrayList<RiLiaoBean> dataList) {
this.dataList = dataList;
notifyDataSetChanged();
}
private ArrayList<RiLiaoBean> dataList;
public HaoLiaoAdapter() {
dataList = new ArrayList<>();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.haoliao_item_layout, parent, false);
return new ViewHolder(v, new CustomTextWatcher());
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
RiLiaoBean riLiaoBean = dataList.get(position);
if (riLiaoBean.getType() == 0) {//未上报
holder.inHaoLiaoEditText.setVisibility(View.VISIBLE);
holder.haoliaoTextView.setVisibility(View.GONE);
holder.customTextWatcher.updatePosition(position);
if(riLiaoBean.getCfhaoqty()>0) {
holder.inHaoLiaoEditText.setText(riLiaoBean.getCfhaoqty() + "");
}else {
holder.inHaoLiaoEditText.setText(null);
}
} else {//已上报
holder.inHaoLiaoEditText.setVisibility(View.GONE);
holder.haoliaoTextView.setVisibility(View.VISIBLE);
holder.haoliaoTextView.setText(riLiaoBean.getCfhaoqty() + "包");
}
holder.dateTitleTextView.setText(riLiaoBean.getHaoliaodate() + "(" + riLiaoBean.getRiling() + "日龄)");
holder.biaozhunTextView.setText("标准日耗:" + riLiaoBean.getBzrlh() + "包");
}
@Override
public int getItemCount() {
return dataList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.dateTitleTextView)
TextView dateTitleTextView;
@BindView(R.id.biaozhunTextView)
TextView biaozhunTextView;
@BindView(R.id.haoliaoTextView)
TextView haoliaoTextView;
@BindView(R.id.inHaoLiaoEditText)
EditText inHaoLiaoEditText;
CustomTextWatcher customTextWatcher;
public ViewHolder(View view, CustomTextWatcher customTextWatcher) {
super(view);
ButterKnife.bind(this, view);
this.customTextWatcher = customTextWatcher;
inHaoLiaoEditText.addTextChangedListener(customTextWatcher);
}
}
private class CustomTextWatcher implements TextWatcher {
private int position;
public void updatePosition(int position) {
this.position = position;
}
@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) {
if(s.toString()!=null&&!s.toString().equals("")) {
dataList.get(position).setCfhaoqty(Double.parseDouble(s.toString()));
}
}
}
}
网友评论