Set state of BottomSheetDialogFragment to expanded

前端 未结 14 829
有刺的猬
有刺的猬 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:29

    I think those above is better. Sadly I did not find those solution before I had solved. But write my solution. quite similar to all.

    ==================================================================================

    I face the same issue. This is what I solved. The Behavior is hidden in BottomSheetDialog, which is available to get the behavior If you would like not to change your parent layout to be CooridateLayout, you can try this.

    STEP 1: customize the BottomSheetDialogFragment

    open class CBottomSheetDialogFragment : BottomSheetDialogFragment() {
       //wanna get the bottomSheetDialog
       protected lateinit var dialog : BottomSheetDialog 
       override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
          dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
          return dialog
       }
    
       //set the behavior here
       fun setFullScreen(){
          dialog.behavior.state = STATE_EXPANDED
       }
    }
    

    STEP 2: make your fragment extend this customized fragment

    class YourBottomSheetFragment : CBottomSheetDialogFragment(){
    
       //make sure invoke this method after view is built
       //such as after OnActivityCreated(savedInstanceState: Bundle?)
       override fun onStart() {
          super.onStart()
          setFullScreen()//initiated at onActivityCreated(), onStart()
       }
    }
    

提交回复
热议问题