Reputation: 1382
I am trying to make some modifications to the layout of a recyclerView child at runtime. This means for example switching between two images in a child view as the user touches on it. How can i do it ? Do i have to rebind the ViewHolder? Thank you in advance
Upvotes: 1
Views: 1000
Reputation: 499
You have a Viewholder class that has a reference to the view passed into it when you create it. So extend the Viewholder class and create a setContent method in that class. There you can set an OnClick listener or whatever is needed for your view.
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
//cast the viewholder here to the correct viewholder type
MyViewHolder myViewHolder = (MyViewHolder) viewHolder;
myViewHolder.setContent();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
View mView;
public MyViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
@Override
public void setContent() {
//set click listeners here
}
Upvotes: 1