Skip to content

Commit d4ac3d5

Browse files
committed
[TopAppBar] Fixed the toolbar not snap when switch to freeform windowing.
PiperOrigin-RevId: 715031294
1 parent f4342f1 commit d4ac3d5

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

lib/java/com/google/android/material/appbar/AppBarLayout.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,11 @@ boolean isCollapsible() {
14561456
return (scrollFlags & SCROLL_FLAG_SCROLL) == SCROLL_FLAG_SCROLL
14571457
&& (scrollFlags & COLLAPSIBLE_FLAGS) != 0;
14581458
}
1459+
1460+
/** Returns true if the scroll flags are set to snap. */
1461+
boolean shouldSnapOnScroll() {
1462+
return (scrollFlags & SCROLL_FLAG_SNAP) == SCROLL_FLAG_SNAP;
1463+
}
14591464
}
14601465

14611466
/**
@@ -1720,7 +1725,7 @@ private void snapToChildIfNeeded(CoordinatorLayout coordinatorLayout, @NonNull T
17201725
final LayoutParams lp = (LayoutParams) offsetChild.getLayoutParams();
17211726
final int flags = lp.getScrollFlags();
17221727

1723-
if ((flags & LayoutParams.FLAG_SNAP) == LayoutParams.FLAG_SNAP) {
1728+
if (lp.shouldSnapOnScroll()) {
17241729
// We're set the snap, so animate the offset to the nearest edge
17251730
int snapTop = -offsetChild.getTop();
17261731
int snapBottom = -offsetChild.getBottom();
@@ -1811,9 +1816,10 @@ public boolean onLayoutChild(
18111816
// 3. non-forced pending actions
18121817
final int pendingAction = abl.getPendingAction();
18131818
if (savedState != null && (pendingAction & PENDING_ACTION_FORCE) == 0) {
1819+
final int totalScrollRange = abl.getTotalScrollRange();
18141820
if (savedState.fullyScrolled) {
18151821
// Keep fully scrolled.
1816-
setHeaderTopBottomOffset(parent, abl, -abl.getTotalScrollRange());
1822+
setHeaderTopBottomOffset(parent, abl, -totalScrollRange);
18171823
} else if (savedState.fullyExpanded) {
18181824
// Keep fully expanded.
18191825
setHeaderTopBottomOffset(parent, abl, 0);
@@ -1826,6 +1832,10 @@ public boolean onLayoutChild(
18261832
} else {
18271833
offset += Math.round(child.getHeight() * savedState.firstVisibleChildPercentageShown);
18281834
}
1835+
// If snap is set, set the offset to top or bottom.
1836+
if (((LayoutParams) child.getLayoutParams()).shouldSnapOnScroll()) {
1837+
offset = -totalScrollRange * (-offset < totalScrollRange / 2 ? 0 : 1);
1838+
}
18291839
setHeaderTopBottomOffset(parent, abl, offset);
18301840
}
18311841
} else if (pendingAction != PENDING_ACTION_NONE) {

0 commit comments

Comments
 (0)