Skip to content

Commit 2bfc7ba

Browse files
drchendsn5ft
authored andcommitted
[SnackBar] Fix margins are added multiple times when show() is called
When a snack bar is hidden and its show() method is called, it will be added to the target parent, and at this moment the parent view will set the layout params to the snack bar, with the same existing margins if any. Therefore if the margins are already updated with extra margins, the original margins will be incorrectly updated. This CL introduces a flag to tells this situation from other "real" scenarios in which clients want to update their custom margins. Resolves #2666 PiperOrigin-RevId: 445159923 (cherry picked from commit 46fa8cc)
1 parent d6cd0e6 commit 2bfc7ba

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

lib/java/com/google/android/material/snackbar/BaseTransientBottomBar.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ final void showView() {
712712
setUpBehavior((CoordinatorLayout.LayoutParams) lp);
713713
}
714714

715-
targetParent.addView(this.view);
715+
this.view.addToTargetParent(targetParent);
716716
recalculateAndUpdateMargins();
717717

718718
// Set view to INVISIBLE so it doesn't flash on the screen before the inset adjustment is
@@ -1111,6 +1111,7 @@ public boolean onTouch(View v, MotionEvent event) {
11111111
private PorterDuff.Mode backgroundTintMode;
11121112

11131113
@Nullable private Rect originalMargins;
1114+
private boolean addingToTargetParent;
11141115

11151116
protected SnackbarBaseLayout(@NonNull Context context) {
11161117
this(context, null);
@@ -1233,7 +1234,10 @@ protected void onDetachedFromWindow() {
12331234
@Override
12341235
public void setLayoutParams(ViewGroup.LayoutParams params) {
12351236
super.setLayoutParams(params);
1236-
if (params instanceof MarginLayoutParams) {
1237+
if (!addingToTargetParent && params instanceof MarginLayoutParams) {
1238+
// Do not update the original margins when the layout is being added to its target parent,
1239+
// since the margins are just copied from the existing layout params, which can already be
1240+
// updated with extra margins.
12371241
updateOriginalMargins((MarginLayoutParams) params);
12381242
if (baseTransientBottomBar != null) {
12391243
baseTransientBottomBar.updateMargins();
@@ -1266,6 +1270,12 @@ int getMaxInlineActionWidth() {
12661270
return maxInlineActionWidth;
12671271
}
12681272

1273+
void addToTargetParent(ViewGroup targetParent) {
1274+
addingToTargetParent = true;
1275+
targetParent.addView(this);
1276+
addingToTargetParent = false;
1277+
}
1278+
12691279
private void setBaseTransientBottomBar(BaseTransientBottomBar<?> baseTransientBottomBar) {
12701280
this.baseTransientBottomBar = baseTransientBottomBar;
12711281
}

0 commit comments

Comments
 (0)