Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add/Replace Fragment inside of BottomSheetDialogFragment

This this code : in which i open bottomSheetDialogFragment in that i want to add fragment.

I want to add multiple fragments in bottomsheetDialogFragment but it throws

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

Solved: I tried to add multiple fragments transaction in bottomSheetDialogFragment but it's not possible to do transaction in BottomSheetDialogFragment thats why its through this exception. so i used viewPager inside the BottomsheetDialog and its work perfect.

like image 902
khushbu Avatar asked Feb 04 '23 20:02

khushbu


1 Answers

try calling loadAddNotesFragments() in

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

And try using childFragmentManager to begin transaction's: reference

private fun loadAddNotesFragments() {

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

I believe this will solve your problem.

UPDATE:

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

use this to inflate the content of bottomSheet, and

REMOVE:

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
    }

NOTE: No need of overriding onCreateDialog() unless you want your own Dialog to be initiated, i.e some other type of dialog.

like image 150
Anmol Avatar answered Feb 06 '23 15:02

Anmol