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
  • 9

5 Answers5

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
  • 9
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,018
  • 12
  • 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,435
  • 18
  • 48
  • 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
0

Multiple onClick events inside a recyclerView:

    public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{

    public ImageView iconImageView;
    public TextView iconTextView;

    public MyViewHolder(final View itemView) {
        super(itemView);

        iconImageView = (ImageView) itemView.findViewById(R.id.myRecyclerImageView);
        iconTextView = (TextView) itemView.findViewById(R.id.myRecyclerTextView);
        // set click event
        itemView.setOnClickListener(this);
        iconTextView.setOnClickListener(this);
        // set long click event
        iconImageView.setOnLongClickListener(this);
    }

    // onClick Listener for view
    @Override
    public void onClick(View v) {

        if (v.getId() == iconTextView.getId()){
            Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
        }
    }


    //onLongClickListener for view
    @Override
    public boolean onLongClick(View v) {

        final AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
        builder.setTitle ("Hello Dialog")
                .setMessage ("LONG CLICK DIALOG WINDOW FOR ICON " + String.valueOf(getAdapterPosition()))
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

        builder.create().show();
        return true;
    }
}
Atman Bhatt
  • 1,245
  • 8
  • 14
-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());
    }
}