Skip to content

Instantly share code, notes, and snippets.

Last active February 9, 2023 05:09
Show Gist options
  • Save grantland/cd70814fe4ac369e3e92 to your computer and use it in GitHub Desktop.
Save grantland/cd70814fe4ac369e3e92 to your computer and use it in GitHub Desktop.
RecyclerView item onClick

RecyclerView item onClick

RecyclerView does not have an OnItemClickListener like it's predecessor, ListView. However, detecting item clicks is pretty simple.

Set an OnClickListener in your ViewHolder creation:

private class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>  {

    public static class ViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener {
        private String mItem;
        private TextView mTextView;

        public ViewHolder(View view) {
            mTextView = (TextView) view;

        public void setItem(String item) {
            mItem = item;

        public void onClick(View view) {
            Log.d(TAG, "onClick " + getPosition() + " " + mItem);

    private String[] mDataset;

    public MyAdapter(String[] dataset) {
        mDataset = dataset;

    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;

    public void onBindViewHolder(ViewHolder holder, int position) {

    public int getItemCount() {
        return mDataset.length;

Add touch feedback to your item layout:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android=""
Copy link

There's a good documentation about these values:

Copy link

You should not couple your ViewHolders and ItemClickListeners. Please see a better solution here.

Copy link

@jacobtabak that exactly is what i think, but how you get the position element was clicked on the RecyclerView? I'm just find answers like above.

Copy link

ghost commented Apr 29, 2015

@jacobtabak how to get click listener on individual views in the row item. The click is intercepted by on touch right??

Copy link

ghost commented Jul 9, 2015

Great post!

Copy link

Please see more simple solution here

Copy link

yosraa commented Jul 30, 2015

how to get startactivity in onclick()

Copy link

dinigo commented Aug 20, 2015

This works on my head, but doesn't run as expected. The onClick() is never triggered, and I can't find why

Copy link

@jacobtabak Read the comment in the solution you linked to:

This will not provide any clue about which button or view (within the item) was clicked.

Copy link

how to delete row in recycler view...
plzz help me

Copy link

how to delete row in recycle view when click on delete than that row is delete..plzzz help me

Copy link

@demil133 after adding android:clickable="true" to the parent element for the item's layout in xml, it started to work. Hope it'll help someone out there.

Copy link

dima925 commented Mar 14, 2016

Good sample. it's well for my app.

Copy link

it works !!!

Copy link

I have a recycler view with event onTap Listener and in item row I have a TextView that have click listener event
in my code OnTap only work. mean after click textview event onTap run and textview event dont work.

please help for solve this problem.


Copy link

where did you add touch feedback to item layout

Copy link

How do you implement onClickListener on GridLayout using the RecyclerView

Copy link

This code doesn't work, but this does work

Copy link

the base class doesn't have any on click method???

Copy link

SkymanOne commented Dec 6, 2016

but can use int item instead of String item and it's easier work with items

Copy link

JSila commented Feb 15, 2017

getPosition is deprecated, use getAdapterPosition instead.

Copy link

thanks, this save me from item clicklistener mess.
Though this is what i use...
int itemPosition = getLayoutPosition();
Products products = productsArrayList.get(itemPosition);
Thanks for the clue

Copy link

👍 👍 👍 👍

Thanks! This is by far the cleanest solution I have seen for this. Helped a lot!

Copy link

Short and concise. Thanks!

Copy link


Copy link

thanks for the code, you saved my lot of time.

Copy link

how to get startactivity in onclick()


Copy link

how to implement onItemClickListener on individual items/icons of viewHolder in a recyclerView and perform actions on them inside fragment/activity, but not inside the adapter

Copy link

You should not couple your ViewHolders and ItemClickListeners. Please see a better solution here.

how is this better? You don't need a gesture detector to handle clicks.

Just set a setOnClickListener(). The original gist is significantly better than what you linked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment