android-RecyclerView在新行中显示在EditText中输入的先前值
作者:互联网
我正在创建一个Android应用程序,其中使用recyclerView,recyclerView行中有editText.
这是我的ReadingAdapter类
public class ReadingAdapter extends RecyclerView.Adapter<ReadingAdapter.ViewHolder> implements AdapterView.OnItemSelectedListener {
Context context;
String valOpenReading, valClosReading, valConsumption;
private List<ReadingData> readingList;
static String[] arrValOpenRead, arrValClosRead, arrValConsumption;
public ReadingAdapter(Context context, List<ReadingData> readingList) {
this.context = context;
this.readingList = readingList;
arrValOpenRead = new String[readingList.size()];
arrValClosRead = new String[readingList.size()];
arrValConsumption = new String[readingList.size()];
}
@Override
public ReadingAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.reading_sheet_layout, parent, false);
return new ReadingAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(final ReadingAdapter.ViewHolder holder, final int position) {
ReadingData tempData = readingList.get(position);
holder.pdtName.setText(tempData.pdtName);
holder.keyId.setText("Key "+tempData.keyId);
holder.etClosRead.addTextChangedListener(new TextWatcher() {
boolean ignore = false;
@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 (ignore)
return;
ignore = true;
valOpenReading = holder.etOpenRead.getText().toString();
arrValOpenRead[position] = valOpenReading;
valClosReading = s.toString().equals("") ? "0": s.toString();
arrValClosRead[position] = valClosReading;
if (!valOpenReading.equals("")) {
if (Integer.parseInt(valClosReading) < Integer.parseInt(valOpenReading)) {
Toast.makeText(context, "Check once! closing reading should be more than opening reading!", Toast.LENGTH_LONG).show();
valConsumption = "0";
holder.consumption.setText("");
} else {
valConsumption = (Integer.parseInt(valClosReading) - Integer.parseInt(valOpenReading))+"";
arrValConsumption[position] = valConsumption;
holder.consumption.setText(valConsumption);
}
} else
Toast.makeText(context, "Please fill the opening reading!", Toast.LENGTH_SHORT).show();
ignore = false;
}
});
}
@Override
public int getItemCount() {
return readingList.size();
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView pdtName, keyId, consumption;
EditText etOpenRead, etClosRead;
public ViewHolder(View view) {
super(view);
pdtName = (TextView)view.findViewById(R.id.txt_list_pdt_supp);
keyId = (TextView)view.findViewById(R.id.key_set);
etOpenRead = (EditText)view.findViewById(R.id.open_val_set);
etClosRead = (EditText)view.findViewById(R.id.clos_val_set);
consumption = (TextView)view.findViewById(R.id.consumption_val);
}
}
}
这是我的ReadingData.java
public class ReadingData {
String pdtName, keyId, openReading, closReading, consumption;
public ReadingData(String pdtName, String keyId) {
this.pdtName = pdtName;
this.keyId = keyId;
}
}
在这里,如果我在recyclerView的起始项中输入值,那么当我向上滚动项至列表的底部时,最后一项将具有该值.
请忽略图片质量,因为我们无法上传2MiB以上的快照.
在此,列表滚动时视图被回收.如何防止将值复制到列表中的其他项目.
那个吐司也要重复几次.如何阻止这个.
更新:
通过SO问题How ListView’s recycling mechanism works的建议LQ Gioan,我得到了ListView实际上如何与视图回收一起工作的逻辑.
但是我不确定recyclerView是否也能正常工作.
但是在我的情况下,我该如何实施此过程.请有人在这里帮助我.
解决方法:
RecyclerView重用视图,实际上它只生成与屏幕上可见的视图一样多的视图.因此,如果可以看到为其他行设置的值,这是可以预期的
该解决方案将设置为您要更改为默认视图的视图的所有属性,或者应从数据集中显示任何行
因此,将addTextChangedListener放入insode ViewHolder构造函数中(您可以通过调用getAdapterPosition()来获得位置)以获得更好的性能,并从数据集中设置onBindViewHolder方法中的editText值
标签:recycler-adapter,android,android-recyclerview,addtextchangedlistener 来源: https://codeday.me/bug/20191013/1909240.html