【问题标题】:Show scrollable full screen Bottom sheet Android显示可滚动的全屏底部工作表Android
【发布时间】:2021-11-03 06:02:53
【问题描述】:

我已经反驳了需要实现可滚动模态底页的情况。基本上,它首先会显示一半的屏幕,然后向上滚动时,它会拉伸到屏幕的 90%。如果继续向上滑动,它将滚动底部表格内的内容。

我的尝试

layout.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/design_bottom_sheet"
        style="@style/RootLayout"
        android:background="@drawable/rounded_shape"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

        <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <...Content.../>

        </android.core.widget.NestedScrollView>
    </FrameLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout

底页对话框类

我在accepted answer这里关注了答案

结果

打开时它只显示一半的屏幕,当我像这样result 向上滚动内容时,不会将底部的工作表滚动到展开状态。

期待

我希望上面的代码应该像这样expectation

有没有人有解决这种情况的方法? 谢谢

【问题讨论】:

    标签: android bottom-sheet


    【解决方案1】:

    于是一个多星期后,我找到了解决这个问题的方法, 我重写了 onCreateDialog 方法,如下所示

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        // This will disable the behavior of bottom sheet to have a flat corner
        //https://github.com/material-components/material-components-android/pull/437#issuecomment-678742683
    
        val dialog = BottomSheetDialog(requireContext(), theme)
        dialog.behavior.disableShapeAnimations()
    
        dialog.setOnShowListener { dialogInterface ->
    
            val bottomSheetDialog = dialogInterface as BottomSheetDialog
            val parentLayout =
                bottomSheetDialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
            parentLayout?.let { it1 ->
                val behaviour = BottomSheetBehavior.from(it1)
                it1.layoutParams.also {
                    it.height =
                        context?.resources?.displayMetrics?.heightPixels?.times(0.9)?.toInt()!!
                }
                behaviour.peekHeight =
                    context?.resources?.displayMetrics?.heightPixels?.times(0.6)?.toInt()!!
            }
        }
        return dialog
    }
    

    这段代码发生了什么?

    我没有使用 BottomSheetDialog 的对话框提供程序,也没有 super.onCreateDialog()

    相反,我使用了 BottomSheetDialog(context,theme) 来创建我的自定义底部工作表主题。

    然后,我为对话框设置监听器,获取父布局和底部工作表对话框,如下所示。最重要的部分是处理行为。

    正确实现行为后,您就完成了。按照@Nitish Chaudhary 的建议将其状态设置为展开,并将布局高度设置为您想要的高度。这就是我所需要的,无论您遵循模态底部工作表 XML 布局规则,下面的这段代码也可以重复使用。

    编码愉快。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      • 2021-09-16
      • 1970-01-01
      • 2020-12-11
      • 2022-10-10
      • 2019-03-19
      • 2021-03-18
      相关资源
      最近更新 更多