How to use BottomSheetDialog?

前端 未结 5 458
暖寄归人
暖寄归人 2021-02-03 19:41

I want to try BottomSheetDialog introduced in Android Support Library 23.2 but it doesn\'t seem to work correctly. Here is what the doc says:

5条回答
  •  情深已故
    2021-02-03 20:38

    This is the layout file of BottomSheetDialog.

    
    
    
    
    
    

    Your content view is inside the view design_bottom_sheet, it will be positioned center vertically by CoordinatorLayout, and BottomSheetBehavior will offset it.

    mParentHeight = parent.getHeight();
    mMinOffset = Math.max(0, mParentHeight - child.getHeight());
    mMaxOffset = mParentHeight - mPeekHeight;
    if (mState == STATE_EXPANDED) {
        ViewCompat.offsetTopAndBottom(child, mMinOffset);
    } else if (mHideable && mState == STATE_HIDDEN) {
        ViewCompat.offsetTopAndBottom(child, mParentHeight);
    } else if (mState == STATE_COLLAPSED) {
        ViewCompat.offsetTopAndBottom(child, mMaxOffset);
    }
    

    It intented to positon design_bottom_sheet at mMaxOffset, but actually the initial getTop of the child view is not 0, but (mParentHeight - childHeight) / 2, so you view if offset more than the desired offset.

    Find the view design_bottom_sheet and set its gravity to Gravity.TOP | Gravity.CENTER_HORIZONTAL will fix it. But, if the childHeight is less than mPeekHeight, there will be blank area below you content view.

    However, if peekHeight > childHeight, the mMaxOffset will less than mMinOffset, which will cause weird behavior.

    Maybe the code should be changed to

    mMaxOffset = Math.max((mParentHeight - mPeekHeight), mMinOffset);
    

    insted of

    mMaxOffset = mParentHeight - child.getHeight();
    

提交回复
热议问题