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:
[xml]
dependencies {
compile ‘com.android.support:recyclerview-v7:22+’
}
[/xml]

After it, need to create an interface with prototype of onClick method.
[java]
public interface ItemClickListener {
void onClick(View view, int position, boolean isLongClick);
}
[/java]

Next step is creating layout for displaying some information use RecyclerView
[xml]


[/xml]

After it need to change main layout
[xml]


[/xml]

Next step is creating an own adapter
[java]
public class RecyclerViewAdapter extends RecyclerView.Adapter {

private Context mContext;
private String[] mList;

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

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

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.titleTextView.setText(mList[position]);
holder.setClickListener(new ItemClickListener() {
@Override
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();
}
}
});
}

@Override
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) {
super(itemView);
titleTextView = (TextView)itemView.findViewById(R.id.textView);
itemView.setTag(itemView);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);

}

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

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

@Override
public boolean onLongClick(View view) {
clickListener.onClick(view, getPosition(), true);
return true;
}
}
}
[/java]

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

One
Two
Three
Four
Five
Six
Seven
Eight

[/xml]

After it need to update main activity class:
[java]
public class MainActivity extends ActionBarActivity {

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

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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

mRecyclerView = (RecyclerView) findViewById(R.id.list);
mGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setLayoutManager(mGridLayoutManager);
mAdapter = new RecyclerViewAdapter(getApplicationContext(), mList);
mRecyclerView.setAdapter(mAdapter);
}
}
[/java]

Project on GitHub

24 CommentsClose Comments

24 Comments

  • Tuan
    Posted December 22, 2015 at 3:25 am 0Likes

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

    • Posted December 26, 2015 at 11:39 am 0Likes

      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.

  • avi
    Posted April 30, 2016 at 10:24 am 0Likes

    hi, where exactly do you define the interface?

    • Posted May 1, 2016 at 5:11 pm 0Likes

      Hi,

      thank you for your question. I created this interface in separate file. You can find all classes and interfaces here

  • Ahesanali Suthar
    Posted April 30, 2016 at 12:48 pm 0Likes

    Really nice explanation. Fully helpfull

    Awesome tutorial.

    Keep it up!

    Thanks! Happy programming! 🙂

    • Posted May 1, 2016 at 5:14 pm 0Likes

      Thank you for your comment. I hope my blog helps developers.

  • mustafa
    Posted June 29, 2016 at 8:34 pm 0Likes

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

    • Posted July 3, 2016 at 5:21 pm 0Likes

      Hi,

      as example you can create additional method for view holder class

  • chirag
    Posted August 5, 2016 at 8:25 am 0Likes

    Such a great article… thank you so much….

  • Rajeesh
    Posted August 25, 2016 at 6:36 pm 0Likes

    Thank you.
    This is really a simple and nice article.

  • Sephiroth
    Posted November 7, 2016 at 9:00 am 0Likes

    Working with some modification to my code.
    Thank you.

  • jon
    Posted March 22, 2017 at 2:16 am 0Likes

    how to implement this code in the fragment recyclerview ??

    • Posted April 16, 2017 at 9:16 am 0Likes

      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.

  • Mandeep Yadav
    Posted April 12, 2017 at 7:07 am 0Likes

    Thank You So Much…
    Very Nice Explanation.

  • ajay
    Posted May 9, 2017 at 10:31 am 0Likes

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

    • Posted June 14, 2017 at 5:54 pm 0Likes

      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

  • nitish
    Posted August 15, 2017 at 1:08 pm 0Likes

    How to implement this using Fragments ?

    • Posted November 23, 2017 at 8:14 pm 0Likes

      You can create a fragment with RecyclerView and move all code connected with RecyclerView to the fragment.

  • Pradeepa
    Posted January 10, 2018 at 7:38 am 0Likes

    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.

    • Posted January 16, 2018 at 7:19 pm 0Likes

      I need more information about your code for help. Could you check the implementation of ItemClickListener interface?

  • خرید گیفت کارت پلی استیشن
    Posted January 29, 2018 at 8:52 pm 0Likes

    very useful post thank you

  • Lans
    Posted March 7, 2018 at 12:29 am 0Likes

    This solved my problem!! I’d been struggling with it for over two days until now.
    Thank you!

  • Nur
    Posted July 25, 2018 at 4:51 pm 0Likes

    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 comment

Newsletter Subscribe

Get the Latest Posts & Articles in Your Email

We Promise Not to Send Spam:)