在BottomSheetDialogFragment中添加/替换片段

问题描述 投票:0回答:1

这个代码:我在其中打开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()
}
}

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

android android-xml bottom-sheet
1个回答
5
投票

尝试调用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
    }

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

© www.soinside.com 2019 - 2024. All rights reserved.