1

So the problem is the onClick only gets called for the imageButton and not the while itemView. Here is my ViewHolder Class

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    protected TextView title;
    protected TextView rank;
    protected ImageView image;
    protected ImageButton share;
    public ViewHolder(View itemView) {
        super(itemView);
        title =  (TextView) itemView.findViewById(R.id.main_title);
        rank = (TextView) itemView.findViewById(R.id.rank_text);
        image = (ImageView) itemView.findViewById(R.id.main_image);
        share = (ImageButton) itemView.findViewById(R.id.main_share);
        share.setOnClickListener(this);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Log.d("click", "clicked at" + getAdapterPosition());
    }
}

UPDATE:

Because I was using cardviews in my recycleview, I ended up changing my viewholder code to this

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    protected TextView title;
    protected TextView rank;
    protected ImageView image;
    protected ImageButton share;
    protected CardView cardView;
    public ViewHolder(View itemView) {
        super(itemView);
        title =  (TextView) itemView.findViewById(R.id.main_title);
        rank = (TextView) itemView.findViewById(R.id.rank_text);
        image = (ImageView) itemView.findViewById(R.id.main_image);
        share = (ImageButton) itemView.findViewById(R.id.main_share);
        cardView = (CardView) itemView.findViewById(R.id.main_card_view);
        share.setOnClickListener(this);
        cardView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Log.d("test", "test" + getAdapterPosition());
    }
}

And it works

gman9732
  • 77
  • 2
  • 10

4 Answers4

1

Because I was using cardviews in my recycleview, I ended up changing my viewholder code to this

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    protected TextView title;
    protected TextView rank;
    protected ImageView image;
    protected ImageButton share;
    protected CardView cardView;
    public ViewHolder(View itemView) {
        super(itemView);
        title =  (TextView) itemView.findViewById(R.id.main_title);
        rank = (TextView) itemView.findViewById(R.id.rank_text);
        image = (ImageView) itemView.findViewById(R.id.main_image);
        share = (ImageButton) itemView.findViewById(R.id.main_share);
        cardView = (CardView) itemView.findViewById(R.id.main_card_view);
        share.setOnClickListener(this);
        cardView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Log.d("test", "test" + getAdapterPosition());
    }
}

`And it works

gman9732
  • 77
  • 2
  • 10
0

You are using same click listener for both itemView and share

  • separate view clicks properly

    @Override
    public void onClick(View view) {
        if (view.equals(share) {
            // 'share' was tapped
        }
        else {
            // 'itemView' was tapped
        }
    }
    
  • in your XML layout (and I am guessing it here :) change as follow (i.e. add clickable on right places)

    // this is your list cell (the item renderer)
    <RelativeLayout
        android:clickable="true"
        ...>
        <TextView 
            android:id="@+id/main_title"
            android:clickable="false"
            .../>
        <TextView 
            android:id="@+id/rank_text"
            android:clickable="false"
            .../>
        <ImageView 
            android:id="@+id/main_image"
            android:clickable="false"
            .../>
        <ImageButton 
            android:id="@+id/main_share"
            android:clickable="true"
            .../>
    </RelativeLayout>
    

If it does not work for some reason, please post your XML layout and any other code you can share.

Dimitar Genov
  • 2,056
  • 13
  • 11
0

RecyclerView doesn't support onClick method over a whole itemview. If you want to implement onClick, you need to set OnClickListener on child views of itemview.

Meanwhile, RecyclerView only support onTouch method of OnTouchListener on itemview.

SilentKnight
  • 13,761
  • 19
  • 49
  • 78
  • I am trying this on a similar issue that I am looking to solve. I'd appreciate any thoughts or insights you may have. Located here: https://stackoverflow.com/questions/47783631/recyclerview-how-to-add-onclick-and-keep-onlongclick-working – AJW Dec 16 '17 at 02:42
-1
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    protected TextView title;
    protected TextView rank;
    protected ImageView image;
    protected ImageButton share;
    protected CardView cardView;
    public ViewHolder(View itemView) {
        super(itemView);
        title =  (TextView) itemView.findViewById(R.id.main_title);
        rank = (TextView) itemView.findViewById(R.id.rank_text);
        image = (ImageView) itemView.findViewById(R.id.main_image);
        share = (ImageButton) itemView.findViewById(R.id.main_share);
        cardView = (CardView) itemView.findViewById(R.id.main_card_view);
        itemView.share.setOnClickListener(this);
        itemView.cardView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Log.d("test", "test" + getAdapterPosition());
    }
}