Skip to content

Commit 5371290

Browse files
dsn5ftkendrickumstattd
authored andcommitted
[CollapsingToolbarLayout] Refactor multiline rotation fix to address related issue caused by keyboard
PiperOrigin-RevId: 645057096
1 parent 51e5cec commit 5371290

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,10 @@ int getPendingAction() {
11741174
return pendingAction;
11751175
}
11761176

1177+
void setPendingAction(int pendingAction) {
1178+
this.pendingAction = pendingAction;
1179+
}
1180+
11771181
void resetPendingAction() {
11781182
pendingAction = PENDING_ACTION_NONE;
11791183
}

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ public class CollapsingToolbarLayout extends FrameLayout {
186186

187187
int currentOffset;
188188

189+
private int screenOrientation;
190+
189191
@TitleCollapseMode private int titleCollapseMode;
190192

191193
@Nullable WindowInsetsCompat lastInsets;
@@ -208,6 +210,8 @@ public CollapsingToolbarLayout(@NonNull Context context, @Nullable AttributeSet
208210
// Ensure we are using the correctly themed context rather than the context that was passed in.
209211
context = getContext();
210212

213+
screenOrientation = getResources().getConfiguration().orientation;
214+
211215
collapsingTextHelper = new CollapsingTextHelper(this);
212216
collapsingTextHelper.setTextSizeInterpolator(AnimationUtils.DECELERATE_INTERPOLATOR);
213217
collapsingTextHelper.setRtlTextDirectionHeuristicsEnabled(false);
@@ -449,6 +453,25 @@ && isTitleCollapseFadeMode()
449453
protected void onConfigurationChanged(@NonNull Configuration newConfig) {
450454
super.onConfigurationChanged(newConfig);
451455
collapsingTextHelper.maybeUpdateFontWeightAdjustment(newConfig);
456+
457+
// When the orientation changes with extra multiline height enabled and when collapsed, there
458+
// can be an issue where the offset/scroll state is invalid due to the number of lines of text
459+
// changing which causes a different height for the collapsing toolbar. We can use a pending
460+
// action of collapsed to make sure that the collapsing toolbar stays fully collapsed if it was
461+
// fully collapsed prior to screen rotation.
462+
if (screenOrientation != newConfig.orientation
463+
&& extraMultilineHeightEnabled
464+
&& collapsingTextHelper.getExpansionFraction() == 1f) {
465+
ViewParent parent = getParent();
466+
if (parent instanceof AppBarLayout) {
467+
AppBarLayout appBarLayout = (AppBarLayout) parent;
468+
if (appBarLayout.getPendingAction() == AppBarLayout.PENDING_ACTION_NONE) {
469+
appBarLayout.setPendingAction(AppBarLayout.PENDING_ACTION_COLLAPSED);
470+
}
471+
}
472+
}
473+
474+
screenOrientation = newConfig.orientation;
452475
}
453476

454477
@Override
@@ -599,7 +622,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
599622
updateTextBounds(0, 0, getMeasuredWidth(), getMeasuredHeight(), /* forceRecalculate= */ true);
600623

601624
int lineCount = collapsingTextHelper.getExpandedLineCount();
602-
if (lineCount > 1 && collapsingTextHelper.getExpansionFraction() == 0f) {
625+
if (lineCount > 1) {
603626
// Add extra height based on the amount of height beyond the first line of title text.
604627
int expandedTextHeight = Math.round(collapsingTextHelper.getExpandedTextFullHeight());
605628
extraMultilineHeight = expandedTextHeight * (lineCount - 1);

0 commit comments

Comments
 (0)