From b935604e2c9192e3ad3f9e3f5f2ebf93086d2ea0 Mon Sep 17 00:00:00 2001 From: Khang NT Date: Mon, 1 Nov 2021 15:45:04 +0000 Subject: [PATCH] [AppBarLayout] Fix snap issue with transparent status bar Resolves https://github.com/material-components/material-components-android/pull/2387 GIT_ORIGIN_REV_ID=47538c3b4cb10337c537d1ef532bd1d73b216ae3 PiperOrigin-RevId: 406835368 --- .../android/material/appbar/AppBarLayout.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/java/com/google/android/material/appbar/AppBarLayout.java b/lib/java/com/google/android/material/appbar/AppBarLayout.java index 4094528bea8..de6f034f987 100644 --- a/lib/java/com/google/android/material/appbar/AppBarLayout.java +++ b/lib/java/com/google/android/material/appbar/AppBarLayout.java @@ -1526,10 +1526,11 @@ public void onAnimationUpdate(@NonNull ValueAnimator animator) { } private int getChildIndexOnOffset(@NonNull T abl, final int offset) { + final int ablTopInset = abl.getTopInset() + abl.getPaddingTop(); for (int i = 0, count = abl.getChildCount(); i < count; i++) { View child = abl.getChildAt(i); - int top = child.getTop(); - int bottom = child.getBottom(); + int top = child.getTop() - ablTopInset; + int bottom = child.getBottom() - ablTopInset; final LayoutParams lp = (LayoutParams) child.getLayoutParams(); if (checkFlag(lp.getScrollFlags(), LayoutParams.SCROLL_FLAG_SNAP_MARGINS)) { @@ -1552,16 +1553,12 @@ private void snapToChildIfNeeded(CoordinatorLayout coordinatorLayout, @NonNull T final View offsetChild = abl.getChildAt(offsetChildIndex); final LayoutParams lp = (LayoutParams) offsetChild.getLayoutParams(); final int flags = lp.getScrollFlags(); + final int ablTopInset = abl.getTopInset() + abl.getPaddingTop(); if ((flags & LayoutParams.FLAG_SNAP) == LayoutParams.FLAG_SNAP) { // We're set the snap, so animate the offset to the nearest edge - int snapTop = -offsetChild.getTop(); - int snapBottom = -offsetChild.getBottom(); - - if (offsetChildIndex == abl.getChildCount() - 1) { - // If this is the last child, we need to take the top inset and padding into account - snapBottom += abl.getTopInset() + abl.getPaddingTop(); - } + int snapTop = -offsetChild.getTop() + ablTopInset; + int snapBottom = -offsetChild.getBottom() + ablTopInset; if (checkFlag(flags, LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED)) { // If the view is set only exit until it is collapsed, we'll abide by that