@@ -186,6 +186,8 @@ public class CollapsingToolbarLayout extends FrameLayout {
186
186
187
187
int currentOffset ;
188
188
189
+ private int screenOrientation ;
190
+
189
191
@ TitleCollapseMode private int titleCollapseMode ;
190
192
191
193
@ Nullable WindowInsetsCompat lastInsets ;
@@ -208,6 +210,8 @@ public CollapsingToolbarLayout(@NonNull Context context, @Nullable AttributeSet
208
210
// Ensure we are using the correctly themed context rather than the context that was passed in.
209
211
context = getContext ();
210
212
213
+ screenOrientation = getResources ().getConfiguration ().orientation ;
214
+
211
215
collapsingTextHelper = new CollapsingTextHelper (this );
212
216
collapsingTextHelper .setTextSizeInterpolator (AnimationUtils .DECELERATE_INTERPOLATOR );
213
217
collapsingTextHelper .setRtlTextDirectionHeuristicsEnabled (false );
@@ -449,6 +453,25 @@ && isTitleCollapseFadeMode()
449
453
protected void onConfigurationChanged (@ NonNull Configuration newConfig ) {
450
454
super .onConfigurationChanged (newConfig );
451
455
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 ;
452
475
}
453
476
454
477
@ Override
@@ -599,7 +622,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
599
622
updateTextBounds (0 , 0 , getMeasuredWidth (), getMeasuredHeight (), /* forceRecalculate= */ true );
600
623
601
624
int lineCount = collapsingTextHelper .getExpandedLineCount ();
602
- if (lineCount > 1 && collapsingTextHelper . getExpansionFraction () == 0f ) {
625
+ if (lineCount > 1 ) {
603
626
// Add extra height based on the amount of height beyond the first line of title text.
604
627
int expandedTextHeight = Math .round (collapsingTextHelper .getExpandedTextFullHeight ());
605
628
extraMultilineHeight = expandedTextHeight * (lineCount - 1 );
0 commit comments