【问题标题】:ViewBinding in a Dialogfragment using a customview使用自定义视图的 Dialogfragment 中的 ViewBinding
【发布时间】:2021-02-08 11:14:18
【问题描述】:

我有一个关于在使用自定义视图的对话框片段中使用viewBinding 的问题。有这样做的标准方法吗?

我的代码使用findViewById() 作为对话框片段,但我想使用视图绑定,因为这是我项目其余部分的标准。

class WifiHandlerDialogFragment(private val wifiErrorType: Int): DialogFragment() {

private var _binding: DialogWifiHandlerBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

    _binding = DialogWifiHandlerBinding.inflate(LayoutInflater.from(context))

    val dialog = activity?.let {
        Dialog(it)
    }

    if(dialog != null) {
        dialog.window?.requestFeature(Window.FEATURE_NO_TITLE)
        dialog.window?.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN)
        dialog.setContentView(R.layout.dialog_wifi_handler)
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

        val positiveButton = dialog.findViewById<Button>(R.id.positive_button) // really want to change this to use binding
        val closeButton = dialog.findViewById<Button>(R.id.close_button) // really want to change this to use binding
        val dialogMessage = dialog.findViewById<TextView>(R.id.dialog_message)

        positiveButton.setOnClickListener {
            startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
        }
        closeButton.setOnClickListener {
            dismiss()
        }

        dialogMessage.text = when (wifiErrorType) {
            1 ->  getString(R.string.connection_dialog_op1)
            2 -> getString(R.string.connection_dialog_op2)
            3 -> getString(R.string.connection_dialog_op3)
            else -> getString(R.string.error)
        }

    }

    return dialog!!
}

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}
}

我试图在onCreateDialog() 函数中使用binding.closebutton,但我们无法正常工作(我假设由于片段生命周期)。

我看过这些问题:

How to correctly use Android View Binding in DialogFragment?

Android DialogFragment onViewCreated not called

但仍然没有找到实现这一目标的最佳方法(也是我第一次使用来自 kotlin 合成的viewbinding)。

【问题讨论】:

    标签: android android-dialogfragment android-dialog android-viewbinding


    【解决方案1】:

    已修复。根本没有将内容视图设置为binding.root。现在工作正常。 https://medium.com/nerd-for-tech/exploring-view-binding-in-activities-fragments-dialogs-and-recyclerview-adapters-789f84b31a2a

    class WifiHandlerDialogFragment(private val wifiErrorType: Int): DialogFragment() {
    
        private var _binding: DialogWifiHandlerBinding? = null
        // This property is only valid between onCreateView and
        // onDestroyView.
        private val binding get() = _binding!!
    
        override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    
            _binding = DialogWifiHandlerBinding.inflate(LayoutInflater.from(context))
    
            val dialog = activity?.let {
                Dialog(it)
            }
    
            if(dialog != null) {
                dialog.window?.requestFeature(Window.FEATURE_NO_TITLE)
                dialog.window?.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN)
                dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                dialog.setContentView(binding.root)
    
                binding.positiveButton.setOnClickListener {
                    startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
                }
                binding.closeButton.setOnClickListener {
                    dismiss()
                }
    
                binding.dialogMessage.text = when (wifiErrorType) {
                    1 ->  getString(R.string.connection_dialog_op1)
                    2 -> getString(R.string.connection_dialog_op2)
                    3 -> getString(R.string.connection_dialog_op3)
                    else -> getString(R.string.error)
                }
    
            }
            return dialog!!
        }
    
        override fun onDestroyView() {
            super.onDestroyView()
            _binding = null
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多