我创建了一个扩展BottomSheetDialogUtil class
的{{1}}。只要在要显示在底部工作表对话框中的视图中传递,就可以使用此类。
第一次显示BottomSheetDialogFragment
时可以正常显示,但是第二次尝试显示时会崩溃
BottomSheetDialogFragment
private void configureBudgetPeriodSpinner() {
OrderPaymentCreditCardBudgetOptionsBinding binding = OrderPaymentCreditCardBudgetOptionsBinding.inflate(getLayoutInflater());
BottomSheetDialogUtil bottomSheetDialog = BottomSheetDialogUtil.newInstance(binding.getRoot());
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(), R.array.credit_card_budget_period, R.layout.spinner_budget_option);
binding.listView.setAdapter(adapter);
binding.listView.setOnItemClickListener((aAdapterView, aView, aI, aL) -> {
String string = binding.listView.getItemAtPosition(aI).toString();
mBinding.formInclude.budgetOptionsTextInput.setText(string);
mViewModel.setBudgetPeriod(CreditCardUtil.getBudgetPeriodInt(string));
bottomSheetDialog.dismiss();
});
mBinding.formInclude.budgetOptionsTextInput.setOnClickListener(aView -> bottomSheetDialog.show(getFragmentManager(), "Bottom Sheet Dialog Fragment"));
}
public class BottomSheetDialogUtil extends BottomSheetDialogFragment {
private static View mView;
public static BottomSheetDialogUtil newInstance(View aView) {
mView = aView;
return new BottomSheetDialogUtil();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void setupDialog(Dialog dialog, int style) {
dialog.setContentView(mView);
((View) mView.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent));
}
@Override
public void onDestroy() {
super.onDestroy();
}
}