RecyclerView click listener

In this post we will talk about RecyclerView. How to add listener for item click.

RecyclerView OnClick

What is RecyclerView.
With the Android L release, RecyclerView has replaced ListView as the goto component for creating a view for providing window to a large data set. It promises lots of performance improvements over the standard list view.

How to realize ItemClick and LongItemClick Listeners.
Firstly need to update build.gradle file for our project. Add one dependence:

dependencies {
    compile ''

After it, need to create an interface with prototype of onClick method.

public interface ItemClickListener {
    void onClick(View view, int position, boolean isLongClick);

Next step is creating layout for displaying some information use RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" android:layout_width="match_parent"

        android:layout_height="wrap_content" />


After it need to change main layout

<RelativeLayout xmlns:android=""



Next step is creating an own adapter

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    private Context mContext;
    private String[] mList;

    public RecyclerViewAdapter(Context contexts, String[] list) {
        this.mContext = contexts;
        this.mList = list;

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

    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.setClickListener(new ItemClickListener() {
            public void onClick(View view, int position, boolean isLongClick) {
                if (isLongClick) {
                    Toast.makeText(mContext, "#" + position + " - " + mList[position] + " (Long click)", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(mContext, "#" + position + " - " + mList[position], Toast.LENGTH_SHORT).show();

    public int getItemCount() {
        return mList.length;

    public static class ViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener, View.OnLongClickListener{
        private TextView titleTextView;
        private ItemClickListener clickListener;

        public ViewHolder(View itemView) {
            titleTextView = (TextView)itemView.findViewById(;


        public void setClickListener(ItemClickListener itemClickListener) {
            this.clickListener = itemClickListener;

        public void onClick(View view) {
            clickListener.onClick(view, getPosition(), false);

        public boolean onLongClick(View view) {
            clickListener.onClick(view, getPosition(), true);
            return true;

Our list will display numbers, for it need add to arrays.xml next array:

<string-array name="numbers">

After it need to update main activity class:

public class MainActivity extends ActionBarActivity {

    private RecyclerView mRecyclerView;
    private StaggeredGridLayoutManager mGridLayoutManager;
    private RecyclerViewAdapter mAdapter;
    private String[] mList;

    protected void onCreate(Bundle savedInstanceState) {

        mList = getResources().getStringArray(R.array.numbers);

        mRecyclerView = (RecyclerView) findViewById(;
        mGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mAdapter = new RecyclerViewAdapter(getApplicationContext(), mList);

Project on GitHub


24 thoughts on “RecyclerView click listener

  1. Why do u call this in onBindViewHolder() intead of call in onCreateViewHolder() ?
    The onBindViewHolder() method will be called every time a new item scrolls into view, or has its data change.

    holder.setClickListener(new ItemClickListener() {
    public void onClick(View view, int position, boolean isLongClick) {
    if (isLongClick) {
    Toast.makeText(mContext, “#” + position + ” – ” + mList[position] + ” (Long click)”, Toast.LENGTH_SHORT).show();
    } else {
    Toast.makeText(mContext, “#” + position + ” – ” + mList[position], Toast.LENGTH_SHORT).show();

    1. Hi,

      I used this code in onBindViewHolder(), because data will be different for each item. This example is really basic, but in more complex application you will use more complex realisation for this method.

  2. hi i use seperately recycleradapter and recycleritemviewholder and i dont know where will i use setClickListener(). can you help me? thank you

    1. As I understand your RecyclerView in a Fragment. You can update an adapter from the Fragment without any problem.

      If you have any problem, please let me know.

  3. hy…… what to do if i want to get different data on each item click i.e every item shows different data when clicked.

    1. You will get different data on each click because you get position. Afterwards you can get item from list of database use position, id, etc

  4. i have two fragment called fragment a and fragment b, if i click item in fragment a replace fragment a by fragment c. same thing click fragment b item replace fragment b by fragment c.
    but my issue is the item where ever i click fragment a is replace by fragment c.

  5. I have an issue about my trying. My bindviewholder is getting angry on this line:

    holder.setClickListener(new ItemClickListener() {

    for setClickListener.

    What can ı do now? Can u help me

Leave a Reply

Your email address will not be published. Required fields are marked *