Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView: how to catch the onClick on an ImageView?

I have an ImageView in CardViews of my RecyclerView. I would like to catch the onClick on the ImageView, like I already did with the ListView. So, in the public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) I tried with:

 myViewHolder.imageView.setClickable(true);
 myViewHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               Log.d("TEST", "Clicked");
            }

but this was totally ignored. The following code is working:

 myViewHolder.imageView.setImageResource(R.drawable.star_yellow);

What can I do?

like image 658
helloimyourmind Avatar asked Oct 21 '15 16:10

helloimyourmind


People also ask

Where do you add the android onClick attribute to make items in a RecyclerView respond to clicks?

Where do you add the android:onClick attribute to make items in a RecyclerView respond to clicks? In the layout file that displays the RecyclerView, add it to the element. Add it to the layout file for an item in the row.

What is ViewHolder in RecyclerView?

A ViewHolder describes an item view and metadata about its place within the RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive findViewById results. While LayoutParams belong to the LayoutManager , ViewHolders belong to the adapter.


1 Answers

Here's what I do in my project.

  1. Have an interface class somewhere in your project:

    public interface RecyclerViewClickListener {
    
        void onRowClicked(int position);
        void onViewClicked(View v, int position);
    }
    
  2. In your RecyclerView.Adapter class, add this interface as a field and pass it to adapter via constructor:

    public class YourRecAdapter extends RecyclerView.Adapter<YourRecAdapter.SimpleViewHolder>{
    
        private RecyclerViewClickListener listener;
    
        public YourRecAdapter(RecyclerViewClickListener listener){
            this.listener = listener;
        }
    
        public static class SimpleViewHolder extends RecyclerView.ViewHolder {
    
            public SimpleViewHolder(View itemView, final RecyclerViewClickListener listener) {
                super(itemView);
    
                // find view ids here
    
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(listener != null)
                            listener.onRowClicked(getAdapterPosition());
                    }
                });
    
                someViewholderView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(listener != null)
                            listener.onViewClicked(v, getAdapterPosition());
                    }
                });
            }
        }
    }
    
  3. Implement the interface in your Activity / Fragment:

    public class YourFragment extends Fragment implements RecyclerViewClickListener {
    
       @Override
       public void onViewClicked(View v, int position) {
           if(v.getId == R.id.yourDesiredView){
               // Do your stuff here
           }
       }
    
       @Override
       public void onRowClicked(int position) {
           // Clicked entire row
       }
    }
    
like image 103
Tomislav Turcic Avatar answered Nov 13 '22 01:11

Tomislav Turcic