我在multiple checkbox
中有recycler view adapter
。在parent view
中有一个按钮。如果要选中任何复选框,我想启用按钮,如果所有所有复选框都未选中,我想启用disable
按钮。我该怎么做,
Holder.checboxone.setOnCheckedChangeListener(new
CompButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompButton compButton,
boolean b) {
item.setChecked(b);
checkboxStatus(b);
}
});
private void checkboxStatus(boolean isChecked) {
if (isChecked) {
((MainActivity)context).getButton().setEnabled(true);
} else {
((MainActivity)context).getButton().setEnabled(false);
}
}
根据我的代码,当我单击a复选框时,我可以启用按钮,但是禁用功能无法正常工作,如果我未选中单个复选框,那么该按钮也会禁用,我想在所有复选框都取消选中后禁用该按钮
最佳答案
内部活动(实现MyInterface
)
int selectedCount = 0; // Global variable
@Override
public void checkboxListener(boolean isSelected){
if (isSelected)
selectedCount++;
else
selectedCount--;
if (selectedCount > 0)
button.setEnabled(true);
else
button.setEnabled(false);
}
接口~~ !!
public interface MyInterface {
void checkboxListener(boolean isSelected);
}
从Checkbox.onCheckedChangeListener()内部调用此接口。在适配器内部。
更新
在适配器内部:
更改此方法。
private void checkboxStatus(boolean isChecked) {
myInterface.checkboxListener(isChecked);
}
您可能也为适配器使用了构造函数,请将其更改为:
public void MyAdapter(Context c, Data d, MyInterface myInterface){
...
this.myInterface = myInterface;
}
内部活动:
public class MyActivity extends Activity implements MyInterface { ...
并在初始化适配器时执行以下操作:
MyAdapter myAdapter = new MyAdapter(this /* context */, data /* whatever is your data */, this);
在您的活动中(我的答案的第一部分)添加代码,您就可以开始了!!!