3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

BottomSheetDialogFragmentで全画面表示だけ有効にしたい

Last updated at Posted at 2019-04-15

目的

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
    }
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?