[英]Item Onclick RecyclerView Kotlin Android
我在Kotlin中有RecyclerView适配器,当用户单击categoryPhoto时,我想打开一个新活动。 我应该如何实施呢?
class CategoryAdapter(private val categoryList: List<Category>, private val context: Context) : RecyclerView.Adapter<CategoryAdapter.MyViewHolder>() {
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var categoryName = view.text_view_category_name
var categoryPhoto = view.image_view_category
var cardView = view.card_view_category
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = MyViewHolder(parent.inflate(R.layout.category_list_row))
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val category = categoryList[position]
// Set height of cardview based on screen width
val displayMetrics = context.resources.displayMetrics
val finalHeight = displayMetrics.widthPixels / 2
holder.cardView.layoutParams.height = finalHeight
holder.categoryName.text = category.oc
holder.categoryPhoto.loadUrl(category.icon)
}
override fun getItemCount(): Int {
return categoryList.size
}}
只需将click listener作为参数添加到适配器的构造函数即可。
class CategoryAdapter(
private val categoryList: List<Category>,
private val context: Context,
private val onClickListener: (View, Category) -> Unit
) : RecyclerView.Adapter<CategoryAdapter.MyViewHolder>() {
...
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val category = categoryList[position]
// Set height of cardview based on screen width
...
holder.itemView.setOnClickListener { view ->
onClickListener.invoke(view, category)
}
}
...
}
然后您可以使用以下方法:
fun initList() {
adapter = CategoryAdapter(
categoryList = ...,
context = ...,
onClickListener = { view, category -> openActivity(view, category) }
}
关闭顶部。 上面代码的一些可选改进
为lambda创建typealias。 使您的代码更具可读性。
typealias MyCategoryClickListener = (View, Category) -> Unit class CategoryAdapter( private val categoryList: List<Category>, private val context: Context, private val onClickListener: MyCategoryClickListener ) : RecyclerView.Adapter<CategoryAdapter.MyViewHolder>() {
省略对侦听器的invoke
。 可以像函数一样调用Lambda。
holder.itemView.setOnClickListener { view -> onClickListener(view, category) }
创建适配器时用参考替换lambda
fun initList() { adapter = CategoryAdapter( categoryList = ..., context = ..., onClickListener = this::openActivity) } fun openActivity(view: View, category: Category) { ... }
您可以在onBindViewHolder(...)中完成此操作
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val category = categoryList[position]
// Set height of cardview based on screen width
val displayMetrics = context.resources.displayMetrics
val finalHeight = displayMetrics.widthPixels / 2
holder.cardView.layoutParams.height = finalHeight
holder.categoryName.text = category.oc
holder.categoryPhoto.loadUrl(category.icon)
holder.categoryPhoto.setOnClickListener { view ->
// categoryPhoto clicked.
// start your activity here
}
}
像这样
类RecyclerListAdapter:RecyclerView.Adapter {var context:Context? = null var listData:ArrayList? =空
步骤1:活动参考..................................... var activityref:MainActivity?= null
constructor(context: Context?, listData: ArrayList<ItemDetails>?, activityref: MainActivity?) : super() {
this.context = context
this.listData = listData
this.activityref = activityref
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewsHolder {
val view = LayoutInflater.from(context).inflate(R.layout.row_list, parent, false)
return ViewsHolder(view)
}
override fun getItemCount(): Int {
return listData!!.size
}
override fun onBindViewHolder(holder: ViewsHolder?, position: Int) {
holder?.item=listData?.get(position)
holder!!.first!!.setText(holder.item?.First)
holder.second!!.setText(holder.item?.Second)
holder.third!!.setText(holder.item?.Third)
Step 2 OnClick on item.....................
holder.third!!.setOnClickListener{
activityref?.OnItemClicked(holder.item!!)
}
}
class ViewsHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
var item:ItemDetails?=null
var first: TextView? = null;
var second: TextView? = null;
var third: TextView? = null;
init {
first = itemView?.findViewById(R.id.first)
second = itemView?.findViewById(R.id.second)
third = itemView?.findViewById(R.id.third)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.