I'm trying to add some checkboxes my recyclerview, anytime that I scroll the list, all checkboxes will be checked and unchecked! on creating the list, I need to make some checkboxes checked by default and I don't want to all checkboxes to be unchecked or checked when I scroll the list.
@Override
public Holder onCreateViewHolder(ViewGroup viewGroup) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.inf_add_manager, viewGroup, false);
Holder holder = new Holder(view, true);
holder.checkbox.setOnCheckedChangeListener(this);
for (Profile profile : mProfiles) {
if (mComplex.getManagersId().contains(profile.getId()) && !checkedContactsId.contains
(profile.getId())) {
checkedContactsId.add(profile.getId());
}
}
return holder;
}
@Override
public void onBindViewHolder(Holder holder, int position) {
Profile item = mProfiles.get(position);
holder.checkbox.setTag(item.getId());
holder.checkbox.setChecked(checkedContactsId.contains(item.getId()));
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
checkedContactsId.add((String) buttonView.getTag());
} else {
checkedContactsId.remove(buttonView.getTag());
}
}