How to use ButterKnife OnItemClick with RecyclerView?
Asked Answered
D

4

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?

Dardani answered 6/11, 2014 at 4:24 Comment(2)
Could illustrate a bit mor on this, how do you get the position of the clicked cell please ?Alessi
Can you show more data or code to understand what you needOutmaneuver
D
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.

Dedans answered 28/1, 2015 at 12:36 Comment(0)
A
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
    }
}
Aideaidedecamp answered 27/11, 2017 at 18:25 Comment(0)
S
1

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

Schiller answered 28/11, 2014 at 2:58 Comment(0)
R
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);

            }
Reeder answered 8/7, 2016 at 7:45 Comment(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.Habit

© 2022 - 2024 — McMap. All rights reserved.