点击一个按钮会触发Android RecyclerView中的另一个按钮

点击一个按钮会触发Android RecyclerView中的另一个按钮,android,listview,android-recyclerview,buttonclick,Android,Listview,Android Recyclerview,Buttonclick,我目前正在开发一款电子商务Android应用程序。在我的recyclerview中,有13种产品是从数据库动态填充的。当我单击列表中的第一个按钮时,它也会更改列表中第11个按钮的按钮文本(我编码为在单击按钮时将按钮文本从“添加”更改为“删除”)。像wise to Second button一样,单击可更改第12个视图按钮的文本。 在日志中,我注意到两个按钮具有相同的内存值。 我做错什么了吗 以下是3次不同按钮点击(第1、第11和第2次按钮)生成的日志: 将ViewHolder与Recycler

我目前正在开发一款电子商务Android应用程序。在我的recyclerview中,有13种产品是从数据库动态填充的。当我单击列表中的第一个按钮时,它也会更改列表中第11个按钮的按钮文本(我编码为在单击按钮时将按钮文本从“添加”更改为“删除”)。像wise to Second button一样,单击可更改第12个视图按钮的文本。 在日志中,我注意到两个按钮具有相同的内存值。 我做错什么了吗

以下是3次不同按钮点击(第1、第11和第2次按钮)生成的日志:



将ViewHolder与RecyclerView一起使用


尝试此操作,首先在适配器保持器类中初始化一个变量。 比如,您的模型类名产品我认为,产品列表产品;然后在onBindViewHolder中尝试以下更改

     @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
holder.product = productList.get(position);
        Glide.with(mContext)
                .asBitmap()
                .load(holder.product.getImage())
                .into(holder.imageView);

        holder.itemname.setText(holder.product.getName());
        holder.itemprice.setText(holder.product.getPrice());
        holder.itemprice_g.setText(holder.product.getPrice_g());

        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!holder.product.isAddedTocart())
                {
                    holder.product.setAddedTocart(true);
                    String x = holder.product.getName().toString();
                    Log.i("log",""+holder.button);
                    holder.button.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                    holder.button.setPadding(0,0,0,0);
                    holder.button.setText("Remove");

                    ((AddorRemoveCallbacks)mContext).onAddProduct();

                }
                else
                {
                    holder.product.setAddedTocart(false);
                    holder.button.setText("Add");
                    holder.button.setPadding(12,0,0,0);
                    holder.button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_shopping_cart_black_24dp, 0, 0, 0);
                    ((AddorRemoveCallbacks)mContext).onRemoveProduct();
                }
            }
        });
    }

onBindViewHolder
中,您应该在
设置ClickListener
之前,首先初始化
holder.按钮的状态,例如:

...
holder.itemname.setText(productsList.get(position).getName());
holder.itemprice.setText(productsList.get(position).getPrice());
holder.itemprice_g.setText(productsList.get(position).getPrice_g());

if (productsList.get(position1).isAddedTocart()) {
    holder.button.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
    holder.button.setPadding(0,0,0,0);
    holder.button.setText("Remove");
} else {
    holder.button.setText("Add");
    holder.button.setPadding(12,0,0,0);
    holder.button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_shopping_cart_black_24dp, 0, 0, 0);
}

holder.button.setOnClickListener(new View.OnClickListener() {
...
为什么要使用“int position1=holder.getAdapterPosition();”
当您已经在bindholder中获得帖子时,请尝试删除它并使用默认帖子。

请提供您尝试的代码和UI。是的,我也尝试过,但问题仍然存在:(这解决了我的问题..但从技术上讲,我认为我的代码中仍然存在一些问题。因为为什么两个不同的按钮具有相同的内存空间..必须找到它。谢谢。这不是问题,因为视图持有者中的视图是重复使用的,这正是
RecyclerView
Recycler
的意思,因为视图是回收和重复使用时,每次都必须设置视图的状态,否则将得到不需要的过时状态:)
     @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
holder.product = productList.get(position);
        Glide.with(mContext)
                .asBitmap()
                .load(holder.product.getImage())
                .into(holder.imageView);

        holder.itemname.setText(holder.product.getName());
        holder.itemprice.setText(holder.product.getPrice());
        holder.itemprice_g.setText(holder.product.getPrice_g());

        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!holder.product.isAddedTocart())
                {
                    holder.product.setAddedTocart(true);
                    String x = holder.product.getName().toString();
                    Log.i("log",""+holder.button);
                    holder.button.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                    holder.button.setPadding(0,0,0,0);
                    holder.button.setText("Remove");

                    ((AddorRemoveCallbacks)mContext).onAddProduct();

                }
                else
                {
                    holder.product.setAddedTocart(false);
                    holder.button.setText("Add");
                    holder.button.setPadding(12,0,0,0);
                    holder.button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_shopping_cart_black_24dp, 0, 0, 0);
                    ((AddorRemoveCallbacks)mContext).onRemoveProduct();
                }
            }
        });
    }
...
holder.itemname.setText(productsList.get(position).getName());
holder.itemprice.setText(productsList.get(position).getPrice());
holder.itemprice_g.setText(productsList.get(position).getPrice_g());

if (productsList.get(position1).isAddedTocart()) {
    holder.button.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
    holder.button.setPadding(0,0,0,0);
    holder.button.setText("Remove");
} else {
    holder.button.setText("Add");
    holder.button.setPadding(12,0,0,0);
    holder.button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_shopping_cart_black_24dp, 0, 0, 0);
}

holder.button.setOnClickListener(new View.OnClickListener() {
...