Setting maximum expanded height for bottomsheet dynamically

痞子三分冷 提交于 2019-12-07 08:37:22

Finally found it,

This question troubled me a lot with no solution reported anywhere, and the answer lies in the behavior itself.

The minimum offset is the max value upto which the bottomsheet should move and we set the lower cap of the value to our desired height upto which we want the bottomsheet to move. You can expose a function to set the value or do it direclty in our behavior.

To dynamically set the max expanded height for bottomsheet we need to increase the minimum offset value from 0 to our desired value in BottomSheetBehavior class, let me show the code.

Happy coding!!

         // 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);
               } 
        }

The simplest solution is to set the maxHeight property of the bottom sheet like this.

DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
 bottomSheet.setMaxHeight((int) (displayMetrics.heightPixels * 0.65));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!