android - 在 BottomSheetDialogFragment 添加/替换 Fragment
问题描述
这个代码:我在其中打开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,它的工作很完美。
解决方案
尝试调用 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)
}
使用它来膨胀 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
}
注意: 不需要覆盖 onCreateDialog() 除非您希望启动自己的对话框,即其他类型的对话框。
推荐阅读
- reactjs - 通过 NextJs 导航将 props 传递给组件,无需额外的库
- c# - 如何将 azure key vault 值设置为 kuberetes 环境变量(不从文件系统中读取)
- python - 将字典键与python中的用户输入匹配
- node.js - 如何将从 response.on('end') 获得的数据发送回客户端
- javascript - Discord.js 静音命令和创建 muterole 不起作用并给出错误
- annotations - 一个spring aop切入点可以过滤注解和返回类型的组合吗?
- azure - 从公共网络/本地计算机连接到 Azure HDInsight Kafka 集群
- django - 如何控制 Django 模型中的对象数量
- javascript - JS - 中断异步执行
- android - 如何正确使用 recyclerview 实现 searchview?