java - 更改项目中的值也会更改另一个项目 - RecyclerView Android
问题描述
问题出在 recyclerview 中,如果其中一个项目中的视图发生更改,那么另一个项目中的视图也会更改。它在要更改的实际项目的位置之后的第 8 个位置发生变化。比如说,我在位置 1 更改了商品的数量或价格,那么位置 1 + 8 = 第 9 件商品的数量或价格也发生了变化。我无法弄清楚为什么会发生这种情况,因为我只是在查看器位置更改视图。如果有人遇到这种问题或有任何解决方案,那将会很有帮助。谢谢你。
RecyclerView 布局:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/product_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_10sdp"
android:visibility="visible"/>
适配器:
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.MyViewHolder> {
private List<ProductItem> productItemList;
private Context context;
private ExpenseActivityContract.Presenter presenter;
public ProductAdapter(Context context, ExpenseActivityContract.Presenter presenter, List<ProductItem> productItemList) {
this.productItemList = productItemList;
this.presenter = presenter;
this.context = context;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView productImage;
TextView productName, add;
EditText quantity, price;
ImageButton plus, minus;
public MyViewHolder(View v) {
super(v);
productImage = v.findViewById(R.id.product_image);
productName = v.findViewById(R.id.product_name);
add = v.findViewById(R.id.add_product_btn);
quantity = v.findViewById(R.id.quantity_input);
price = v.findViewById(R.id.product_price_input);
plus = v.findViewById(R.id.plus_qnty_btn);
minus = v.findViewById(R.id.minus_qnty_btn);
}
}
@Override
public ProductAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.product_item_layout, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.productName.setText(productItemList.get(position).getProductName());
holder.plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String input = holder.quantity.getText().toString();
if(TextUtils.isEmpty(input)) {
holder.quantity.setText("1");
}
else {
double qnty = Double.parseDouble(input);
qnty = qnty + 1.00;
holder.quantity.setText(String.valueOf(qnty));
}
}
});
holder.minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String input = holder.quantity.getText().toString();
if(TextUtils.isEmpty(input)) {
holder.quantity.setText("0");
}
else {
double qnty = Double.parseDouble(input);
if(qnty > 1.0) {
qnty = qnty - 1.00;
holder.quantity.setText(String.valueOf(qnty));
}
else if(qnty >= 0.0 && qnty <= 1.0) {
holder.quantity.setText("0");
}
}
}
});
}
@Override
public int getItemCount() {
return productItemList.size();
}
}
解决方案
这是因为 RecyclerView 的性质。
RecyclerView 在绘制新项目时尝试重用(回收)已绘制的项目(viewHolders)。
由于您input
从被回收的视图中获得,它已经具有一个值并继续从该值增加/减少。
您需要做的就是holder.quantity.setText(some default value);
在onBindView
方法中或将输入存储在模型中。
推荐阅读
- css - 如何完全以角度呈现来自组件的不同页面?
- vb.net - 警告 1 参数不正确。(来自 HRESULT 的异常:0x80070057 (E_INVALIDARG))0 0
- javascript - 在 fetch 调用填充 Redux 存储时反应延迟渲染
- encryption - 用户注销后,无法从 SQL 代理进行 GPG 解密
- amazon-web-services - 如何在 Spring Boot 应用程序中注入 docker 环境变量
- node.js - Node.js 只有一名工作人员工作
- c - 从标准输入读取到 FILE*
- r - 使用第三个变量添加大小和颜色以使用 plotly 进行绘图
- apache-spark - 如何避免在 Spark DataFrames 中多次执行相同的转换?
- asp.net - ASP.NET GridView Bootstrap 3 手风琴