繁体   English   中英

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

[英]Kotlin : How to click on item in a Recyclerview Inside another Recyclerview

在看过所有类似的问题后,我没有找到我的问题的解决方案,描述如下: 在我的 android 应用程序中,我使用 Kotlin 作为语言。 在我的应用程序中,我有很多类别,每个类别都有一个产品列表。 在我的家庭活动中,我创建了一个名为“productListOfCategory”的垂直 RecyclerView。 在“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>>)
    }

我的问题是如何点击产品并显示产品详细信息活动。我尝试如下但仍然没有得到我想要的。

你的 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM