Skip to content

[BottomSheetBehavior] specially treat child's top margin #2094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ public abstract static class BottomSheetCallback {

private static final int DEF_STYLE_RES = R.style.Widget_Design_BottomSheet_Modal;

private int requestedExpandedOffset;
int expandedOffset;

int fitToContentsOffset;
Expand Down Expand Up @@ -266,6 +267,7 @@ public abstract static class BottomSheetCallback {
private boolean nestedScrolled;

private int childHeight;
private int childTopMargin;
int parentWidth;
int parentHeight;

Expand Down Expand Up @@ -441,18 +443,22 @@ public boolean onLayoutChild(
if (parentHeight - childHeight < insetTop) {
childHeight = parentHeight;
}
fitToContentsOffset = max(0, parentHeight - childHeight);
fitToContentsOffset = max(childTopMargin, parentHeight - childHeight);
calculateExpandedOffset();
calculateHalfExpandedOffset();
calculateCollapsedOffset();

ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) child.getLayoutParams();
childTopMargin = params.topMargin;

if (state == STATE_EXPANDED) {
ViewCompat.offsetTopAndBottom(child, getExpandedOffset());
ViewCompat.offsetTopAndBottom(child, getExpandedOffset() - childTopMargin);
} else if (state == STATE_HALF_EXPANDED) {
ViewCompat.offsetTopAndBottom(child, halfExpandedOffset);
ViewCompat.offsetTopAndBottom(child, halfExpandedOffset - childTopMargin);
} else if (hideable && state == STATE_HIDDEN) {
ViewCompat.offsetTopAndBottom(child, parentHeight);
ViewCompat.offsetTopAndBottom(child, parentHeight - childTopMargin);
} else if (state == STATE_COLLAPSED) {
ViewCompat.offsetTopAndBottom(child, collapsedOffset);
ViewCompat.offsetTopAndBottom(child, collapsedOffset - childTopMargin);
} else if (state == STATE_DRAGGING || state == STATE_SETTLING) {
ViewCompat.offsetTopAndBottom(child, savedTop - child.getTop());
}
Expand Down Expand Up @@ -888,7 +894,8 @@ public void setExpandedOffset(int offset) {
if (offset < 0) {
throw new IllegalArgumentException("offset must be greater than or equal to 0");
}
this.expandedOffset = offset;
this.requestedExpandedOffset = offset;
calculateExpandedOffset();
}

/**
Expand Down Expand Up @@ -1181,8 +1188,12 @@ private void calculateCollapsedOffset() {
}
}

private void calculateExpandedOffset() {
this.expandedOffset = max(childTopMargin, requestedExpandedOffset);
}

private void calculateHalfExpandedOffset() {
this.halfExpandedOffset = (int) (parentHeight * (1 - halfExpandedRatio));
this.halfExpandedOffset = (int) ((parentHeight - childTopMargin) * (1 - halfExpandedRatio));
}

private void reset() {
Expand Down