Kotlin:如何在另一个Recyclerview中单击Recyclerview中的项目

问题描述 投票:1回答:1

看完所有类似的问题后,我找不到我的问题的解决方案,其描述如下:在我的Android应用中,我使用Kotlin作为语言。在我的应用程序中,我有很多类别,每个类别都有一个产品列表。在我的家庭活动中,我创建了一个垂直的RecyclerView,名为“ productListOfCategory”。在“ productListOfCategory”适配器中,一个textView显示一个类别名称,一个recyclerView显示所有相关的产品列表。以下代码是“ ProductListOfProductTypeAdapter”适配器的描述:(ProductType = category)

 class ProductListOfProductTypeAdapter(private val context: Context, private val ProductTypeIDWithProduct: Map<String, Array<ProductData>>, private val productTypeDetail: Array<ProductTypeData>)
    : RecyclerView.Adapter<ProductListOfProductTypeAdapter.ViewHolder>(),ProductOfProductTypeAdapter.OnItemClickListener{
    override fun onItemClick(view: View, viewModel: ProductData) {
        val intent = Intent(context,ProductDetail::class.java)
        context.startActivity(intent)
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_list_product_product_type, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount() = ProductTypeIDWithProduct.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val itemType = ProductTypeIDWithProduct.keys.elementAt(position)
        holder.productTypeName.text = getName(itemType)

        val arrayProductOfProductType = ProductTypeIDWithProduct[itemType] as ArrayList<ProductData>

        holder.productListOfProductType.apply {
            holder.productListOfProductType.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
            holder.productListOfProductType.adapter = ProductOfProductTypeAdapter(arrayProductOfProductType,context,this@ProductListOfProductTypeAdapter)
        }

    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val productTypeName: TextView = itemView.textViewProductTypeName
        val productListOfProductType: RecyclerView = itemView.recyclerViewProductOfProductType
    }

“” ProductOfProductTypeAdapter“是第二个适配器,其代码如下:

class ProductOfProductTypeAdapter(private val products: ArrayList<ProductData>, private val context: Context,private val mListener: OnItemClickListener)
    : RecyclerView.Adapter<ProductOfProductTypeAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_product_featured, parent, false)
        return ViewHolder(view)

    }

    override fun getItemCount() = products.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val productItem = products[position] as Map<String, Map<String, String>>

        holder.productName.text = productItem["name"]?.get("En").toString()
        holder.productWeight.text = productItem["weight"].toString().plus(context.resources.getString(R.string.ml))
        holder.productPrice.text = "$".plus(productItem["price"].toString()).plus("/")
        holder.productRating.text = productItem["reviewsValue"].toString()
        holder.itemView.setOnClickListener {
            mListener.onItemClick(holder.itemView, products[position])
            notifyDataSetChanged()
        }
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val productName: TextView = itemView.itemProdFeaturedName
        val productPrice: TextView = itemView.itemProdFeaturedPrice
        val productImage: ImageView = itemView.itemProdFeaturedImage
        val productWeight: TextView = itemView.txtViewWeight
        val productRating: TextView = itemView.itemProdFeaturedRate
    }

   interface OnItemClickListener {
        fun onItemClick(view: View,viewModel: Map<String, Map<String, String>>)
    }

我的问题是如何单击产品并显示产品详细信息活动。我尝试以下操作,但仍未获得我想要的。

android kotlin android-recyclerview recycler-adapter
1个回答
0
投票

您的OnItemClickListener不会像下面这样吗?

interface OnItemClickListener {
    fun onItemClick(view: View, product: ProductData)
}

并且您需要更改启动ProductDetail活动的方式,并在意图的额外数据中放入您的产品ID或用于标识所选产品的内容。例如:

val intent = Intent(context,ProductDetail::class.java)
intent.putExtra("PRODUCT_ID", product.id)
context.startActivity(intent)
© www.soinside.com 2019 - 2024. All rights reserved.