I have a RecyclerView grid adapter that contains checkboxes and a show button in parent activity. If we select the show checkbox parent activity show button to enable, and if we unchecked the show check button will be disabled, this is my requirement and it works fine, but when I scroll through the view mode of my show button to disable. When I scroll, I get item.isChecked()false. Can someone help me achieve my requirement please explain
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
try {
final GridViewHolder viewHolder = (GridViewHolder) holder;
final MyList item = items.get(position);
viewHolder.myCheckBox.setTag(position);
viewHolder.myCheckBox.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton,
boolean b) {
item.setChecked(b);
if (item.isChecked())
Count++;
else
Count--;
if (Count > 0) {
((MyActivity)context).showButton().setEnabled(true);
} else {
((MyActivity)context).showButton().setEnabled(false);
}
}
});
viewHolder.myCheckBox.setChecked(item.isChecked());
} catch (Exception e) {
e.printStackTrace();
}
}
In my pojo list class I have written two functions for the checkbox id return values
boolean isChecked;
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
ViewHolder inner class in adapter
public class GridViewHolder extends RecyclerView.ViewHolder {
private CheckBox myCheckBox;
public GridViewHolder(final View parent) {
super(parent);
myCheckBox = (CheckBox) parent.findViewById(myCheckBox);
((ProductListingActivity)context).showButton().setEnabled(false);
parent.setOnClickListener(GridAdapterMy.this);
}
}
source