Android DialogFragment-多选对话框复选框不更新/需要两次单击
我认为这只在最近的Android API中才开始发生,但问题是,当您第一次尝试在多选择对话框中选择复选框时,需要额外的点击才能更新UI 我很确定这是由于Android的错误,因为我的代码非常简单Android DialogFragment-多选对话框复选框不更新/需要两次单击,android,android-dialogfragment,android-dialog,Android,Android Dialogfragment,Android Dialog,我认为这只在最近的Android API中才开始发生,但问题是,当您第一次尝试在多选择对话框中选择复选框时,需要额外的点击才能更新UI 我很确定这是由于Android的错误,因为我的代码非常简单 经过大量的实验,我找到了答案,因此将在下面分享…解决方案的棘手部分是获取视图对象。一旦有了视图,就可以使其失效()以更新复选框的UI 下面是我的DialogFragment子类的要点: public class MyMultiChoiceDialogFragment extends DialogFrag
经过大量的实验,我找到了答案,因此将在下面分享…解决方案的棘手部分是获取
视图
对象。一旦有了视图
,就可以使其失效()
以更新复选框的UI
下面是我的DialogFragment
子类的要点:
public class MyMultiChoiceDialogFragment extends DialogFragment {
private View mView = null;
@Override @NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(title);
builder.setMultiChoiceItems(
cursor,
isCheckedColumn
labelColumn
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which, boolean isChecked) {
// Handle the checkbox de/selection
/*
* The problem is that, despite onClick being called (with the correct parameter values), the
* checkbox ticks were not updating on the UI.
* Solution is to invalidate/redraw the layout so the checkboxes also update visually
*/
mView.invalidate();
mView.forceLayout(); // Following tests, this line is also required.
}
});
AlertDialog dialog = builder.create();
/*
* This seems to be the only way to get the view.
* Save it in an instance variable so we can access it within onClick()
*/
mView = dialog.getListView();
return dialog;
}
@Override
public void onDestroyView() {
super.onDestroyView();
mView = null; // Clean up / prevent memory leak - necessary?
}
}