Reputation: 1859
I have a list of CardView items in a RecyclerView. I need to bind the RecyclerView to child item list on CardView Click. So that I can use the same RecyclerView to show the child items too.Is there any way to do it?
public static class BoxViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView boxID;
TextView boxTitle;
TextView boxDescription;
BoxViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.mycard);
boxID = (TextView)itemView.findViewById(R.id.box_id);
boxTitle = (TextView)itemView.findViewById(R.id.box_title);
boxDescription = (TextView)itemView.findViewById(R.id.box_description);
itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
// view.getParent()
/* What to do ??? */
}
});
}
}
Upvotes: 0
Views: 150
Reputation: 754
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null)
mClickListener.onItemClick(view, getAdapterPosition());
}
}
The listener is a custom one, like this instantiated in your adapter class
public void setClickListener(AdapterClass.ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
When you simply implement the adapter in your Fragment / Activity you set this listener also
adapter.setClickListener(new AdapterClass.ItemClickListener() {
@Override
public void onItemClick(View view, int position) {
// change your data based on position clicked and call notifyDataSetChanged (after you made any data changes ofc)
adapter.notifyDataSetChanged();
}
});
Upvotes: 1