When i clicked on add to cart button then i will show another product clicked also. I want to set add to cart button and after increase and decrease button visibility will be show.
Here is my adapter code.
''' public class PrdouctByCategoryAdapter extends RecyclerView.Adapter<PrdouctByCategoryAdapter.ViewHolder> {
private Context mContext;
List<ProductByCategoryModel> productByCategoryModelList;
int quantity1 = 0;
private ProgressDialog pDialog;
CustomSharedPrefs objCustomSharedPrefs;
public PrdouctByCategoryAdapter(Context mContext, List<ProductByCategoryModel> productByCategoryModelList) {
this.mContext = mContext;
this.productByCategoryModelList= productByCategoryModelList;
}
@NonNull
@Override
public PrdouctByCategoryAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.slide_main_item, parent, false);
return new ViewHolder(view);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
pDialog = CommonMethod.getProgressDialog(mContext);
objCustomSharedPrefs = new CustomSharedPrefs(mContext);
ProductByCategoryModel productByCategoryModel = productByCategoryModelList.get(position);
holder.qntyAmount.setText(productByCategoryModelList.get(position).getProduct_qty() + productByCategoryModelList.get(position).getProductUnit());
holder.productPrice.setText("Rs. " + String.valueOf(productByCategoryModelList.get(position).getProduct_mrp_price()) + " - ");
holder.productName.setText(productByCategoryModelList.get(position).getProduct_name());
Picasso.get().load(productByCategoryModelList.get(position).getMobile_icon()).into(holder.productImage);
int priceMain = productByCategoryModelList.get(position).getProduct_mrp_price();
holder.hinditextVeiw.setText("( " + productByCategoryModelList.get(position).getProduct_name_hindi() + " )");
holder.addtocartbtn.setOnClickListener(v -> {
if (mContext instanceof ViewProduct) {
if (Validation.validateString(productByCategoryModelList.get(position).getProduct_id())) {
((ViewProduct) mContext).addtoCartCall(productByCategoryModelList.get(position).getProduct_id(),
productByCategoryModelList.get(position).getCategory_id(),
productByCategoryModelList.get(position).getProduct_qty(),
priceMain,
productByCategoryModelList.get(position).getMember_id(),
productByCategoryModelList.get(position).getSubcategoryName());
holder.addtocartbtn.setVisibility(View.GONE);
holder.add_subs.setVisibility(View.VISIBLE);
holder.totalPrice.setText("₹" + String.valueOf(productByCategoryModel.getProduct_mrp_price()));
notifyDataSetChanged();
}
}
});
holder.plus.setOnClickListener(v -> {
if (mContext instanceof ViewProduct) {
try {
quantity1 = quantity1 + 1;
int productPrice = productByCategoryModelList.get(position).getProduct_mrp_price() * quantity1;
int valueQNty = productByCategoryModelList.get(position).getProduct_qty() * quantity1;
holder.quantity.setText(String.valueOf(quantity1));
holder.totalPrice.setText("₹" + String.valueOf(productPrice));
if (Validation.validateString(productByCategoryModelList.get(position).getProduct_id())) {
((ViewProduct) mContext).increaseQntyCall(productByCategoryModelList.get(position).getProduct_id(),
productByCategoryModelList.get(position).getCategory_id(),
String.valueOf(valueQNty),
productPrice,
productByCategoryModelList.get(position).getMember_id(),
productByCategoryModelList.get(position).getSubcategoryName());
notifyDataSetChanged();
}
} catch (Exception e) {
}
}
});
holder.minus.setOnClickListener(v -> {
try {
if (quantity1 != 1) {
if (mContext instanceof ViewProduct) {
quantity1 = quantity1 - 1;
int productPrice = productByCategoryModelList.get(position).getProduct_mrp_price() * quantity1;
int valueQNty = productByCategoryModelList.get(position).getProduct_qty() * quantity1;
holder.quantity.setText(String.valueOf(quantity1));
holder.totalPrice.setText("₹" + String.valueOf(productPrice));
if (Validation.validateString(productByCategoryModelList.get(position).getProduct_id())) {
((ViewProduct) mContext).increaseQntyCall(productByCategoryModelList.get(position).getProduct_id(),
productByCategoryModelList.get(position).getCategory_id(),
String.valueOf(valueQNty),
productPrice,
productByCategoryModelList.get(position).getMember_id(),
productByCategoryModelList.get(position).getSubcategoryName());
}
}
} else {
holder.addtocartbtn.setVisibility(View.VISIBLE);
holder.add_subs.setVisibility(View.GONE);
}
} catch (Exception e) {
}
notifyDataSetChanged();
});
}
@Override
public int getItemCount() {
return productByCategoryModelList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
ImageView productImage, favourate, plus, minus;
TextView productName;
TextView productPrice;
TextView qntyAmount, quantity, totalPrice;
Button addtocartbtn;
TextView hinditextVeiw;
LinearLayout add_subs;
public ViewHolder(@NonNull View itemView) {
super(itemView);
productImage = itemView.findViewById(R.id.prodcutMainImage);
productName = itemView.findViewById(R.id.product_name_main_item);
productPrice = itemView.findViewById(R.id.pricePerqunaty);
qntyAmount = itemView.findViewById(R.id.peramountQunty);
//favourate = itemView.findViewById(R.id.img_favroute);
addtocartbtn = itemView.findViewById(R.id.button_add_toCart1);
plus = itemView.findViewById(R.id.plus1);
minus = itemView.findViewById(R.id.minus1);
quantity = itemView.findViewById(R.id.quantity23);
add_subs = itemView.findViewById(R.id.add_subs1);
totalPrice = itemView.findViewById(R.id.totalPrice);
hinditextVeiw= itemView.findViewById(R.id.hindiStringProdcut);
}
}
}
'''
You can achieve this using interface easily. First you need to pass the interface reference in your adapter class like below
class SampleAdapter(
private val mContext: Context,
private val sampleModelList: ArrayList<SampleModel>,
private val onItemClickListener: AddItemToCartClickListener) : RecyclerView.Adapter<SampleAdapter.MyViewHolder>() {****
After that you need to create the interface in your adapter class:
interface AddToCartListener {
fun onAddItemToCartClick(
position: Int,
view: View,
itemModel: SampleModel
)
}
You can call the interface in your button's click event like
holder.itemView.setOnClickListener { view ->
onItemClickListener.onAddItemToCartClick(
position,
view,
itemModel
)
}
Lastly just need to implement the interface in your Activity class and you will get the single item value. Now you can create multiple number of interface and can do whatever your need.