Create a custom interface class like this
public interface ClickInterface {
public void recyclerviewOnClick(int position);
}
implement it in your Fragment and initialize the interface
YourFragment extends Fragment implements ClickInterface{
private ClickInterface listner;
------- Your oncreateView --------
listner=this; //Now pass this in your adapter
}
In your adapter constructor get this listner like this
public MyAdapter(String[] myDataset,ClickInterface listner){
this.mDataset = myDataset;
this.listner=listner;
}
and at last in your ViewHolder
public class MyViewHolder extends RecyclerView.ViewHolder{
public CardView mCardView;
public TextView mTextView;
public MyViewHolder(View v){
super(v);
mCardView = (CardView) v.findViewById(R.id.card_view);
mTextView = (TextView) v.findViewById(R.id.tv_text);
mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listner.recyclerviewOnClick(getAdapterPosition());
}
});
}
}
Now you will get the position in your fragment in
public void recyclerviewOnClick(int position){
// Here you will get the position
}
8
solved How to add OnItemClick Listener on recycler view [duplicate]