I'm having trouble with implementing clickable cards with my RecyclerView. I want to get the position of which card was clicked and then load the proper fragment.
I did take a look at this post and spent almost 30 minutes trying to make sense of the code and implementing it myself but I gave up when I realized how many mistakes were in the snippet.
Here is my adapter class.
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
public static CardView cv;
public static class PersonViewHolder extends RecyclerView.ViewHolder {
TextView chapterName;
TextView chapterNumber;
// ImageView chapterPhoto;
public PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
chapterName = (TextView) itemView.findViewById(R.id.chapter_name);
chapterNumber = (TextView) itemView.findViewById(R.id.chapter_number);
// chapterPhoto = (ImageView) itemView.findViewById(R.id.person_photo);
}
}
List<Chapter> chapters;
RVAdapter(List<Chapter> chapters) {
this.chapters = chapters;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.chapterName.setText(chapters.get(i).chapterName);
personViewHolder.chapterNumber.setText(chapters.get(i).chapterNumber);
// personViewHolder.chapterPhoto.setImageResource(persons.get(i).photoId);
}
@Override
public int getItemCount() {
return chapters.size();
}
}
I've looked through every implementation on StackOverflow and either I'm having tunnel vision or none of them are actually working. I know there are plenty of posts on the side, but I did spend more than a bit of time searching for the answer before asking this question, so all of those links are purple for me.
All help is appreciated, and I would be more than happy to post any other code that may be of help!
todo this you need to add the OnClickListener to your ViewHolder with an own Clicklistener, like this:
new Interface:
and change your RVAdapter class like this: