如何使用 Kotlin 通过数据绑定在 RecyclerView Adapter 中实现 onClick [英] How to implement onClick in RecyclerView Adapter with databinding using Kotlin

查看:34
本文介绍了如何使用 Kotlin 通过数据绑定在 RecyclerView Adapter 中实现 onClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用数据绑定创建了一个 RecyclerView 适配器,我正在尝试处理该适配器内的 Click 事件,但我不知道具体如何处理.我找到了不同的主题,但还不能解决我的问题.谢谢!我应该在哪里(在 Adapter 类中)实现 onClick 函数?

I created a RecyclerView Adapter using databinding and I'm trying to handle Click events inside this adapter but I don't know exactly how. I found different topics but couldn't solve my problem yet. Thanks! Where should I implement (inside the Adapter class) the onClick function?

适配器类

class RemovableContactsAdapter(val viewModel: GroupDetailsViewModel,val callback:GroupContactsCallback) : RecyclerView.Adapter<RemovableContactsViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RemovableContactsViewHolder {
    val binding: ViewDataBinding =
        DataBindingUtil.inflate(LayoutInflater.from(parent.context), viewType, parent, false)
    return RemovableContactsViewHolder(binding)
}

override fun onBindViewHolder(holder: RemovableContactsViewHolder, position: Int) {
    holder.bind(viewModel, callback, position)

}

override fun getItemCount(): Int {
    return viewModel.currentGroup.value?.contacts?.size ?: 0
}

override fun getItemViewType(position: Int): Int {
    return R.layout.removable_contact_layout
}
}

class RemovableContactsViewHolder(val binding: ViewDataBinding) :
RecyclerView.ViewHolder(binding.root) {

fun bind(viewModel: GroupDetailsViewModel, callback: GroupContactsCallback, position: Int) {
    binding.root.selected_contact_iv.apply {
        visibility = if (viewModel.currentGroup.value?.contacts!![position].selected) {
            View.VISIBLE
        } else {
            View.GONE
        }
    }
    binding.setVariable(BR.contact, viewModel.currentGroup.value?.contacts?.get(position))
    binding.setVariable(BR.callback, callback)
    binding.executePendingBindings()
   }
}

Fragment 类中的回调接口:

Callback interface in Fragment class:

interface GroupContactsCallback {
   fun selectContact(contact: ContactModel)
}

编辑

我更新了 RemovableContactsViewHolder 中的绑定函数,但仍然无法正常工作.

I updated the bind function inside RemovableContactsViewHolder but still not working.

class RemovableContactsViewHolder(val binding: ViewDataBinding) :
RecyclerView.ViewHolder(binding.root) {

fun bind(viewModel: GroupDetailsViewModel, callback: GroupContactsCallback, position: Int) {
    itemView.setOnClickListener {
        viewModel.currentGroup.value?.contacts?.get(position)!!.selected = !viewModel.currentGroup.value?.contacts?.get(position)!!.selected
        callback.selectContact(viewModel.currentGroup.value?.contacts?.get(position)!!)
    }

    binding.root.selected_contact_iv.apply {
        visibility = if (viewModel.currentGroup.value?.contacts!![position].selected) {
            View.VISIBLE
        } else {
            View.GONE
        }
    }

    binding.setVariable(BR.contact, viewModel.currentGroup.value?.contacts?.get(position))
    binding.setVariable(BR.callback, callback)
    binding.executePendingBindings()
}

推荐答案

处理适配器内部的点击不是一个好习惯.并且不要仅仅为此使用接口.改用这个.

It is not a good practice to handle click inside your adapter. And don't use interface just for this. Use this instead.

class YourRecyclerViewAdapter(private val onSelect: (YourDataType?) -> Unit) : RecyclerView.Adapter<YourRecyclerViewAdapter.YourViewHolder>() {

    override fun onBindViewHolder(holder: YourViewHolder, position: Int) {
        holder.bind(getItem(position), onSelect)
    }

    class ViewHolder(private val binding: YourViewBinding) : RecyclerView.ViewHolder(binding.root) {

        fun bind(yourDataType: YourDataType?, onSelect: (YourDataType?) -> Unit) {

           // bind your view here
           binding.root.setOnClickListener {
               onSelect(yourDataType)
           }
        }
    }
}

在您的片段/活动中

// Set this adapter to your recycler view
val adapter = YourRecyclerViewAdapter { yourDataType-> 
    // Handle click here
}

这篇关于如何使用 Kotlin 通过数据绑定在 RecyclerView Adapter 中实现 onClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆