13

As onItemClick is no more in RecyclerView, does ButterKnife still able to handle its item clicks with @onItemClick or with @onClick annotations?

If not so, any work around to use that?

Sufian
  • 6,405
  • 16
  • 66
  • 120
Silly Questions
  • 183
  • 1
  • 1
  • 12

4 Answers4

10

My solution is to use @OnClick inside the ViewHolder. This is the only way to have a clue about which element of the "list item" has been clicked.

Ivan Morgillo
  • 2,837
  • 4
  • 30
  • 46
8

@OnClick Annotation works, Use as below in your ViewHolder class.

class ViewHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.title)
    TextView title;

    ViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }

    @OnClick
    void onClick(View view) {
        System.out.println(getAdapterPosition()); //clicked item position
    }
}
Naveen T P
  • 6,955
  • 2
  • 22
  • 29
1

Unfortunately ButterKnife does not support this feature. You could make something out using RecyclerView.OnItemTouchListener or @OnClick and interface.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
0
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
          @Override
            public void onClick(View view) {

                int data = getAdapterPosition();
/*
             Intent in = new Intent(getContext(),NextActivity.class);
               in.putExtra("data",mTextView.getText().toString());
             startActivity(in);
*/
//                Flower current = ls.get(position);
## Heading ##

                int itemPosition = recycelr.getChildLayoutPosition(view);
                Flower item = fl_List.get(itemPosition);

                Intent in = new Intent(getContext(),NextActivity.class);
                in.putExtra("id",0);
                in.putExtra("data",item.getName());
                startActivity(in);

            }
  • 1
    While **this code** may answer the question, it is better to include the essential part of text for understanding and provide the code for reference. **Code-only** answers can become invalid if **the code** becomes old. – Sufian Jul 08 '16 at 14:21