4

I'm currently learning butterknife, and I need to bind specific @OnClicks on different views generated by my customer adapter. How do I go about with this?

I've seen an answer in one of the questions here saying that he uses '@onclick' inside the 'ViewHolder'. I'm not sure how to implement this exactly. Any ideas?

Thanks!

GonnaGetGet
  • 254
  • 2
  • 12

4 Answers4

6

Imagine you have a ViewHolder like this.

static class ViewHolder {

    @InjectView(R.id.user_name)
    TextView userName;

    @InjectView(R.id.user_title)
    TextView userTitle;


    public ViewHolder(View view) {
        ButterKnife.inject(this, view);
    }

}

You can bind events in your adapters getView method for each view in your holder.

 @Override
public View getView(int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;
    if (convertView != null) {
        holder = (ViewHolder) convertView.getTag();
    } else {
        convertView = LayoutInflater.from(mContext).inflate(
                R.layout.your_row_layout, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    }

   holder.userName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Your stuff here
        }
    });


    holder.userTitle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           //Your stuff here  
        }
    });

    return convertView;

}
Ugur
  • 1,679
  • 14
  • 20
  • 2
    +1 for making ViewHolder final and actually calling it ViewHolder. +1 for making the ViewHolder Class static. Finally, +1 for using != null to get the smaller block of if-logic up top and out of the way. Never considered doing the the if-logic that way. I'll be updating my code ;) I wish more SO answers were like this. Well done @Ugur. – Bill Mote Mar 29 '15 at 03:06
  • Thanks for the quick answer! This is exactly what I needed! Marked as answer. (: – GonnaGetGet Mar 29 '15 at 03:26
3

As @Ugur has suggested you would initiate your ViewHolder using butterknife. You can add an on click listener to a view by doing this as an example for the userName View:

@OnClick(R.id.user_name)
public void clickedUserName(SocialEyeTextView userName)
{
    //Do something with user name
}
Marcus Hooper
  • 647
  • 7
  • 12
1

I have this ViewHolder..

public class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item_tipo_evento_estado_text) TextView text;
        @Bind(R.id.item_text_radiobutton_check) AppCompatCheckBox checkBox;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick(R.id.btnCheck)
        public void onCheck() {
            Log.d("ViewHolder", "position ->" + getAdapterPosition());
        }
    }

ok my R.id.btnCheck is my id of LinearLayout content all Views

marlonpya
  • 624
  • 7
  • 21
0

write this code in ViewHolder class

static class ViewHolder {
    public int getPosition;

    @Nullable
    @Bind(R.id.txt_title_name)
    TextView txtTitle;   

 public ViewHolder(View view) {
        ButterKnife.bind(this, view);
    }


    @OnClick(R.id.btn_download)
    public void onCheck() {
        Log.d("ViewHolder", "position ->" + getPosition);
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

holder.getPosition = position;
}
Pedro del Sol
  • 2,840
  • 9
  • 39
  • 52