Set state of BottomSheetDialogFragment to expanded

前端 未结 14 863
有刺的猬
有刺的猬 2020-11-30 19:11

How do you set the state of a fragment extending BottomSheetDialogFragment to expanded using BottomSheetBehavior#setState(STATE_EXPANDED) using the

14条回答
  •  孤独总比滥情好
    2020-11-30 19:49

    Similar to uregentx answer, in kotlin, you can declare your fragment class that extends from BottomSheetDialogFragment, and when the view is created you can set the dialog listener default state after the dialog is displayed.

    STATE_COLLAPSED: The bottom sheet is visible but only showing its peek height.

    STATE_EXPANDED: The bottom sheet is visible and its maximum height.

    STATE_HALF_EXPANDED: The bottom sheet is visible but only showing its half height.

    class FragmentCreateGroup : BottomSheetDialogFragment() {
          ...
    
        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {
            // Set dialog initial state when shown
            dialog?.setOnShowListener {
                val bottomSheetDialog = it as BottomSheetDialog
                val sheetInternal: View = bottomSheetDialog.findViewById(com.google.android.material.R.id.design_bottom_sheet)!!
                BottomSheetBehavior.from(sheetInternal).state = BottomSheetBehavior.STATE_COLLAPSED
            }
    
            val view = inflater.inflate(R.layout.fragment_create_group, container, false)
            ...
    
            return view
        }
    }
    

    Remember using material design implementation in gradle.

    implementation "com.google.android.material:material:$version"

    Also take a look to material design reference Bottom Sheets

提交回复
热议问题