【问题标题】:How to use View Binding on BottomSheetDialog如何在 BottomSheetDialog 上使用视图绑定
【发布时间】:2021-04-20 04:11:10
【问题描述】:

我正在尝试在我的 MainActivity 中创建一个 BottomSheetDialog,但现在我正在使用合成来绑定我的视图。但现在我不知道如何将视图绑定附加到我的 BottomSheetDialog。

这就是我所做的,使用合成。

dialog = BottomSheetDialog(this, R.style.BottomDialogStyle)
dialogView = LayoutInflater.from(this).inflate(R.layout.test_dialog, dialog.mainContainer, false)
dialog.setContentView(dialogView)

这就是让我对视图绑定感到困惑的地方

dialog = BottomSheetDialog(this, R.style.BottomDialogStyle)
binding = TestDialogBinding.inflate(?)
dialog.setContentView(binding.root)

从上面的示例中,我想知道应该用什么填充参数,因为与在活动中我可以只用 layoutInflater 填充该参数或在我无法用 @987654324 填充该参数的 RecyclerView 适配器中不同@、parentfalse

【问题讨论】:

    标签: android kotlin android-viewbinding android-bottomsheetdialog


    【解决方案1】:

    你也可以创建一个 LayouInflater :

    val inflater = LayoutInflater.from(requireContext())
    

    然后将inflater传递给:

    binding = TestDialogBinding.inflate(inflater)
    

    【讨论】:

    • 我尝试使用requireContext(),但该功能在我的 MainActivity 中不可用。如果我只使用dialog.layoutInflater 可以吗?
    • 如果你在一个Activity中初始化ViewBinding,你只需要把这个(你的MainActity)放到LayoutInflater.from(this)中;
    【解决方案2】:

    如前所述进行绑定:

     bottomSheetDialog = BottomSheetDialog(mContext, R.style.BottomSheetDialogStyle)
     mBottomSheetBinding = TestDialogBinding.inflate(layoutInflater, null, false)
    
     bottomSheetDialog.setContentView(mBottomSheetBinding!!.root)
     bottomSheetDialog.show()
    

    【讨论】:

      猜你喜欢
      • 2020-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      • 1970-01-01
      • 2019-01-25
      相关资源
      最近更新 更多