Skip to content

Commit d227870

Browse files
Material Design Teampekingme
authored andcommitted
[CollapsingToolbarLayout] Fix jumping when multiline title changes while collapsed
When a multiline title changes length (e.g. multi-line to single-line) while collapsed, the layout height decreases. If the previous scroll offset is deeper than the new valid range, the AppBarLayout can jump or get stuck in an invalid state. This fix forces the AppBarLayout to `PENDING_ACTION_COLLAPSED` if the CollapsingToolbarLayout height changes while fully collapsed. This ensures it snaps correctly to the new collapsed offset. PiperOrigin-RevId: 861133526
1 parent 85c910f commit d227870

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -545,18 +545,22 @@ protected void onConfigurationChanged(@NonNull Configuration newConfig) {
545545
if (screenOrientation != newConfig.orientation
546546
&& extraMultilineHeightEnabled
547547
&& collapsingTitleHelper.getExpansionFraction() == 1f) {
548-
ViewParent parent = getParent();
549-
if (parent instanceof AppBarLayout) {
550-
AppBarLayout appBarLayout = (AppBarLayout) parent;
551-
if (appBarLayout.getPendingAction() == AppBarLayout.PENDING_ACTION_NONE) {
552-
appBarLayout.setPendingAction(AppBarLayout.PENDING_ACTION_COLLAPSED);
553-
}
554-
}
548+
maybeSetPendingActionCollapsed();
555549
}
556550

557551
screenOrientation = newConfig.orientation;
558552
}
559553

554+
private void maybeSetPendingActionCollapsed() {
555+
ViewParent parent = getParent();
556+
if (parent instanceof AppBarLayout) {
557+
AppBarLayout appBarLayout = (AppBarLayout) parent;
558+
if (appBarLayout.getPendingAction() == AppBarLayout.PENDING_ACTION_NONE) {
559+
appBarLayout.setPendingAction(AppBarLayout.PENDING_ACTION_COLLAPSED);
560+
}
561+
}
562+
}
563+
560564
@Override
561565
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
562566
// This is a little weird. Our scrim needs to be behind the Toolbar (if it is present),
@@ -771,6 +775,16 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
771775
setMinimumHeight(getHeightWithMargins(toolbarDirectChild));
772776
}
773777
}
778+
779+
// When extra multiline height is enabled, the height of the CollapsingToolbarLayout can change
780+
// dynamically. If the height changes while the view is collapsed, we need to ensure that the
781+
// AppBarLayout updates its collapse state to match the new height, otherwise it may try to
782+
// scroll to an invalid offset based on the old height.
783+
if (extraMultilineHeightEnabled
784+
&& collapsingTitleHelper.getExpandedMaxLines() > 1
785+
&& collapsingTitleHelper.getExpansionFraction() == 1f) {
786+
maybeSetPendingActionCollapsed();
787+
}
774788
}
775789

776790
@Override

0 commit comments

Comments
 (0)