Instead of setting onclicklistener for the entire RecyclerView
set it inside the constructor of the ViewHolder
class of your Adapter.
Some sample code will be like the following class which is a inner class inside the recyclerview adapter.
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
.....
public ViewHolder(View itemView) {
super(itemView);
.......
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
.......
}
}
Inside onCreateViewHolder
of your adapter , pass the inflated view to the constructor of ViewGroup
something like ,
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.your_layout, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…