question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Item Click Support

See original GitHub issue

How do you add click listener to a view in a ViewHolder?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
lisawraycommented, May 24, 2018

Yes, exactly. Thanks @ayonymus 😃

If you want a click listener on the root view of every Item in your adapter, Groupie can do that for you by calling GroupAdapter.setOnClickListener().

If I want a click listener that I set myself (usually on some part of an item and not the whole view), I usually create a meaningful interface, and pass it in the item’s constructor. e.g.:

class ProductItem(val product: Product, val heartListener: OnHeartClickedListener) {
    interface OnHeartClickedListener {
        fun onHeartClicked(item: Item, product: Product)
    }

    override fun bind(viewHolder: ViewHolder, position: Int) {
        viewHolder.itemView.heart.setOnClickListener {
            heartListener.onHeartClicked(this@ProductItem, product)
        }
    }
}
3reactions
vincent-paingcommented, Feb 4, 2020

You would do like this in kotlin:

override fun bind(viewHolder: ViewHolder, position: Int) {
    viewHolder.itemView.setOnClickListener {
        ( your on click logic)
    }
}

That is not really a efficient way to execute, We should be not be recycling OnClickListener. Instead, pass a single listener object into the ViewHolder, and it should return with adapterPosition to the adapter. Currently, we have to override createViewHolder to make it really efficient.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding item click support to a RecyclerView. - gists · GitHub
A class that adds item click support to a {@link RecyclerView}. *. * @author Hugo Visser ... ItemClickSupport support = (ItemClickSupport) view.
Read more >
Item click listeners for RecyclerView - Stack Overflow
I have been trying to work on RecycleView item click listener. I have come up with following solution.
Read more >
RecyclerView Item Click Listener the Right Way - Medium
What we want is that when an item is clicked, we get the item's model(User) information and may be pass it to a...
Read more >
RecyclerView item Click using Interface | Advanced Way
In this video you will learn how to create an onClick Listener using an Interface. This is the best practice of implementing an...
Read more >
Element: click event - Web APIs | MDN
An element receives a click event when a pointing device button (such as a mouse's primary mouse button) is both pressed and released...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found