1

What would be the proper way of adding Butter Knife's @OnClick to the view in this method?

    private List<Foo> foos;
    private RecyclerView recyclerView;
    private MapActivity mapView;
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_foo, parent,
                false);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int selectedPosition = recyclerView.getChildAdapterPosition(v);
                mapView.onFooSelected(foos.get(selectedPosition));
            }
        });

        return new FooInfoViewHolder(view);
    }

The way this question is different is that I want to bind @OnClick to the FooInfoViewHolder itself.

Simon
  • 2,643
  • 3
  • 40
  • 61

2 Answers2

0

Use ViewHolder class and inside of that class we can handle clicks for butter knife.

Below is the just example for understanding.

public class YourAdapter extends RecyclerView.Adapter<YourAdapter.ViewHolder> {

    private List<Data> list;

    public YourAdapter(List<Data> list) {
        this.list = list;
    }

    @Override
    public YourAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        return new ViewHolder(inflater.inflate(R.layout.list_item_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(YourAdapter.ViewHolder holder, int position) {
        Data item = list.get(position);
        holder.tvTitle1.setText(item.getTitle1());
    }

    @Override
    public int getItemCount() {
        int count = 0;
        if (null != list) {
            count = list.size();
        }
        return count;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tvVRValue1)
        TextView tvTitle1;

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

        @OnClick(R.id.tvVRValue1)
        public void onClickYourFunction(){
            //Here your onclick method goes
        }
    }
}

Hope it will help you. Cheers !!!

Ganesh Katikar
  • 2,620
  • 1
  • 26
  • 28
0

I figured out what was wrong. Actually I think it's a slightly different answer than the possible duplicates. The key was to implement the @OnClick on the ViewHolder ITSELF (not, like in the other question, on children of the view):

private List<Foo> foos;
private RecyclerView recyclerView;
private MapActivity mapView;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_foo, parent,
            false);
    return new FooInfoViewHolder(view);
}

And then the FooInfoViewHolder:

public class FooInfoViewHolder extends RecyclerView.ViewHolder {

        @OnClick
        public void onClick(View v) {
            int selectedPosition = recyclerView.getChildAdapterPosition(v);
            mapView.onFooSelected(foos.get(selectedPosition));
        }

        public FooInfoViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
Simon
  • 2,643
  • 3
  • 40
  • 61