如何在 RecyclerView kotlin 的两个不同视图上实现两个 OnClickListener?

Posted

技术标签:

【中文标题】如何在 RecyclerView kotlin 的两个不同视图上实现两个 OnClickListener?【英文标题】:How to implement two OnClickListener on two different views on RecyclerView kotlin? 【发布时间】:2021-08-27 23:28:28 【问题描述】:

我正在处理一个包含两个视图的 RecyclerView 项目:一个 TextView 和一个 ImagaView。我希望能够同时点击它们来执行不同的功能。

我该怎么办

这是我的适配器


class AddMeasurementAdapter(private val currentList:MutableList<DressMeasurementModel>, private val listener1: RecyclerClickListener, private val listener2: RecyclerClickListener): RecyclerView.Adapter<AddMeasurementAdapter.CardViewHolder>() 

    //inner class
    inner class CardViewHolder (itemView : View):RecyclerView.ViewHolder(itemView), View.OnClickListener
        val display:TextView = itemView.findViewById(R.id.measurement_recyclerview_item)
        //Binding the data with the view
        fun  bind(dressMeasurementModel: DressMeasurementModel)
            display.text = "$dressMeasurementModel.measurementName $dressMeasurementModel.measurement"

        

        init 
            itemView.setOnClickListener(this@CardViewHolder)
        

        override fun onClick(v: View?) 
            val position: Int = adapterPosition
            if (position != RecyclerView.NO_POSITION) 
                listener1.onItemClick1(position, currentList)

            

            if (position != RecyclerView.NO_POSITION) 
                listener2.onItemClick2(position, currentList)

            

        


    


    //Creating view
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewHolder 
        val view = LayoutInflater.from(parent.context).inflate(R.layout.measurement_fragment_recyclerview_items, parent, false)

        return CardViewHolder(view)
    
    //Binding the view
    override fun onBindViewHolder(holder: CardViewHolder, position: Int) 
        holder.bind(currentList[position])
    
    //Getting the item cout size
    override fun getItemCount(): Int 
        return currentList.size
    



这是我的界面

interface RecyclerClickListener 
    fun onItemClick1(position: Int, currentList: MutableList<DressMeasurementModel>)
    fun onItemClick2(position: Int, currentList: MutableList<DressMeasurementModel>)

【问题讨论】:

这能回答你的问题吗? How to handle multiple layout clicks in recyclerView in android 我在 java 方面不是很好,我正在使用 kotlin @Anand 【参考方案1】:

可以在createViewHolder中添加监听器如

//Creating view
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewHolder 
        val view = LayoutInflater.from(parent.context).inflate(R.layout.measurement_fragment_recyclerview_items, parent, false)
        view.textView.onClick
        view.image.onClick
        return CardViewHolder(view)

【讨论】:

【参考方案2】:

好的,这是Kotlin 版本的答案。

在你的 bind 方法中,

fun bind(dressMeasurementModel: DressMeasurementModel) 
    ...
    var position = adapterPosition

    display.setOnClickListener 
        listener.onTextClick(position)
    

    image.setOnClickListener 
        listener.onImageClick(position)
    
    ...

【讨论】:

【参考方案3】:

我后来想通了,所以我想分享一下我是怎么做到的。

class AddMeasurementAdapter(
    private val currentList: MutableList<DressMeasurementModel>,
    private val listener1: RecyclerClickListener,
    private val listener2: RecyclerClickListener
) : RecyclerView.Adapter<AddMeasurementAdapter.CardViewHolder>() 

    // inner class
    inner class CardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) 
        val display: TextView = itemView.findViewById(R.id.measurement_recyclerview_item)
        val delete: ImageView = itemView.findViewById(R.id.measurementment_recyclerview_item_delete_button)
//        Binding the data with the view
        fun bind(dressMeasurementModel: DressMeasurementModel) 
            display.text = "$dressMeasurementModel.measurementName $dressMeasurementModel.measurement"
        
    

    // Creating view
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewHolder 
        val view = LayoutInflater.from(parent.context).inflate(
            R.layout.measurement_fragment_recyclerview_items,
            parent,
            false
        )

        return CardViewHolder(view)
    
    // Binding the view and attaching the listener
    override fun onBindViewHolder(holder: CardViewHolder, position: Int) 
        holder.bind(currentList[position])
        
        holder.display.setOnClickListener 
            listener1.onItemClickToEdit(holder.adapterPosition, currentList)
        

        holder.delete.setOnClickListener 
            listener2.onItemClickToDelete(holder.adapterPosition, currentList)
        
    
    // Getting the item cout size
    override fun getItemCount(): Int 
        return currentList.size
    


当我在fun bind() 中分配 onClickListener 时,监听器覆盖了整个 itemView 布局,而不是布局内的单个视图,因此只实现了一次单击监听器。在内部类中也应用了同样的东西。

界面如下

interface RecyclerClickListener 
    fun onItemClickToEdit(position: Int, currentList: MutableList<DressMeasurementModel>)
    fun onItemClickToDelete(position: Int, currentList: MutableList<DressMeasurementModel>)


通过在片段的任一活动中扩展接口,您就有了 onclickListener。

本文帮助https://antonioleiva.com/recyclerview-listener/

【讨论】:

以上是关于如何在 RecyclerView kotlin 的两个不同视图上实现两个 OnClickListener?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Kotlin 中将 listView 转换为 RecyclerView

如何在 Kotlin 中为 Android 的 RecyclerView 适配器初始化 viewHolder

如何使用kotlin滑动删除存储在firestore中的recyclerview项目?

如何在 Kotlin 中使用与 Activity 和 Fragment 相同的 RecyclerView 适配器?

如何将数据从 RecyclerView 传递到 Kotlin 中的 DialogFragment?

如何使用 Kotlin 在 RecyclerView Adapter 中实现 onClick 并进行数据绑定