在我的活动中,有一个RecyclerView和一个按钮,用于向RecyclerView添加项目。每个项目都有两个EditTexts
Here's a Screenshot
当我向下和向上滚动时,EditTexts变空。有时在EditText中输入的值会改变它的位置!我该如何解决这个问题呢?
以下是代码:
适配器:
public class VoltageAdapter extends RecyclerView.Adapter<VoltageAdapter.DataViewHolder> {
private List<Voltage> datas = new ArrayList<>();
public List<Voltage> getDatas() {
return datas;
}
@Override
public DataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.item_voltage, parent, false);
return new DataViewHolder(view);
}
@Override
public void onBindViewHolder(DataViewHolder holder, int position) {
holder.bindData(datas.get(position));
}
@Override
public int getItemCount() {
return datas.size();
}
public static class DataViewHolder extends RecyclerView.ViewHolder {
private EditText vOnEditText;
private EditText vOffEditText;
public DataViewHolder(View itemView) {
super(itemView);
vOnEditText = itemView.findViewById(R.id.et_ItemVoltage_Von);
vOffEditText = itemView.findViewById(R.id.et_ItemVoltage_Voff);
}
public void bindData(final Voltage voltage) {
vOnEditText.setText(voltage.getVon());
vOffEditText.setText(voltage.getVoff());
vOnEditText.addTextChangedListener(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) {
voltage.setVon(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
vOnEditText.setSelection(vOnEditText.getText().length());
}
});
vOffEditText.addTextChangedListener(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) {
voltage.setVoff(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
public void addData(Voltage voltage) {
datas.add(voltage);
notifyItemInserted(datas.size() - 1);
}
}
活动类:
public class VoltageActivity extends AppCompatActivity {
private VoltageAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voltage);
setupViews();
}
private void setupViews() {
Button calcucalteButton = findViewById(R.id.btn_voltage_calculate);
Button addDataButton = findViewById(R.id.btn_voltage_addData);
adapter = new VoltageAdapter();
RecyclerView recyclerView = findViewById(R.id.rv_voltage_voltages);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
recyclerView.setAdapter(adapter);
addDataButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Voltage voltage = new Voltage();
adapter.addData(voltage);
}
});
calcucalteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<Voltage> datas = adapter.getDatas();
float DeltaVoltages[] = new float[datas.size()];
for (int i = 0; i < datas.size(); i++) {
Voltage voltage = datas.get(i);
float res = Float.parseFloat(voltage.getVon()) - Float.parseFloat(voltage.getVoff());
DeltaVoltages[i] = res;
}
float minDelta = DeltaVoltages[0];
for (int i = 0; i < DeltaVoltages.length; i++) {
if (DeltaVoltages[i] < minDelta) minDelta = DeltaVoltages[i];
}
Log.i("VoltageActivity", Arrays.toString(DeltaVoltages));
Log.i("Min is ", String.valueOf(minDelta));
Toast.makeText(VoltageActivity.this, "Min Delta V is " + String.valueOf(minDelta), Toast.LENGTH_SHORT).show();
}
});
}
}
答案 0 :(得分:0)
你的适配器应该是这样的:
public class VoltageAdapter extends RecyclerView.Adapter<VoltageAdapter.ViewHolder> {
private final Context context;
private final BorderSelectedListener borderSelectedListener;
public VoltageAdapter(Context context, BorderSelectedListener borderSelectedListener) {
this.context = context;
this.borderSelectedListener = borderSelectedListener;
}
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(this.context).inflate(R.layout.overlay_adapter_item, parent, false));
}
public void onBindViewHolder(@NonNull final ViewHolder holder, @SuppressLint("RecyclerView") int position) {
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
borderSelectedListener.onBorderSelected(holder.getAdapterPosition());
}
});
holder.vOnEditText.setText(getDatas().get(position).getVon());
holder.vOffEditText.setText(getDatas().get(position).getVoff());
}
public int getItemCount() {
return datas.size();
}
public interface BorderSelectedListener {
void onBorderSelected(int position);
}
class ViewHolder extends RecyclerView.ViewHolder {
private EditText vOnEditText;
private EditText vOffEditText;
ViewHolder(View itemView) {
super(itemView);
vOnEditText = itemView.findViewById(R.id.et_ItemVoltage_Von);
vOffEditText = itemView.findViewById(R.id.et_ItemVoltage_Voff);
}
}
}
答案 1 :(得分:0)
这是正确的行为,因为RecyclerView
在滚动时会重复使用项目。您可以使用此库来恢复状态 - RendererRecyclerViewAdapter,请参阅ViewState维基页面。