[英]Kotlin : How to click on item in a Recyclerview Inside another Recyclerview
After seen all the similiar questions, I don't find a solution of my problem, which the description as follows: In my android app, I use Kotlin as language.在看过所有类似的问题后,我没有找到我的问题的解决方案,描述如下: 在我的 android 应用程序中,我使用 Kotlin 作为语言。 In my app, I have a lot of category and each category have a list of products.
在我的应用程序中,我有很多类别,每个类别都有一个产品列表。 In my home activity, I create a vertical RecyclerView named "productListOfCategory".
在我的家庭活动中,我创建了一个名为“productListOfCategory”的垂直 RecyclerView。 In the "productListOfCategory" adapter,a textView to display a category name and a recyclerView to display all related product list.
在“productListOfCategory”适配器中,显示类别名称的 textView 和显示所有相关产品列表的 recyclerView。 The following code is the description of "ProductListOfProductTypeAdapter" adapter : (ProductType = category)
以下代码是“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" is the second adapter which the code as the following : “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>>)
}
My problem is How to click on product and dispaly the product detail activity.I try as the following but still not get what I want.我的问题是如何点击产品并显示产品详细信息活动。我尝试如下但仍然没有得到我想要的。
Shouldn't your OnItemClickListener be like the following?你的 OnItemClickListener 不应该像下面这样吗?
interface OnItemClickListener {
fun onItemClick(view: View, product: ProductData)
}
And you need to change the way you start your ProductDetail activity, and put your product ID or something to identify the product selected in the extra data of the intent.并且您需要更改启动 ProductDetail 活动的方式,并在意图的额外数据中放入您的产品 ID 或其他内容来标识所选产品。 For example:
例如:
val intent = Intent(context,ProductDetail::class.java)
intent.putExtra("PRODUCT_ID", product.id)
context.startActivity(intent)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.