Android automatically calls onCheckedChanged when the ListView is rolling, causing constant changes in the CheckBox status. androidlistview
When creating a ListView with a CheckBox, we found that when the status of the CheckBox is initialized, The ListView is rolled, and the selected status of the CheckBox continuously changes. The final cause is that the ListView automatically calls onCheckedChanged when scrolling. After checking the solutions for various blogs, there is a solution on a foreign website to solve my problem. Write it down and share it with you.
Write this in the getView method of the custom Adapter.
Java code
- // Before initializing the CheckBox status and setting the status change listener event, set the status change listener event to null.
- Holder. checkBox. setOnCheckedChangeListener (null );
- // Then set the CheckBox status
- If (isChecked ){
- Holder. checkbox. setChecked (true );
- } Else {
- Holder. checkbox. setChecked (false );
- }
- // Then set the status change listener event
- Holder. checkBox. setOnCheckedChangeListener (new CompoundButton. OnCheckedChangeListener (){
- @ Override
- Public void onCheckedChanged (CompoundButton buttonView, boolean isChecked ){
- If (isChecked ){
- //...
- } Else {
- //....
- }
- }
- });