更改项目中的值也会在另一个项目中更改-RecyclerView Android

问题描述 投票:0回答:1

问题出在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();
    }
}
java android android-recyclerview
1个回答
0
投票

由于RecyclerView的性质。

[RecyclerView尝试在绘制新项目时重用(回收)已经绘制的项目(viewHolders)。

因为从回收的视图中获得了input,所以它已经有一个值,并且会继续从该值增加/减少。

您要做的只是在holder.quantity.setText(some default value);方法中将onBindView或将输入存储在模型中。

© www.soinside.com 2019 - 2024. All rights reserved.