Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
600 views
in Technique[技术] by (71.8m points)

android - How can I select only one checkbox in Recyclerview and notifydataset changed

In my code I have create recyclerview with check box and default one item selected already. now I want when select other item checkbox so deselect all other items mean one item select at time.

My Adapter code:

public class SupportSchoolIdAdapter extends RecyclerView.Adapter<SupportSchoolIdAdapter.ViewHolder> {

ArrayList<SupportSchoolIdModel> supportSchoolIdModels;
DataPref mDataPref;
String supportSchoolId;

public SupportSchoolIdAdapter(List<SupportSchoolIdModel> supportSchoolIdModels) {
    this.supportSchoolIdModels = new ArrayList<>(supportSchoolIdModels);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    mDataPref = DataPref.getInstance(context);
    supportSchoolId = mDataPref.getSupportSchoolId();
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.support_school_item, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    final SupportSchoolIdModel event = supportSchoolIdModels.get(position);
    holder.bindData(supportSchoolIdModels.get(position));

    //in some cases, it will prevent unwanted situations
    holder.checkbox.setOnCheckedChangeListener(null);

    //if true, your checkbox will be selected, else unselected
    holder.checkbox.setChecked(supportSchoolIdModels.get(position).isSelected());

    holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            supportSchoolIdModels.get(holder.getAdapterPosition()).setSelected(isChecked);
        }
    });
    if (supportSchoolIdModels.get(position).getPkSchoolId().equalsIgnoreCase(supportSchoolId)) {
        holder.checkbox.setChecked(true);
    } else {
        holder.checkbox.setChecked(false);
    }
}
@Override
public int getItemCount() {
    return supportSchoolIdModels.size();
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    private TextView schoolIdTxt;
    private TextView schoolNameTxt;
    private CheckBox checkbox;

    public ViewHolder(View v) {
        super(v);
        schoolIdTxt = (TextView) v.findViewById(R.id.schoolIdTxt);
        schoolNameTxt = (TextView) v.findViewById(R.id.schoolNameTxt);
        checkbox = (CheckBox) v.findViewById(R.id.checkbox);
    }
    public void bindData(SupportSchoolIdModel supportSchoolIdModel) {
        schoolIdTxt.setText(supportSchoolIdModel.getPkSchoolId());
        schoolNameTxt.setText(supportSchoolIdModel.getSchoolName());
    }
}
}

And Second problem is when I scroll recycle view why selected item unchecked. please help me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

1- You can create a variable inside Adapter class that can be used to hold the selected position:

private int selectedPosition = -1;// no selection by default

2- Inside onBindViewHolder set Check State:

holder.checkBox.setChecked(selectedPosition == position);

3- Inside setOnCheckedChangeListener you must update position

this.selectedPosition = holder.getAdapterPosition();

4- Refresh adapter

adapter.notifyDatasetChanged();

EDIT

onBindViewHolder will be called for all positions,

So method setChecked will called for all CheckBoxes, This method have a boolean input.

In above example i write setChecked(selectedPosition == position). For more readable i can write:

if(selectedPosition == position){ 
   holder.checkBox.setChecked(true);
}
else{
   holder.checkBox.setChecked(false);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...