Skip to content

Commit

Permalink
[TopAppBar] Fixed the toolbar not snap when switch to freeform window…
Browse files Browse the repository at this point in the history
…ing.

PiperOrigin-RevId: 715031294
  • Loading branch information
pekingme committed Jan 14, 2025
1 parent f4342f1 commit d4ac3d5
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/java/com/google/android/material/appbar/AppBarLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,11 @@ boolean isCollapsible() {
return (scrollFlags & SCROLL_FLAG_SCROLL) == SCROLL_FLAG_SCROLL
&& (scrollFlags & COLLAPSIBLE_FLAGS) != 0;
}

/** Returns true if the scroll flags are set to snap. */
boolean shouldSnapOnScroll() {
return (scrollFlags & SCROLL_FLAG_SNAP) == SCROLL_FLAG_SNAP;
}
}

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

if ((flags & LayoutParams.FLAG_SNAP) == LayoutParams.FLAG_SNAP) {
if (lp.shouldSnapOnScroll()) {
// We're set the snap, so animate the offset to the nearest edge
int snapTop = -offsetChild.getTop();
int snapBottom = -offsetChild.getBottom();
Expand Down Expand Up @@ -1811,9 +1816,10 @@ public boolean onLayoutChild(
// 3. non-forced pending actions
final int pendingAction = abl.getPendingAction();
if (savedState != null && (pendingAction & PENDING_ACTION_FORCE) == 0) {
final int totalScrollRange = abl.getTotalScrollRange();
if (savedState.fullyScrolled) {
// Keep fully scrolled.
setHeaderTopBottomOffset(parent, abl, -abl.getTotalScrollRange());
setHeaderTopBottomOffset(parent, abl, -totalScrollRange);
} else if (savedState.fullyExpanded) {
// Keep fully expanded.
setHeaderTopBottomOffset(parent, abl, 0);
Expand All @@ -1826,6 +1832,10 @@ public boolean onLayoutChild(
} else {
offset += Math.round(child.getHeight() * savedState.firstVisibleChildPercentageShown);
}
// If snap is set, set the offset to top or bottom.
if (((LayoutParams) child.getLayoutParams()).shouldSnapOnScroll()) {
offset = -totalScrollRange * (-offset < totalScrollRange / 2 ? 0 : 1);
}
setHeaderTopBottomOffset(parent, abl, offset);
}
} else if (pendingAction != PENDING_ACTION_NONE) {
Expand Down

0 comments on commit d4ac3d5

Please # to comment.