Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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) {
            super(view);
            view.setOnClickListener(this);
            mTextView = (TextView) view;
        }

        public void setItem(String item) {
            mItem = item;
            mTextView.setText(item);
        }

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

    private String[] mDataset;

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

    @Override
    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;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.setItem(mDataset[position]);
    }

    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

Add touch feedback to your item layout:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:background="?android:attr/selectableItemBackground"
/>
@matthewmichihara

This comment has been minimized.

Copy link

commented Jul 3, 2014

how do you find out about all these ?android:attr values? I never know what exists

@alexnavratil

This comment has been minimized.

Copy link

commented Jul 3, 2014

There's a good documentation about these values: http://developer.android.com/reference/android/R.attr.html

@jacobtabak

This comment has been minimized.

Copy link

commented Oct 4, 2014

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

@AmauryEsparza

This comment has been minimized.

Copy link

commented Feb 16, 2015

@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.

@lnikkila

This comment has been minimized.

Copy link

commented Mar 9, 2015

@Raghunandan123

This comment has been minimized.

Copy link

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??

@R3DB4R0N

This comment has been minimized.

Copy link

commented Jul 9, 2015

Great post!

@riyazMuhammad

This comment has been minimized.

Copy link

commented Jul 22, 2015

Please see more simple solution here
https://gist.github.com/riyazMuhammad/1c7b1f9fa3065aa5a46f

@yosraa

This comment has been minimized.

Copy link

commented Jul 30, 2015

how to get startactivity in onclick()

@dinigo

This comment has been minimized.

Copy link

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

@IgorGanapolsky

This comment has been minimized.

Copy link

commented Oct 2, 2015

@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.

@dinesh-gadri

This comment has been minimized.

Copy link

commented Nov 19, 2015

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

@dinesh-gadri

This comment has been minimized.

Copy link

commented Nov 19, 2015

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

@mayooresan

This comment has been minimized.

Copy link

commented Jan 6, 2016

@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.

@dima925

This comment has been minimized.

Copy link

commented Mar 14, 2016

Good sample. it's well for my app.

@shubham171294

This comment has been minimized.

Copy link

commented May 1, 2016

it works !!!

@MansoorJafari

This comment has been minimized.

Copy link

commented Jun 29, 2016

Hi
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.

Thanks.

@pradeepkumarreddyk

This comment has been minimized.

Copy link

commented Jul 12, 2016

where did you add touch feedback to item layout

@EliudNjuguna

This comment has been minimized.

Copy link

commented Sep 17, 2016

How do you implement onClickListener on GridLayout using the RecyclerView

@devmike01

This comment has been minimized.

Copy link

commented Oct 17, 2016

This code doesn't work, but this does work

@MinaGabriel

This comment has been minimized.

Copy link

commented Nov 18, 2016

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

@SkymanOne

This comment has been minimized.

Copy link

commented Dec 6, 2016

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

@JSila

This comment has been minimized.

Copy link

commented Feb 15, 2017

getPosition is deprecated, use getAdapterPosition instead.

@ayetolusamuel

This comment has been minimized.

Copy link

commented Apr 11, 2018

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

@ax-vasquez

This comment has been minimized.

Copy link

commented Jul 20, 2018

👍 👍 👍 👍

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

@deweysia

This comment has been minimized.

Copy link

commented Nov 11, 2018

Short and concise. Thanks!

@imrankhanissm

This comment has been minimized.

Copy link

commented Feb 19, 2019

Thanks

@parthdesai1208

This comment has been minimized.

Copy link

commented May 27, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.