目的
BottomSheetDialogFragmentで全画面表示だけ有効にしたい
コード
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
dialog.setOnShowListener { dialog ->
val d = dialog as BottomSheetDialog
val bottomSheet = d.findViewById(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout?
val behavior = BottomSheetBehavior.from(bottomSheet)
behavior.state = BottomSheetBehavior.STATE_COLLAPSED
behavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onSlide(bottomSheet: View, slideOffset: Float) {
//何もしない
}
override fun onStateChanged(bottomSheet: View, newState: Int) {
//この条件分岐追加しないと画面が消えた後モーダルの半透明黒マスクだけが残る
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss()
behavior.state = BottomSheetBehavior.STATE_COLLAPSED
}
//途中で止まろうとするので強制的に全画面表示する
else if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
}
})
//下から出てきて全画面表示されるようにする
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}