Добавить / заменить фрагмент внутри BottomSheetDialogFragment - PullRequest
0 голосов
/ 17 декабря 2018

Это этот код: в котором я открываю bottomSheetDialogFragment, в котором я хочу добавить фрагмент.

Я хочу добавить несколько фрагментов в bottomomsheetDialogFragment, но он выдает

java.lang.IllegalArgumentException: Нет представления для идентификатора 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, поэтому его можно использовать только в этом исключении.поэтому я использовал viewPager внутри BottomsheetDialog и его работа идеально.

1 Ответ

0 голосов
/ 17 декабря 2018

попробуйте вызвать 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
}

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
    }

ПРИМЕЧАНИЕ: Нет необходимости переопределять onCreateDialog (), если вы не хотите, чтобы ваш собственный диалог был инициирован, т.е. некоторыедругой тип диалога.

...