Reputation: 102
I have a recyclerView with multiples view, I already settle all diferent viewholder correctly and some viewholder are represent with the same ViewGroup, inside the viewgroup I have a button when this button is clicke it change the value in a textView until now everything work well, the problem is when I trigger the event some of the all the viewgroup in the list of same view group change when I click one button in not order specifically inside the list but is constant in the instance,I already set some breakpoint but anything out of the normal, any ide why something like this is happen?
my ViewHolder Hirechary look like this.
public abstract class AbstractViewHolderR extends RecyclerView.ViewHolder{
public Button btn;
public TextView view;
...
}
public class ChildVH1 extends AbstractViewHolderR{
...
}
...other ViewHolder look the same.
/// the event listener look like this.
public class Click implement View.OnClickListener{
AbstractViewHolderR object;
public Click(AbstractViewHolderR o){
object = o;
}
@Override
public void onClick(View v){
////error checking omited
Integer ii = Integer.Parse(object.view.getText().toString);
ii++;
object.view.setText(ii.ToString());
}
}
Upvotes: 0
Views: 421
Reputation: 1278
Move your TextView and Btn to View holder instead and you should add click listener to ViewHolder itself like this
public abstract class AbstractViewHolderR extends RecyclerView.ViewHolder{
//UPDATE
@Override
public long getItemId(int position) {
return position;
}
...
}
public class ChildVH1 extends RecyclerView.ViewHolder{
public Button btn;
public TextView view;
public ChildVH1(View itemView){
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do your stuff
//you can use getAdapterPosition() to get current
//position in adapter
}
});
}
}
}
Upvotes: 1