【问题标题】:Open another BottomSheetDialogFragment from another BottomSheetDialogFragment从另一个 BottomSheetDialogFragment 打开另一个 BottomSheetDialogFragment
【发布时间】:2021-10-05 08:50:48
【问题描述】:

点击视图时,我打开了BottomSheetDialogFragment。单击此片段中的视图时,我需要打开另一个 BottomSheetDialogFragment

MyAdapter.kt:

class MyAdapter : RecyclerView.Adapter<MyAdapter.ViewHolder>() {
  ...
  var fManager: FragmentManager? = null

  inner class ViewHolder(private val binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) {

    init {
      binding.root.setOnClickListener {
        fManager?.let { manager ->
          Dialog1().apply {
            show(manager, tag)
          }
        }
      }
    }

    ...
  }

  ...
}

这是我的 Dialog1.kt:

class Dialog1 : BottomSheetDialogFragment() {
  private lateinit var binding: Dialog1Binding

  override fun onCreateView(...) {...}

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    binding.primaryButton.setOnClickListener {
      Dialog2().apply {
        show(childFragmentManager, tag)
      }
      dismiss()
    }
  }
}

在 RecyclerView 上,Dialog1 按预期打开,但当我尝试打开 Dialog2 时,出现以下错误:

Fragment Dialog2...尚未附加。

我该如何解决这个问题?

【问题讨论】:

    标签: android kotlin android-fragments


    【解决方案1】:

    所以经过大量的试验和错误,我发现解决方案是当我编写这样的代码时:

    binding.primaryButton.setOnClickListener {
      Dialog2().let {
        it.show(parentFragmentManager, it.tag)
      }
      dismiss()
    }
    

    但是我最初使用的 apply 块有什么问题?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-17
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多