Recyclerview with two checkbox (yes or no) in Android

对着背影说爱祢 提交于 2019-12-13 06:33:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!