This is what I do to reuse OnClickListener
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.MyviewHolder>
implements View.OnClickListener
in ViewHoder Take itemlayout's parent
public class MyviewHolder extends RecyclerView.ViewHolder {
LinearLayout linearLayout_item;
public MyviewHolder(View itemView) {
super(itemView);
linearLayout_item=itemView.findViewById(R.id.linearLayout_item);
}
}
in onBindViewHolder set tag as position
@Override
public void onBindViewHolder(MyviewHolder holder, int position) {
holder.linearLayout_item.setTag(position);
holder.linearLayout_item.setOnClickListener(this);
}
and in Onclick
@Override
public void onClick(View v) {
int position = (int) v.getTag();
switch (v.getId()) {
case R.id.linearLayout_item:
// do some thing with position
break;
}
}