我采用的是函数类型和lambda表达式的方式来获取和传递点击事件
如下,在Adapter中
override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
super.onBindViewHolder(holder, position)
val m = holder.getView<LinearLayout>(R.id.recyleview_linearLayout)
m.setOnClickListener {
OnRecyleViewLineItemClick?.invoke(position)
}
val o = holder.getView<ImageButton>(R.id.item_operation)
o.setOnClickListener {
OnRecyleViewOperationItemClick?.invoke(position)
}
}
//整体点击事件
var OnRecyleViewOperationItemClick:((position:Int)->Unit)? = null
//指定操作按钮的点击事件
var OnRecyleViewLineItemClick:((position:Int)->Unit)? = null
具体的使用案例
adapter.OnRecyleViewLineItemClick = {position: Int ->
showMsg("OnRecyleViewLineItemClick:$position")
}
adapter.OnRecyleViewOperationItemClick = {position: Int ->
showMsg("OnRecyleViewOperationItemClick:$position")
}
我是一个初学者,个人觉得这种方式比采用interface接口更为方便一些。
网友评论