Fragment Recyclerview code :
private fun initRecyclerView() {
bestSellersAdapter = BestSellerProductsAdapter(frag)
homeBinding.popularproductsRView.apply {
bestSellersAdapter.second(this@HomeFragment)
adapter = bestSellersAdapter
}
}
private fun getSection() {
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.Main) {
roomProductVM.productFlow.collectLatest {
if (it.isEmpty()) {
categoryViewModel.getAllCategories()
} else {
bestSellersAdapter.setMutableArraylist(it)
bestSellersAdapter.notifyDataSetChanged()
}
}
}
}
based on condition i call this function(getSection) in onViewCreated
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initRecyclerView()
getSection()
addcartviewModel.cartTableSuccessorNot.observe(viewLifecycleOwner) {
if(it.toInt() == variant_id) {
Toast.makeText(context,"Product Updated Successfully",Toast.LENGTH_SHORT).show() homeBinding.popularproductsRView.adapter?.notifyItemChanged(productPosition)
} else {
LoadingUtils.hideDialog()
Toast.makeText(context,"Failure in Cart Table", Toast.LENGTH_SHORT).show()
}
}
}
interface function in fragment : ( here i am taking product position from adapter )
override fun updatepro(position: Int, type: String, id: Int, cart_count: Int, is_notify: Boolean, product_id: Int, name: String, measurement: String,
variant_image: String, price: String, qty: String, stock: String, image: String, moq: String, unit: String) {
productPosition = position
variant_id = id
variant_qty = qty.toInt()
addcartviewModel.updateVariantTableinRoom(id,cart_count.toString(),is_notify,product_id)
addcartviewModel.insertVariantListintoCartTable(id,measurement,price,stock,image,moq,name,variant_image,unit,qty)
}
Adapter class :
class BestSellerProductsAdapter(var context: Context) : RecyclerView.Adapter<BestSellerProductsAdapter.BestSellerViewHolder>() {
var mutableList = mutableListOf<ProductWithVariants>()
fun setMutableArraylist(datas: MutableList<ProductWithVariants>) {
mutableList.clear()
mutableList.addAll(datas)
}
override fun onBindViewHolder(holder: BestSellerViewHolder, position: Int) {
var cart_count = mutableList[position].variantsList[0].cart_count
if(stock?.toInt()==0) {
holder.notify.visibility = View.VISIBLE
holder.addtocart.visibility = View.GONE
holder.pll_cart.visibility = View.GONE
} else if(cart_count == 0) {
holder.addtocart.visibility = View.VISIBLE
holder.pll_cart.visibility = View.GONE
holder.notify.visibility = View.GONE
} else {
holder.notify.visibility = View.GONE
holder.addtocart.visibility = View.GONE
holder.pll_cart.visibility = View.VISIBLE
holder.tv_cart.text = cart_count.toString()
}
holder.addtocart.setOnClickListener {
cart_count = ((cart_count ?: 0) + 1)
selectpro?.updatepro(position,"AddCart",mutableList[position].variantsList[0].id,mutableList[position].variantsList[0].cart_count!!.toInt(),false,mutableList[position].products.id,
mutableList[position].products.name.toString(),
mutableList[position].variantsList[0].measurement.toString() + mutableList[position].variantsList[0].measurement_unit_name.toString(),
mutableList[position].variantsList[0].image.toString(),
mutableList[position].variantsList[0].discounted_price.toString(),
cart_count.toString(),
stock.toString(),
mutableList[position].variantsList[0].image.toString(),
moq.toString(),
mutableList[position].variantsList[0].measurement_unit_name.toString()
)
}
var selectpro: selectproduct?=null
fun second(selectproduct: selectproduct) {
selectpro = selectproduct
}
interface selectproduct {
fun updatepro(position:Int,type:String,id:Int, cart_count:Int,is_notify:Boolean,product_id:Int,
name:String,measurement:String,variant_image:String,price:String,qty:String,
stock:String,image:String,moq:String,unit:String)
fun selectProduct(product: ProductWithVariants,position:Int)
}
Now i want to update the particular item of recycler view after Toast ( Product Updated Successfully ) in fragment .. how to do that
1 After response first find out the item position you want to update in adapter
2 Also you have to update the list in adapter with new list of data you get from the response.
then use below code to update particular item in adapter using
position.