Search code examples
androidkotlinandroid-recyclerviewonclicklisteneronlongclicklistener

How to provide long click listener in Kotlin in Adapter


How can I implement long click listener in Adapter? I have already implemented onClickLister by interface. But I don't know how to implement long click listener.

This is Adapter

class DokladAdapter(private val listener: OnItemClickListener): ListAdapter<DokladTuple, DokladAdapter.PolozkaViewHolder>(DiffCallback()) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PolozkaViewHolder {
        val binding = DokladyItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return PolozkaViewHolder(binding)
    }

    override fun onBindViewHolder(holder: PolozkaViewHolder, position: Int) {
        val currentItem = getItem(position)
        if (currentItem != null) {
            holder.bind(currentItem)
        }
    }

    inner class PolozkaViewHolder(private val binding: DokladyItemBinding): RecyclerView.ViewHolder(binding.root) {
        init {
            binding.root.setOnClickListener{
                val position = bindingAdapterPosition
                if (position != RecyclerView.NO_POSITION){
                    val item = getItem(position)
                    if (item != null){
                        listener.onItemClick(item)
                    }
                }
            }
        }
        fun bind(polozkaHlavicka: DokladTuple){
            binding.apply {
                tvU.text = "U"
                tvDOKL.text = polozkaHlavicka.doklad.toString()
                //tvODB.text = "200"
                tvORG.text = polozkaHlavicka.odj.toString()
                tvDATUM.text = polozkaHlavicka.datuct.toString()
            }
        }
    }
    interface OnItemClickListener{
        fun onItemClick(polozkaHlavicka: DokladTuple)
    }

    class DiffCallback: DiffUtil.ItemCallback<DokladTuple>(){
        override fun areItemsTheSame(oldItem: DokladTuple, newItem: DokladTuple) =
            oldItem.doklad == newItem.doklad

        override fun areContentsTheSame(oldItem: DokladTuple, newItem: DokladTuple) =
            oldItem == newItem
    }
}

I have override function in Activity

class Activity: AppCompatActivity(), PolozkaAdapter.OnItemClickListener{
override fun onItemClick(polozkaDoklad: PolozkaTuple) {
        //TODO - do something
    }
}

Solution

  • using Kotlin , i do it like that without adding listeners interfaces , i found it more readable an easy to use, here is my example and you can adjust it to your adapter and objects for this example i have a Cycle object it's costume you can change it with yours i declare two variables

    var onItemClick: ((Cycle) -> Unit)? = null
    var onItemLongClick: ((Cycle) -> Unit)?= null
    

    then in the view holder

    init {
                itemView.setOnClickListener{
                    onItemClick?.invoke(cycleList[adapterPosition])
                }
            itemView.setOnLongClickListener {
                onItemLongClick?.invoke(cycleList[adapterPosition])
                return@setOnLongClickListener true
            }
    
        }
    

    and i use it like that anywhere :

        cycleAdapter= CycleAdapter(cycleList)
        binding.cyclesRecyclerView.adapter=cycleAdapter
        cycleAdapter.onItemClick={
           Toast.makeText( requireContext(),it.cycleName, Toast.LENGTH_SHORT).show()
        }
        cycleAdapter.onItemLongClick={
          Toast.makeText( requireContext(),it.cycleName, Toast.LENGTH_SHORT).show()
        }
    

    "it" here stand for the clicked item object for me it's the clicked Cycle