-2

I'm looking the answer how to make onClickListener button located in Recycleview Item using Butterknife. I know how to do it without Butterknife, but I can't find anything with Bt. Does Bt supports this?

user9897182
  • 255
  • 5
  • 18

2 Answers2

0

Try to learn from the sample code at official site If you want to implement the click logic in the activity then here are the steps.

1 Create an interface

    public interface ClickHandler{
             void onClick(int position);
         }

2 Implement CLickHandler in activity

    MainActivity extends AppCompatActivity implements ClickHandler{
    ...
    public void onclick(int position){
        Log.d("Check","Clicked at" + position);
    }
    ...
    adapter = new MyAdapter(this);//Pass the reference to activity as it implements the clickhandler.
    ...

}

3 Now your adapter has the reference for clickhandler. Similarly pass it to the viewholder and call the onCLick method from there.

    class ViewHolder {
        @BindView(R.id.title) TextView name;
        @BindView(R.id.job_title) TextView jobTitle;
        ClickHandler clickHandler;

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

        @OnClick(R.id.submit)
         public void submit(View view) {
           if(clickHandler(!=null){
              clickHandler.onClick(getAdapterPosition());
           }
        }
      }
Jitendra A
  • 1,568
  • 10
  • 18
0
' class ViewHolder {
    @BindView(R.id.title) TextView name;
    @BindView(R.id.job_title) TextView jobTitle;

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

    @OnClick(R.id.submit)
     public void submit(View view) {
       // TODO submit data to server...
    }
  }'