androidkotlinandroid-recyclerviewonitemlongclicklistener

how to implement longClickListen on RecyclerView in Kotlin


After searching for a solution for hours, I could implement ItemClickListener but for some reason, I cannot use the same method to implement itomLongClickListener. it gives me "Type Mismatch" error: Type mismatch. Required:Boolean Found:Unit

I would really appreciate if you could pinpoint what I am doing wrong.

Below is my code

class MyAdapter(ct: Context, s1: Array<String>, s2: Array<String>?):RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
    var nameList: Array<String>? = null
    var oneLineMessage: Array<String>? = null
    var context: Context? = null

    init {
        nameList = s1
        oneLineMessage = s2
        context = ct

    }

    fun onClick(view :View){
        val intent= Intent(context,SecondPage::class.java)
        context?.startActivity(intent)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        var inflater = LayoutInflater.from(context)
        var v = inflater.inflate(R.layout.my_row, parent, false)
        return MyViewHolder(v)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.friendName?.text = nameList?.get(position)
        if (oneLineMessage != null) {
            holder.messagePreview?.text = oneLineMessage?.get(position)
        } else {
            holder.messagePreview?.visibility = View.GONE
        }
        holder.layout?.setOnClickListener(View.OnClickListener {
            val intent= Intent(context,SecondPage::class.java)
            context?.startActivity(intent)
        })
    }

    override fun getItemCount(): Int {
        return nameList?.size as Int
    }

    inner class MyViewHolder constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var friendName: TextView? = itemView.findViewById(R.id.nameText)
        var messagePreview: TextView? = itemView.findViewById(R.id.messagePreview)
        var layout: RelativeLayout? = itemView.findViewById(R.id.secondPageConstraintLayout)

        init {
            
            itemView.setOnClickListener{
                val position=adapterPosition+1
                Toast.makeText(context, "you chose $position", Toast.LENGTH_SHORT).show()
            }
            
            itemView.setOnLongClickListener(View.OnLongClickListener {
                val position=adapterPosition+1
                Toast.makeText(context, "you long pressed $position", Toast.LENGTH_SHORT).show()  })
        }


    }
}

Solution

  • View.OnLongClickListner is an Interface with boolean onLongClick(View v), that's mean you must return boolean.

    return true if the callback consumed the long click, false otherwise.

    show() returns Unit and that's your last line in your lambda {}

    so if you consumed the long click on your itemView just return true

    itemView.setOnLongClickListener {
        val position=adapterPosition+1
        Toast.makeText(context, "you long pressed $position", Toast.LENGTH_SHORT).show()
        true
    }