Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Kotlin:如何在另一个Recyclerview中单击Recyclerview中的项目_Android_Kotlin_Android Recyclerview - Fatal编程技术网

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

Android Kotlin:如何在另一个Recyclerview中单击Recyclerview中的项目,android,kotlin,android-recyclerview,Android,Kotlin,Android Recyclerview,在看到所有类似的问题后,我没有找到问题的解决方案,其描述如下: 在我的android应用程序中,我使用Kotlin作为语言。在我的应用程序中,我有很多类别,每个类别都有一个产品列表。在我的家庭活动中,我创建了一个名为“productListOfCategory”的垂直回收视图。在“productListOfCategory”适配器中,显示类别名称的文本视图和显示所有相关产品列表的回收视图。以下代码是“ProductListOfProductTypeAdapter”适配器的说明:(ProductT

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

ProductTypeAdapter的类ProductListOfProductTypeAdapter(私有val上下文:上下文,私有val ProductTypeIDWithProduct:Map,私有val productTypeDetail:Array)
:RecyclerView.Adapter(),ProductOfProductTypeAdapter.OnItemClickListener{
重写MClick(视图:视图,视图模型:ProductData){
val intent=intent(上下文,ProductDetail::class.java)
上下文。起始触觉(意图)
}
override onCreateViewHolder(父级:ViewGroup,viewType:Int):ViewHolder{
val view=LayoutInflater.from(parent.context).充气(R.layout.item\u list\u product\u type,parent,false)
返回视图保持器(视图)
}
重写getItemCount()=ProductTypeIDWithProduct.size
覆盖BindViewHolder(holder:ViewHolder,位置:Int){
val itemType=ProductTypeIDWithProduct.keys.elementAt(位置)
holder.productTypeName.text=getName(itemType)
val arrayProductOfProductType=ProductTypeIDWithProduct[itemType]作为ArrayList
holder.productListOfProductType.apply{
holder.productListOfProductType.layoutManager=LinearLayoutManager(上下文,RecyclerView.HORIZONTAL,false)
holder.productListOfProductType.adapter=ProductOfProductTypeAdapter(arrayProductOfProductType,上下文,this@ProductListOfProductTypeAdapter)
}
}
内部类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>>)
    }
ProductTypeAdapter类产品(私有val产品:ArrayList,私有val上下文:context,私有val mListener:OnItemClickListener)
:RecyclerView.Adapter(){
override onCreateViewHolder(父级:ViewGroup,viewType:Int):ViewHolder{
val view=LayoutInflater.from(parent.context)。充气(R.layout.item\u product\u characterized,parent,false)
返回视图保持器(视图)
}
覆盖getItemCount()=products.size
覆盖BindViewHolder(holder:ViewHolder,位置:Int){
val productItem=产品[位置]作为地图
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[“ReviewValue”].toString()
holder.itemView.setOnClickListener{
mListener.onItemClick(holder.itemView,产品[位置])
notifyDataSetChanged()
}
}
内部类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, product: ProductData)
}
您需要更改启动ProductDetail活动的方式,并在意向的额外数据中添加产品ID或其他标识来标识所选产品。例如:

val intent = Intent(context,ProductDetail::class.java)
intent.putExtra("PRODUCT_ID", product.id)
context.startActivity(intent)

你的听众不应该像下面这样吗

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)

是的,它对我有效,我只是将“产品:产品数据”更改为“产品ID:String”是的,它对我有效,我只是将“产品:产品数据”更改为“产品ID:String”