I am new in android and learning about RecyclerView. I am working with json so that i can get data from server. I followed this tutorial for this. I am not getting how to implement onclicklistner on my recyclerview.
Asked
Active
Viewed 239 times
-1
-
on which view you want onClicklistner ? – h_patel Aug 30 '16 at 13:28
-
I want that when ever i click on List , I should get the name of particular item clciked – Kevin Roberts Aug 30 '16 at 13:30
-
That i know i am asking about view like button , Textview etc ? – h_patel Aug 30 '16 at 13:32
4 Answers
0
Item click listeners for RecyclerView
See the answer
you should use ItemClickSupport library :
ItemClickSupport.addTo(recyclerView)
.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
@Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
/* do your thing */
}
});

Community
- 1
- 1

Hugo Houyez
- 470
- 3
- 19
0
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO
}
}
}

Bhushan
- 205
- 2
- 14
-
-
Please explain your answer with little description to help user understand better – Suhail Mehta Aug 31 '16 at 03:01
0
If you are talking about onItemClickListener of ListView
, unfortunately there is no such method in RecyclerView
. What you can do is, give an id to your parent view of RecyclerView
item, and set an onClickListener
on that in onBindViewHolder
.

Aman Grover
- 1,621
- 1
- 21
- 41
0
Following your tutorial, on the "bind data" method you can set a click listener. It will as easy as writing:
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// write your code here
}
});
You will also need to change the
int position
to final
, like:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder,final int position) {

Javanshir
- 772
- 2
- 7
- 20
-
-
The `int position` refers to the position of the clicked list item, if that is what you mean. If you want to show Toast, you can do this: `Toast.makeText(context, String.valueOf(position), Toast.LENGTH_LONG).show();` – Javanshir Aug 30 '16 at 13:51
-
You should know the name of the item on each position. `Toast.makeText(context, current.fishName, Toast.LENGTH_LONG).show();` should work, if you also define that `current` as `final` – Javanshir Aug 30 '16 at 14:09