问题
How to get the position of particular checkbox(yes or no) in each row of a recycler view I am able to get the position of row wise checkbox but on scrolling both (yes and no) checkbox are getting checked.
((CheckBoxViewHolder) holder).chkNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((CheckBoxViewHolder) holder).chkYes.setChecked(false);
CheckBox cb = (CheckBox) v;
RatingQues contact = (RatingQues) cb.getTag();
contact.setSelected(cb.isChecked());
ratingQuestions.get(position).setSelected(cb.isChecked());
((CheckBoxViewHolder) holder).chkYes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((CheckBoxViewHolder) holder).chkNo.setChecked(false);
CheckBox cb = (CheckBox) v;
RatingQues ratingQues = (RatingQues) cb.getTag();
ratingQues.setSelected(cb.isChecked());
ratingQuestions.get(position).setSelected(cb.isChecked());
回答1:
Maintain a model for the current status, for example -
class MyModel{
String status;
boolean isChecked;
}
Now tag position with every checkbox, in onBind. So whenever you getChangeListner just change in model. Also bind checkbox as per model flag value.
Hope it will help, let me know if you need further help :)
回答2:
See this answer you can go with two Boolean variable and instead od visibility use setchecked(true/false)
回答3:
You can use a SparseArray
to record which one you have already checked.
来源:https://stackoverflow.com/questions/38182426/recyclerview-with-two-checkbox-yes-or-no-in-android