BottomSheetDialogFragment没有显示出来。

7

我按照这篇教程在我的安卓应用中实现了BottomSheetDiaogFragment。

以下是我的底部弹出布局(bottom_sheet.xml):

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<RadioGroup
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <RadioButton
        android:id="@+id/rb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_weight="1"
        android:text="@string/rb1" />

    <RadioButton
        android:id="@+id/rb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_weight="1"
        android:text="@string/rb2" />

</RadioGroup>
</android.support.constraint.ConstraintLayout>

BottomSheetDialogFragment 类:

class BottomSheetTaskRepeat : BottomSheetDialogFragment() {

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

活动:

private val bottomSheetTaskRepeat = BottomSheetTaskRepeat()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    bottomSheetTaskRepeat.show(supportFragmentManager, "my_bottom_sheet")
}

问题是底部工作表没有显示出来!非常感谢您的帮助。

你能否设置一个断点来检查是否执行到了 bottomSheetTaskRepeat.show..?因为我运行了你的代码,它完美地工作了。 - May Rest in Peace
最好检查底部对话框片段中是否有任何 dismiss() 调用。还要在调用它的地方进行检查。有时突然的解散看起来也是这样。 - Reejesh PK
3个回答

5
这是一篇晚期回答,我为那些可能遇到同样问题的人写下这篇文章。以下是我的发现:
由于某些原因,在BottomSheetDialogFragment中,非约束视图高度无法正常工作。像wrap_content这样高度的视图将不会显示(但阴影会存在),但当你指定其高度如80dp时,它就可以正常工作。
对于这个问题,请更改你的RadioGroup高度并将其指定为:
android:layout_height="200dp"

希望有所帮助。
更新: 由于BottomSheetDailogFragment的默认容器为FrameLayout并设置为WRAP_CONTENT,您可以在Fragment的onStart方法中覆盖它,例如(Kotlin):
override fun onStart() {
    super.onStart()
    val containerID = com.google.android.material.R.id.design_bottom_sheet
    val bottomSheet: FrameLayout? = dialog?.findViewById(containerID)
    bottomSheet?.let {
        BottomSheetBehavior.from<FrameLayout?>(it).state =
            BottomSheetBehavior.STATE_HALF_EXPANDED
        bottomSheet.layoutParams.height = FrameLayout.LayoutParams.MATCH_PARENT
    }

    view?.post {
        val params = (view?.parent as View).layoutParams as (CoordinatorLayout.LayoutParams)
        val behavior = params.behavior
        val bottomSheetBehavior = behavior as (BottomSheetBehavior)
        bottomSheetBehavior.peekHeight = view?.measuredHeight ?: 0
        (bottomSheet?.parent as? View)?.setBackgroundColor(Color.TRANSPARENT)

    }
}

2

请尝试以下操作:

  1. BottomSheetDialogFragment中创建并使用无参数静态方法newInstance,并调用show()方法。
  2. 尝试使用LinearLayoutCompat作为根布局,而非ConstraintLayout
  3. 尝试使用丰富多彩的背景色来获得灵感。
  4. 将根布局的高度设置为match_parent
  5. 确保不会立即调用dismiss()cancel()方法。
  6. 检查可见性。
  7. 如果包含recyclerView,请确保其具有项目,getItemCount未返回0,并且我们正确设置了值!
  8. 如果由于Android Studio错误而无法反映更改,请同时重新启动PC和设备。

0

如果你从内部片段进行填充,就必须使用 childFragmentManager 而不是 supportFragmentManager

private val bottomSheetTaskRepeat = BottomSheetTaskRepeat()
bottomSheetTaskRepeat.show(childFragmentManager, "my_bottom_sheet")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接