I cannot solve an issue with the getGroupView-method.
the problem is that the listener setOnCheckedChangeListener is getting invoked to many times.
Let say
Set the listener to null before calling setCheck() function, and enable it after that such as the following:
checkBox.setOnCheckedChangeListener (null);
checkBox.setChecked(true);
checkBox.setOnCheckedChangeListener (this);
Reference: Change Checkbox value without triggering onCheckChanged
OnCheckedChangeListener
worked for me with one if condition
if(buttonView.isShown())
{
//ToDo code
}
strong textThere is many way to solved the issue
checkBoxSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// write here your code for example ...
if(isChecked){
// do somtheing when is checked
}else{
// do somthing when is removed the check**strong text**
}
}
});
**and there is another way **
checkBoxSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(((CheckBox)v).isChecked()){
//do something
}else{
//do something
}
});
I was stuck with the same issue and found a way around by using on click listener
First check if the checkbox has the onclicklistener assigned to it or not. If not that means the adapter is setting the value for the first.
if(!holder.checkbox.hasOnClickListeners())
{
holder.checkbox.setChecked(data.get( position ).getSelected());
}
Now after checking this
holder.checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data.get( position ).setSelected(holder.checkbox.isChecked());
holder.checkbox.setChecked(data.get( position ).getSelected());
Toast.makeText(getApplicationContext(),
data.get( position ).get_number()+" : "+position, Toast.LENGTH_LONG).show();
}
});
Hopefully the scrolling issue goes away. Works fine here.
Add an additional if statement isShown()
in the OnCheckedChange method and it solves the problem that the OnCheckedChangeListener gets called multiple times.
There is no need to change to an onClickListener:
@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isShown()) {
if (buttonView.isChecked()) {
//Do something
} else {
//Do something
}
}
}
Add the below to ToggleButton layout:
android:saveEnabled="false"
This would make sure android doesn't store the check state to RestoreInstance
and in turn won't cause the state change experienced by you.