Android RecyclerView OnItemClickListener Example

If you’ve used a RecyclerView, you may know that they don’t have a setOnItemClickListener as ListView had, so we have to create our own way to do it.

There are many ways to achieve this, but I will show you the way I usually do it. My mechanism consists of passing the listener as a parameter to the constructor and then assign it when I bind the data to the view in onBindViewHolder.

You’ll first need an interface that specifies listener’s behavior. In this example, there is a data class called Book, so the click will return an item of that type:

public interface OnItemClickListener {
    void onItemClick(Book book);

}

The constructor will receive an object that implements this interface, along with the items to be rendered:

public class BookAdapter extends RecyclerView.Adapter<BookAdapter.MyViewHolder> {


    private List<Book> bookList;
    private OnItemClickListener listener;

    public BookAdapter(List<Book> bookList, OnItemClickListener listener) {
        this.bookList = bookList;
        this.listener = listener;
    }

Now, in onBindViewHolder the ViewHolderwill receive the constructor in the custom bind method:

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.bind(bookList.get(position), listener);
    
}

This is how this bind method looks:

public void bind(final Book item, final OnItemClickListener listener) {

    authorName.setText(item.getAuthorName());
    bookName.setText(item.getBookName());
    imageView.setImageResource(item.getImageResource());
    itemView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            listener.onItemClick(item);
        }

    });
}

Use it whenever you need it by creating a new adapter and the listener that will implement the behaviour when an item is clicked. A simple example:

//setting custum onItemClickListener
recyclerView.setAdapter(new BookAdapter(bookList,new BookAdapter.OnItemClickListener(){
    @Override
    public void onItemClick(Book book){
        Toast.makeText(getApplicationContext(), book.getBookName()+" "+" book is Clicked", Toast.LENGTH_LONG).show();

    }
}));

Take a look at the whole code of this adapter.

package com.example.lenovo.recyclerview.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.lenovo.recyclerview.R;
import com.example.lenovo.recyclerview.model.Book;
import java.util.List;


public class BookAdapter extends RecyclerView.Adapter<BookAdapter.MyViewHolder> {

    private List<Book> bookList;
    private OnItemClickListener listener;

    public BookAdapter(List<Book> bookList, OnItemClickListener listener) {
        this.bookList = bookList;
        this.listener = listener;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView authorName, bookName;
        public ImageView imageView;

        public MyViewHolder(View view) {
            super(view);

            authorName = (TextView) view.findViewById(R.id.author_name);
            bookName = (TextView) view.findViewById(R.id.book_name);
            imageView = (ImageView) view.findViewById(R.id.img_book);
        }

        public void bind(final Book item, final OnItemClickListener listener) {

            authorName.setText(item.getAuthorName());
            bookName.setText(item.getBookName());
            imageView.setImageResource(item.getImageResource());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    listener.onItemClick(item);

                }
            });
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.book_list_row, parent, false);
        return new MyViewHolder(itemView);

    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.bind(bookList.get(position), listener);


    }

    @Override
    public int getItemCount() {
        return bookList.size();
    }

    public interface OnItemClickListener {
        void onItemClick(Book book);

    }
}

2 thoughts on “Android RecyclerView OnItemClickListener Example”

  1. So if i have a RecyclerView list can we open different activities for EACH item from the list? For example: Sports, Food, Drinks . I clicked on Sport=> Sport activity ; Food=> other activity with different layout to.
    Can you show a example in Kotlin?
    Thanks.

Leave a Reply to danill Cancel reply