问题描述
感谢您阅读我的问题,
我已经使用dataBinding在recyclerView中实现了onClick,但是我可以将对象本身发送给click函数,但是我需要物品位置,是否有可能在<$ c中获得物品位置$ c> onClick ?
是在recyclerView项目中实现点击的方式:
thank you for reading my question,I've Implemented onClick in recyclerView using dataBinding but I can send the object itself to click function but I need the item position is it possible to get item position in onClick
?here is how click is implemented in the recyclerView item:
android:onClick="@{ (v) -> model.click(obj) }"
推荐答案
您可以在视图持有者中使用 adapterPosition
将项目位置传递给ItemView,然后可以将 itemPosition
传递给 android:onClick = @ {(v)-> model.click(obj,itemPosition)}
You can use adapterPosition
in view holder to pass item position to ItemView and then you can pass itemPosition
to android:onClick="@{ (v) -> model.click(obj, itemPosition) }"
class GeneralHolder<DataModel, ClickListner>(
private val binding: ViewDataBinding,
val listener: ClickListner
) :
RecyclerView.ViewHolder(binding.root) {
fun bind(data: DataModel) {
binding.setVariable(BR.item, data)
binding.setVariable(BR.presenter, listener)
binding.setVariable(BR.itemPosition, adapterPosition)
binding.executePendingBindings()
}
}
这篇关于使用dataBinding获取被单击项在recyclerView中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!