From 2f3110ff5d67dc46308621e5bf63d4dae9b2e04b Mon Sep 17 00:00:00 2001 From: conradchen Date: Mon, 11 Apr 2022 15:06:33 -0400 Subject: [PATCH] [TopAppBar] Fix app:expanded=false not working When an AppBarLayout is loaded, it will load the attribute and set up a non-forcing pending action. However when it's measured, the current scroll state will be saved and restored, which will override the non-forcing pending action. This behavior is incorrect - if there's a pending action, we shouldn't restore the scrolling state because it's meant to be changed. Resolves https://github.com/material-components/material-components-android/issues/2629 PiperOrigin-RevId: 440960254 (cherry picked from commit 05be1b92c5720e4c6c6f42f93918bd6554099625) --- .../com/google/android/material/appbar/AppBarLayout.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/java/com/google/android/material/appbar/AppBarLayout.java b/lib/java/com/google/android/material/appbar/AppBarLayout.java index 085356b7ba3..f775bb5f4f0 100644 --- a/lib/java/com/google/android/material/appbar/AppBarLayout.java +++ b/lib/java/com/google/android/material/appbar/AppBarLayout.java @@ -537,7 +537,12 @@ private boolean hasCollapsibleChild() { private void invalidateScrollRanges() { // Saves the current scrolling state when we need to recalculate scroll ranges - SavedState savedState = behavior == null || totalScrollRange == INVALID_SCROLL_RANGE + // If the total scroll range is not known yet, the ABL is never scrolled. + // If there's a pending action, we should skip this step and respect the pending action. + SavedState savedState = + behavior == null + || totalScrollRange == INVALID_SCROLL_RANGE + || pendingAction != PENDING_ACTION_NONE ? null : behavior.saveScrollState(AbsSavedState.EMPTY_STATE, this); // Invalidate the scroll ranges totalScrollRange = INVALID_SCROLL_RANGE;