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

前端 未结 5 1154
没有蜡笔的小新
没有蜡笔的小新 2020-12-09 14:14

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 i

5条回答
  •  佛祖请我去吃肉
    2020-12-09 14:28

    As you said, you are new to android first you will notice that your checkbox will have issues with recycler view.You selected some item in recycle view and after scrolling you will notice some other item is selected to solve this you need to follow this answer. or You need to create a boolean variable( to capture the state of checked checkbox for each item in recycler view)in your School Model class like this

     class SchoolModel{
        private boolean isChecked;
        public boolean isChecked(){
         return ischecked;}
    
        public void setIsChecked(boolean checked){
          isChecked = checked;
        }
       }
    

    Second problem is to select only one checkbox and deselect rest of them for this you need write your logic(Check here for solution).And one major code change is to move your setOnCheckedChangeListener to view holder class because bindviewholder is called everytime you update your recyclerview and thus setting setOnCheckedChangeListener every time which is not correct.So you put this listener in view holder this will assign per checkbox listener only one time.

     checkBoxDeleteComment.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                   //your code
                }
            });
    

提交回复
热议问题