Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
    }
}
like image 672
jenik2205 Avatar asked Nov 05 '25 16:11

jenik2205


1 Answers

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

like image 151
Zea Avatar answered Nov 07 '25 06:11

Zea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!