Search code examples
androidkotlinandroid-recyclerviewonclicklisteneronitemclicklistener

How to set listener for two diferent events on recyclerView?


I am trying to implement two different listeners for the cardview on my recyclerview adapter. I think I have to use the element id in order to handle the event, but I do not know quite how to do it. This is my adapter view holder in which I call one of the listeners and which works just fine.

class ProductListViewHolder(itemView: View, listener: OnItemClickListener ) : RecyclerView.ViewHolder(itemView) {

    var lytProductInfo: LinearLayout? = null
    var btnAddProduct: Button? = null

    init {
        
        lytProductInfo = itemView.findViewById(R.id.lytProductInfo)
        btnAddProduct = itemView.findViewById(R.id.btnAddProduct)

        lytProductInfo!!.setOnClickListener(View.OnClickListener {
            if (itemView.hasFocus()) {
                itemView.clearFocus()
                //hide keyboard when item has been clicked
                if (itemView is EditText) {
                    val imm = itemView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                    imm.hideSoftInputFromWindow(itemView.getWindowToken(), 0)
                }
            }
            val position = adapterPosition
            if (position != RecyclerView.NO_POSITION) {
                //click function
                listener.onClick(position)
            }
        })
    }
}

and this is how I call it from the class

productListAdapter!!.setOnItemClickListener( object: ProductListAdapter.OnItemClickListener {
            override fun onClick(position: Int) {
                val saleId = productList[position].idProduct
            }

        override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
            val saleId = productList[position].idProduct
        }
    })

The second listener would come from the btnAddProduct.

How can I do this?


Solution

  • implement this on your adapter class

    interface OnItemClickListener : AdapterView.OnItemClickListener {
            fun onInformationClicked(position: Int, v: View?)
            fun onAddClicked(position: Int, v: View?)
        }
    

    Then on your viewholder

    lytProductInfo!!.setOnClickListener(View.OnClickListener { v ->
                    val position = adapterPosition
                    if (position != RecyclerView.NO_POSITION) {
                        listener.onInformationClicked(position, v)
                    }
                })
    
            btnAddProduct!!.setOnClickListener(View.OnClickListener { v ->
                val position = adapterPosition
                if (position != RecyclerView.NO_POSITION) {
                    listener.onAddClicked(position, v)
                }
            })
    

    Then on your parent class

    productListAdapter!!.setOnItemClickListener( object: ProductListAdapter.OnItemClickListener {
                
            override fun onInformationClicked(position: Int, v: View?) {
                showProductInfo()
            }
    
            override fun onAddClicked(position: Int, v: View?) {
                addProduct()
            }
        })