动态设置 bottomsheet 的最大展开高度

Setting maximum expanded height for bottomsheet dynamically

这个问题是对上面问题的扩展,我想设置 sheet 的最大展开高度,但是根据屏幕大小动态设置。

我已经尝试为实现底部sheet行为的视图设置新的布局参数,但它没有任何好处。

终于找到了,

这个问题很困扰我,没有任何解决方案,答案在于行为本身。

最小偏移量是 bottomsheet 应该移动到的最大值,我们将值的下限设置为我们希望 bottomsheet 移动到的所需高度。 您可以公开一个函数来设置值或直接在我们的行为中执行。

要动态设置 bottomsheet 的最大展开高度,我们需要在 BottomSheetBehavior class 中将最小偏移值从 0 增加到我们想要的值,让我展示一下代码。

编码愉快!!

         // The minimum offset value upto which your bottomsheet to move
         private int mMinOffset;

        /**
          * Called when the parent CoordinatorLayout is about the layout the given child view.
          */
        @Override
        public boolean onLayoutChild(CoordinatorLayout parent, V child, int layoutDirection) {
               int dynamicHeight = Utils.dpToPx(parent.getContext(), **your_value_in_dp**);
               mMinOffset = Math.max(dynamicHeight, mParentHeight - child.getHeight());
               mMaxOffset = Math.max(mParentHeight - mPeekHeight, mMinOffset);
               mAnchorOffset = Math.min(mParentHeight - mAnchorHeight, mMaxOffset);

               if (mState == STATE_EXPANDED) {
                    ViewCompat.offsetTopAndBottom(child, mMinOffset);
                    anchorViews(mMinOffset);
               } 
        }

最简单的解决方案是像这样设置底部 sheet 的 maxHeight 属性。

DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
 bottomSheet.setMaxHeight((int) (displayMetrics.heightPixels * 0.65));

2021 我来晚了,但有人需要 Kotlin 扩展:

fun View.setupFullHeight(maxHeight: Double = 0.3) {
val displayMetrics = context?.resources?.displayMetrics
val height = displayMetrics?.heightPixels
val maximalHeight = (height?.times(maxHeight))?.toInt()
val layoutParams = this.layoutParams
maximalHeight?.let {
    layoutParams.height = it
}
this.layoutParams = layoutParams

}

使用方法:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    return object : BottomSheetDialog(requireContext(), R.style.DialogRoundedCornerStyle) {

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

            dialog?.setOnShowListener {
                val bottomSheetDialog = it as BottomSheetDialog
                val parentLayout =
                    bottomSheetDialog.findViewById<View>(R.id.design_bottom_sheet)
                parentLayout?.let { view ->
                    val behavior = BottomSheetBehavior.from(view)
                    view.setupFullHeight()
                    behavior.apply {
                        state = BottomSheetBehavior.STATE_EXPANDED
                        isDraggable = false
                        isCancelable = false
                    }
                }
            }
        }

        override fun onBackPressed() {
            super.onBackPressed()
            dialog?.dismiss()
        }
    }
}

请用这个冷静一下:)

  1. const val BOTTOMSHEET_HEIGHT_TO_SCREEN_HEIGHT_RATIO = 0.80 //根据你的要求改变
  2. 覆盖 bottomsheetFragment 中的 onCreateDialog()

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
        dialog.setOnShowListener {
            dialog.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
                ?.apply {
                    val maxDesiredHeight =
                        (resources.displayMetrics.heightPixels * BOTTOMSHEET_HEIGHT_TO_SCREEN_HEIGHT_RATIO).toInt()
                    if (this.height > maxDesiredHeight) {
                        val bottomSheetLayoutParams = this.layoutParams
                        bottomSheetLayoutParams.height = maxDesiredHeight
                        this.layoutParams = bottomSheetLayoutParams
                    }
                    BottomSheetBehavior.from(this)?.apply {
                        this.state = BottomSheetBehavior.STATE_EXPANDED
                        this.skipCollapsed = true
                    }
                }
        }
        return dialog
    }