如何管理recyclerview项目的按钮单击

时间:2019-04-11 11:06:46

标签: android android-recyclerview

我有recyclerview,它正在显示产品购物车清单,现在我添加了功能,用户可以更改产品数量,但是问题是,如果我更改第一个项目的数量,则会影响下一个或其他项目太。谁能检查一下,我也发布了代码....................................... .........

 private List<CartList> stList;

        String prodName = "", imgURL = "", prodSize = "", prodColor = "", prdAvail = "";
        double prodPrice = 0.0, totValue = 0.0;
        int prodQty = 1, prdAvl = 0;

        @NonNull
        @Override
        public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                                 int viewType) {
            @SuppressLint("InflateParams")
            View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.layout_cartlist_item, null);

            return new CardViewDataAdapter.ViewHolder(itemLayoutView);
        }

        CardViewDataAdapter(List<CartList> students) {
            this.stList = students;
        }

        @Override
        public void onBindViewHolder(@NonNull final CardViewDataAdapter.ViewHolder viewHolder, final int position) {

            try {
                prodName = stList.get(position).getCartList1().getProdName();
                imgURL = stList.get(position).getCartList1().getProdImage();
                prodSize = stList.get(position).getCartList1().getProdSize();
                prodColor = stList.get(position).getCartList1().getProdColor();
                prodQty = stList.get(position).getCartList1().getProdQty();
                prodPrice = stList.get(position).getCartList1().getProdQty();
                String productAvails = stList.get(position).getCartList1().getWarehouseDetail();
                totValue = stList.get(position).getCartList1().getGrandTotal() -
                        stList.get(position).getCartList1().getShippingCost();

                String avails[] = productAvails.split(":");
                prdAvail = avails[2];
                prdAvl = Integer.parseInt(prdAvail);

            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (stList.get(position).getCartList1().getSeller_id() == 0
                        || stList.get(position).getCartList1().getSeller_id() != 1) {
                    viewHolder.tvSellerName.setText("seller: Milmila");
                } else {
                    viewHolder.tvSellerName.setText("seller: Seajol Solutions");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            viewHolder.tvName.setText(prodName);
            viewHolder.tvProdPrice.setText("Rs. " + String.valueOf(totValue));
            viewHolder.tvProdQty.setText("Qty: " + String.valueOf(prodQty));
            viewHolder.tvProdSize.setText("Size: " + String.valueOf(prodSize));
            viewHolder.tvProdColor.setText("Color: " + String.valueOf(prodColor));
            if (prdAvail.isEmpty() || prdAvail.equalsIgnoreCase("0.0")) {
                viewHolder.prdAvail.setText("Out Of Stock");
            } else {
                viewHolder.prdAvail.setText("Available in stock: " + prdAvail);
            }

            Glide.with(mContext)
                    .load(imgURL)
                    .apply(new RequestOptions()
                            .placeholder(R.drawable.no_image))
                    .into(viewHolder.prodImage);

            viewHolder.chkSelected.setChecked(stList.get(position).getSelected());
            viewHolder.chkSelected.setTag(stList.get(position));

           // int quantity_count=prodQty = stList.get(position).getCartList1().getProdQty();

           viewHolder.edt_product_qty.setText(String.valueOf(prodQty));
            viewHolder.increase_product_qty.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    prodQty++;
                    viewHolder.edt_product_qty.setText(String.valueOf(prodQty));

                }
            });

            viewHolder.decrease_product_qty.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (prodQty>1)
                    {
                        prodQty--;
                        viewHolder.edt_product_qty.setText(String.valueOf(prodQty));

                    }

                }
            });

1 个答案:

答案 0 :(得分:0)

是的,因为在滚动recyclerview时有可能更改滚动状态和位置,所以会发生这种情况。

您正在使用相同的prodQty变量,并且直接在按钮单击内使用了viewHolder.edt_product_qty.setText(""),因此在这种情况下,edt_product_qty随滚动位置的不同而不同。

您可以在列表项中的数量编辑文本中使用android:freezesText="true"

您可以通过按钮上的标签来管理我的添加位置。

增加数量:

viewHolder.increase_product_qty.setTag(viewHolder.edt_product_qty);
viewHolder.increase_product_qty.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        EditText selEditText = (EditText) v.getTag();
        final int qty = Integer.parseInt(selEditText.getText().toString()) + 1;
        selEditText.setText(String.valueOf(qty));
    }
});

减少数量:

viewHolder.decrease_product_qty.setTag(viewHolder.edt_product_qty);
viewHolder.decrease_product_qty.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        EditText selEditText = (EditText) v.getTag();
        final int qty = Integer.parseInt(selEditText.getText().toString()) - 1;
         if (qty>1){
            selEditText.setText(String.valueOf(qty));
        }
    }
});