gpt4 book ai didi

android - 在 BottomSheetDialogFragment 中添加/替换 Fragment

转载 作者:太空狗 更新时间:2023-10-29 16:10:37 26 4
gpt4 key购买 nike

这段代码:我在其中打开 bottomSheetDialogFragment,我想添加 fragment 。

我想在 bottomsheetDialogFragment 中添加多个 fragment ,但它抛出

java.lang.IllegalArgumentException: No view found for id 0x7f0a01cb

class AddNotesBottomSheetDialog : BottomSheetDialogFragment() {

private lateinit var bottomSheetDialog: BottomSheetDialog
private lateinit var bottomSheetBehavior: BottomSheetBehavior<View>

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
Log.v(LOG_TAG, "-> onCreateDialog")

bottomSheetDialog = BottomSheetDialog(context!!)

var view = View.inflate(context, R.layout.bottom_sheet_notes, null)
bindViews(view)
bottomSheetDialog.setContentView(view)
bottomSheetBehavior = BottomSheetBehavior.from(view.parent as View)
bottomSheetBehavior.isHideable = false
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED

return bottomSheetDialog
}

private fun bindViews(view: View) {
loadAddNotesFragments()

}

override fun onStart() {
super.onStart()
Log.v(LOG_TAG, "-> onStart")

bottomSheetBehavior.isHideable = false
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
if (!visible)
dialog.hide()
}


fun show(fragmentManager: FragmentManager) {
Log.v(LOG_TAG, "-> show")

visible = true
if (isAdded) {
Log.v(LOG_TAG, "-> Is already added")
dialog.show()
} else {
Log.v(LOG_TAG, "-> Not added")
show(fragmentManager, AddNotesBottomSheetDialog.LOG_TAG)
}
}

override fun onDestroyView() {
super.onDestroyView()
Log.v(LOG_TAG, "-> onDestroyView")
}

private fun loadAddNotesFragments() {

val createNoteFragment = CreateNoteFragment()
val ft = fragmentManager?.beginTransaction()
ft?.replace(R.id.placeHolderBottomSheet, createNoteFragment)
ft?.commit()
}
}

已解决:我尝试在 bottomSheetDialogFragment 中添加多个 fragment 事务,但无法在 BottomSheetDialogFragment 中执行事务,这就是为什么它通过此异常。所以我在 BottomsheetDialog 中使用了 viewPager,它的工作非常完美。

最佳答案

尝试在

中调用 loadAddNotesFragments()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
loadAddNotesFragments()
}

并尝试使用 childFragmentManager 开始事务:reference

private fun loadAddNotesFragments() {

val createNoteFragment = CreateNoteFragment()
val ft = childFragmentManager()?.beginTransaction()
ft?.replace(R.id.placeHolderBottomSheet, createNoteFragment)
ft?.commit()
}

我相信这会解决您的问题。

更新:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.bottom_sheet_notes, container, false)
}

使用它来膨胀 bottomSheet 的内容,并且

删除:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
Log.v(LOG_TAG, "-> onCreateDialog")

bottomSheetDialog = BottomSheetDialog(context!!)

var view = View.inflate(context, R.layout.bottom_sheet_notes, null)
bindViews(view)
bottomSheetDialog.setContentView(view)
bottomSheetBehavior = BottomSheetBehavior.from(view.parent as View)
bottomSheetBehavior.isHideable = false
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED

return bottomSheetDialog
}

添加:

 override fun onStart() {
super.onStart()
val bottomSheet = dialog.findViewById(android.support.design.R.id.design_bottom_sheet) as ViewGroup //FrameLayout as my
val mBehavior = BottomSheetBehavior.from(bottomSheet)

//Add Behavior logic here
}

注意:不需要覆盖 onCreateDialog() 除非你想启动你自己的对话框,即一些其他类型的对话框。

关于android - 在 BottomSheetDialogFragment 中添加/替换 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53810359/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com