RecycleViewのEditText入力ボックス
3083 ワード
RecycleViewリストのItemにEditText入力ボックスがある場合は、ViewHolderが多重化されているため、1つのItemの値を入力すると、スライドリストは次の位置でもこの値が繰り返されます.
解決方法は以下の通りです.
解決方法は以下の通りです.
public class HaoLiaoAdapter extends RecyclerView.Adapter {
public void setDataList(ArrayList dataList) {
this.dataList = dataList;
notifyDataSetChanged();
}
private ArrayList 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()));
}
}
}
}