Skip to content

Commit 0425d3b

Browse files
chrisbanescketcham
authored andcommitted
Fix CollapsingToolbarLayout title bounds
Caused by AppBarLayout + WindowInset commit, which breaks some incorrect assumptions in CTL. Fixed by breaking apart ViewOffsetHelper so that its tracking of layout coordinates, and applying offsets, are done at different times. Also fixed catalog's AppBar sample layouts to set IDs, enabling state restoration to work. PiperOrigin-RevId: 239592953
1 parent e4e6052 commit 0425d3b

File tree

6 files changed

+24
-15
lines changed

6 files changed

+24
-15
lines changed

catalog/java/io/material/catalog/topappbar/res/layout/cat_topappbar_collapsing_fragment.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@
1717
<androidx.coordinatorlayout.widget.CoordinatorLayout
1818
xmlns:android="http://schemas.android.com/apk/res/android"
1919
xmlns:app="http://schemas.android.com/apk/res-auto"
20+
android:id="@+id/coordinator"
2021
android:layout_width="match_parent"
2122
android:layout_height="match_parent"
2223
android:fitsSystemWindows="true">
2324

2425
<com.google.android.material.appbar.AppBarLayout
26+
android:id="@+id/appbarlayout"
2527
android:layout_width="match_parent"
2628
android:layout_height="@dimen/cat_topappbar_tall_toolbar_height"
2729
android:fitsSystemWindows="true">
2830

2931
<com.google.android.material.appbar.CollapsingToolbarLayout
32+
android:id="@+id/collapsingtoolbarlayout"
3033
style="?attr/catalogToolbarStyle"
3134
android:layout_width="match_parent"
3235
android:layout_height="match_parent"

catalog/java/io/material/catalog/topappbar/res/layout/cat_topappbar_fragment.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
<androidx.coordinatorlayout.widget.CoordinatorLayout
1818
xmlns:android="http://schemas.android.com/apk/res/android"
1919
xmlns:app="http://schemas.android.com/apk/res-auto"
20+
android:id="@+id/coordinator"
2021
android:layout_width="match_parent"
2122
android:layout_height="match_parent"
2223
android:fitsSystemWindows="true">
2324

2425
<com.google.android.material.appbar.AppBarLayout
26+
android:id="@+id/appbarlayout"
2527
android:layout_width="match_parent"
2628
android:layout_height="wrap_content"
2729
android:fitsSystemWindows="true"

catalog/java/io/material/catalog/topappbar/res/layout/cat_topappbar_scrolling_fragment.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
<androidx.coordinatorlayout.widget.CoordinatorLayout
1818
xmlns:android="http://schemas.android.com/apk/res/android"
1919
xmlns:app="http://schemas.android.com/apk/res-auto"
20+
android:id="@+id/coordinator"
2021
android:layout_width="match_parent"
2122
android:layout_height="match_parent"
2223
android:fitsSystemWindows="true">
2324

2425
<com.google.android.material.appbar.AppBarLayout
26+
android:id="@+id/appbarlayout"
2527
android:layout_width="match_parent"
2628
android:layout_height="wrap_content"
2729
android:fitsSystemWindows="true">

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,11 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
449449
}
450450
}
451451

452+
// Update our child view offset helpers so that they track the correct layout coordinates
453+
for (int i = 0, z = getChildCount(); i < z; i++) {
454+
getViewOffsetHelper(getChildAt(i)).onViewLayout();
455+
}
456+
452457
// Update the collapsed bounds by getting its transformed bounds
453458
if (collapsingTitleEnabled && dummyView != null) {
454459
// We only draw the title if the dummy view is being displayed (Toolbar removes
@@ -481,13 +486,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
481486
}
482487
}
483488

484-
// Update our child view offset helpers. This needs to be done after the title has been
485-
// setup, so that any Toolbars are in their original position
486-
for (int i = 0, z = getChildCount(); i < z; i++) {
487-
getViewOffsetHelper(getChildAt(i)).onViewLayout();
488-
}
489-
490-
// Finally, set our minimum height to enable proper AppBarLayout collapsing
489+
// Set our minimum height to enable proper AppBarLayout collapsing
491490
if (toolbar != null) {
492491
if (collapsingTitleEnabled && TextUtils.isEmpty(collapsingTextHelper.getText())) {
493492
// If we do not currently have a title, try and grab it from the Toolbar
@@ -501,6 +500,11 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
501500
}
502501

503502
updateScrimVisibility();
503+
504+
// Apply any view offsets, this should be done at the very end of layout
505+
for (int i = 0, z = getChildCount(); i < z; i++) {
506+
getViewOffsetHelper(getChildAt(i)).applyOffsets();
507+
}
504508
}
505509

506510
private static int getHeightWithMargins(@NonNull final View view) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public boolean onLayoutChild(CoordinatorLayout parent, V child, int layoutDirect
4444
viewOffsetHelper = new ViewOffsetHelper(child);
4545
}
4646
viewOffsetHelper.onViewLayout();
47+
viewOffsetHelper.applyOffsets();
4748

4849
if (tempTopBottomOffset != 0) {
4950
viewOffsetHelper.setTopAndBottomOffset(tempTopBottomOffset);

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,13 @@ public ViewOffsetHelper(View view) {
4141
this.view = view;
4242
}
4343

44-
public void onViewLayout() {
45-
// Now grab the intended top
44+
void onViewLayout() {
45+
// Grab the original top and left
4646
layoutTop = view.getTop();
4747
layoutLeft = view.getLeft();
48-
49-
// And offset it as needed
50-
updateOffsets();
5148
}
5249

53-
private void updateOffsets() {
50+
void applyOffsets() {
5451
ViewCompat.offsetTopAndBottom(view, offsetTop - (view.getTop() - layoutTop));
5552
ViewCompat.offsetLeftAndRight(view, offsetLeft - (view.getLeft() - layoutLeft));
5653
}
@@ -64,7 +61,7 @@ private void updateOffsets() {
6461
public boolean setTopAndBottomOffset(int offset) {
6562
if (verticalOffsetEnabled && offsetTop != offset) {
6663
offsetTop = offset;
67-
updateOffsets();
64+
applyOffsets();
6865
return true;
6966
}
7067
return false;
@@ -79,7 +76,7 @@ public boolean setTopAndBottomOffset(int offset) {
7976
public boolean setLeftAndRightOffset(int offset) {
8077
if (horizontalOffsetEnabled && offsetLeft != offset) {
8178
offsetLeft = offset;
82-
updateOffsets();
79+
applyOffsets();
8380
return true;
8481
}
8582
return false;

0 commit comments

Comments
 (0)