Today, when I was doing a ListView with a checkbox, I found that when the state of the checkbox was initialized, the ListView was scrolled, where the checkbox's selected state changed continuously. The last reason to find out is that oncheckedchanged is automatically called when the ListView Scrolls. After reviewing the various blog solutions, there is a way to solve my problem on the foreign website. Write it down and share it.
This is done in the custom adapter GetView method.
Java code
- Set the state change listener event to null before initializing the checkbox state and setting the state Change listener event
- Holder.checkBox.setOnCheckedChangeListener (null);
- Then set the checkbox state
- if (isChecked) {
- Holder.checkbox.setChecked (true);
- } Else {
- Holder.checkbox.setChecked (false);
- }
- Then set the state change listener event
- Holder.checkBox.setOnCheckedChangeListener (new Compoundbutton.oncheckedchangelistener () {
- @Override
- public void OnCheckedChanged (Compoundbutton Buttonview, boolean isChecked) {
- if (isChecked) {
- //...
- }else{
- //....
- }
- }
- });
Android automatically calls OnCheckedChanged when the ListView is scrolling causes a change in the checkbox state to be resolved