From 19af0ac9d98cc3c504ce69ae38fbc00570cc85f5 Mon Sep 17 00:00:00 2001 From: conradchen Date: Wed, 16 Feb 2022 17:46:08 +0000 Subject: [PATCH] [BottomSheet] Update edge-to-edge paddings when bottom sheet is laid out Since adding bottom sheet behavior callbacks won't trigger any callbacks to be called, the bottom sheet paddings won't be updated right away. This causes the issue that top paddings are not correctly applied when the bottom sheet is initially expanded. Besides that, due to some unknown reason, bottom sheet's getTop() method will only return a non-zero value that is the offset to the container top when calling from bottom sheet behavior callbacks. It seems related to how ViewCompat.offsetTopAndBottom() is working. Not 100% sure. Therefore this CL add a internal callback to BottomSheetCallback class so when bottom sheet is being laid out (and getting offset), we can update paddings right away. Resolves https://github.com/material-components/material-components-android/issues/2165 PiperOrigin-RevId: 429071367 --- .../android/material/bottomsheet/BottomSheetBehavior.java | 6 ++++++ .../android/material/bottomsheet/BottomSheetDialog.java | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java b/lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java index 36e54be0988..3b1463f5b0a 100644 --- a/lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java +++ b/lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java @@ -107,6 +107,8 @@ public abstract static class BottomSheetCallback { * expanded states and from -1 to 0 it is between hidden and collapsed states. */ public abstract void onSlide(@NonNull View bottomSheet, float slideOffset); + + void onLayout(@NonNull View bottomSheet) {} } /** The bottom sheet is dragging. */ @@ -553,6 +555,10 @@ public boolean onLayoutChild( } nestedScrollingChildRef = new WeakReference<>(findScrollingChild(child)); + + for (int i = 0; i < callbacks.size(); i++) { + callbacks.get(i).onLayout(child); + } return true; } diff --git a/lib/java/com/google/android/material/bottomsheet/BottomSheetDialog.java b/lib/java/com/google/android/material/bottomsheet/BottomSheetDialog.java index 262692f33ad..ee4dd7f4777 100644 --- a/lib/java/com/google/android/material/bottomsheet/BottomSheetDialog.java +++ b/lib/java/com/google/android/material/bottomsheet/BottomSheetDialog.java @@ -452,6 +452,11 @@ public void onSlide(@NonNull View bottomSheet, float slideOffset) { setPaddingForPosition(bottomSheet); } + @Override + void onLayout(@NonNull View bottomSheet) { + setPaddingForPosition(bottomSheet); + } + private void setPaddingForPosition(View bottomSheet) { if (bottomSheet.getTop() < insetsCompat.getSystemWindowInsetTop()) { // If the bottomsheet is light, we should set light status bar so the icons are visible