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