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"
/>
@pradeepkumarreddyk
Copy link

pradeepkumarreddyk commented Jul 12, 2016

where did you add touch feedback to item layout

@EliudNjuguna
Copy link

EliudNjuguna commented Sep 17, 2016

How do you implement onClickListener on GridLayout using the RecyclerView

@devmike01
Copy link

devmike01 commented Oct 17, 2016

This code doesn't work, but this does work

@MinaGabriel
Copy link

MinaGabriel commented Nov 18, 2016

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

@SkymanOne
Copy link

SkymanOne commented Dec 6, 2016

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

@JSila
Copy link

JSila commented Feb 15, 2017

getPosition is deprecated, use getAdapterPosition instead.

@ayetolusamuel
Copy link

ayetolusamuel 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
Copy link

ax-vasquez commented Jul 20, 2018

👍 👍 👍 👍

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

@deweysia
Copy link

deweysia commented Nov 11, 2018

Short and concise. Thanks!

@imrankhanissm
Copy link

imrankhanissm commented Feb 19, 2019

Thanks

@parthdesai1208
Copy link

parthdesai1208 commented May 27, 2019

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

@JaiMistry
Copy link

JaiMistry commented Feb 11, 2020

how to get startactivity in onclick()

v.getContext().startActivity(activityIntent);

@IrshadKasana
Copy link

IrshadKasana commented Sep 8, 2020

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

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