Skip to content

Commit

Permalink
[CollapsingToolbarLayout] Refactor multiline rotation fix to address …
Browse files Browse the repository at this point in the history
…related issue caused by keyboard

PiperOrigin-RevId: 645057096
  • Loading branch information
dsn5ft authored and kendrickumstattd committed Jun 20, 2024
1 parent 51e5cec commit 5371290
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/java/com/google/android/material/appbar/AppBarLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,10 @@ int getPendingAction() {
return pendingAction;
}

void setPendingAction(int pendingAction) {
this.pendingAction = pendingAction;
}

void resetPendingAction() {
pendingAction = PENDING_ACTION_NONE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ public class CollapsingToolbarLayout extends FrameLayout {

int currentOffset;

private int screenOrientation;

@TitleCollapseMode private int titleCollapseMode;

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

screenOrientation = getResources().getConfiguration().orientation;

collapsingTextHelper = new CollapsingTextHelper(this);
collapsingTextHelper.setTextSizeInterpolator(AnimationUtils.DECELERATE_INTERPOLATOR);
collapsingTextHelper.setRtlTextDirectionHeuristicsEnabled(false);
Expand Down Expand Up @@ -449,6 +453,25 @@ && isTitleCollapseFadeMode()
protected void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
collapsingTextHelper.maybeUpdateFontWeightAdjustment(newConfig);

// When the orientation changes with extra multiline height enabled and when collapsed, there
// can be an issue where the offset/scroll state is invalid due to the number of lines of text
// changing which causes a different height for the collapsing toolbar. We can use a pending
// action of collapsed to make sure that the collapsing toolbar stays fully collapsed if it was
// fully collapsed prior to screen rotation.
if (screenOrientation != newConfig.orientation
&& extraMultilineHeightEnabled
&& collapsingTextHelper.getExpansionFraction() == 1f) {
ViewParent parent = getParent();
if (parent instanceof AppBarLayout) {
AppBarLayout appBarLayout = (AppBarLayout) parent;
if (appBarLayout.getPendingAction() == AppBarLayout.PENDING_ACTION_NONE) {
appBarLayout.setPendingAction(AppBarLayout.PENDING_ACTION_COLLAPSED);
}
}
}

screenOrientation = newConfig.orientation;
}

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

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

0 comments on commit 5371290

Please sign in to comment.