添加/替换底部片段范围内的片段



此代码:在其中打开底部的dialogragment,我想添加片段。

我想在底部表段fragment中添加多个片段,但它会抛出

java.lang.IlgalArgumentException: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()
}
}

已解决:我尝试在底部表格范围内添加多个片段事务,但是不可能在底部表格dialogfragment中进行事务,这就是为什么通过此例外的原因。因此,我在底部表格中使用了ViewPager及其作品完美。

尝试调用

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

并尝试使用ChildFragmentManager开始交易:参考

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)
    }

用它夸大底部表的内容和

删除:

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
}

add:

 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
    }

注意:除非您要启动自己的对话,即其他类型的对话框,否则无需覆盖otCreatedialog()。

最新更新