-1

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.

4 Answers4

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
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
  • and i also want to get value when i click on list – Kevin Roberts Aug 30 '16 at 13:48
  • 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