Android Studio 更新一个particukar项目在recyclerview从片段成功后的回应androidKotlin

vuv7lop3  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(104)

片段回收查看代码:

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()
                    }
                }
            }
    }

基于条件,我在onViewCreated中调用此函数(getSection)

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()
            }
        }
}

片段中接口函数:(此处,我从适配器上取下产品位置)

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)
    }

适配器类:

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)
        }

现在我想在片段中更新吐司(Product Updated Successfully)之后的回收器视图的特定项..如何进行

2ic8powd

2ic8powd1#

1响应后,首先在适配器中找到您要更新的项目位置
2你还必须用你从响应中得到的新的数据列表来更新adapter中的列表。
然后使用以下代码更新适配器中特定项
位置。

adapter.notifyItemChanged(position)

相关问题