在BottomSheetDialogFragment内部添加/替换片段

khu*_*hbu 4 android android-xml bottom-sheet

此代码:在其中我要添加片段的地方打开bottomSheetDialogFragment。

我想在bottomsheetDialogFragment中添加多个片段,但是会抛出

java.lang.IllegalArgumentException:找不到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()
}
}
Run Code Online (Sandbox Code Playgroud)

已解决:我试图在bottomSheetDialogFragment中添加多个片段事务,但是无法在BottomSheetDialogFragment中进行事务,这就是其通过此异常的原因。所以我在BottomsheetDialog内部使用了viewPager,它的工作非常完美。

Anm*_*mol 7

尝试在中调用loadAddNotesFragments()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
     loadAddNotesFragments()
}
Run Code Online (Sandbox Code Playgroud)

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

private fun loadAddNotesFragments() {

    val createNoteFragment = CreateNoteFragment()
    val ft = childFragmentManager()?.beginTransaction()
    ft?.replace(R.id.placeHolderBottomSheet, createNoteFragment)
    ft?.commit()
}
Run Code Online (Sandbox Code Playgroud)

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

更新:

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

使用它来填充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
}
Run Code Online (Sandbox Code Playgroud)

加:

 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
    }
Run Code Online (Sandbox Code Playgroud)

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