Skip to content

Commit 245ffe7

Browse files
dsn5fthunterstich
authored andcommitted
[CollapsingToolbarLayout] Added option to force always applying system window inset top regardless of layout_height
PiperOrigin-RevId: 382286923
1 parent 6f28838 commit 245ffe7

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

catalog/java/io/material/catalog/tableofcontents/res/layout/cat_toc_fragment.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
<com.google.android.material.appbar.AppBarLayout
2828
android:id="@+id/cat_toc_app_bar_layout"
2929
android:layout_width="match_parent"
30-
android:layout_height="@dimen/cat_toc_tall_toolbar_height"
30+
android:layout_height="wrap_content"
3131
android:theme="@style/ThemeOverlay.Catalog.AppBarLayout"
3232
android:fitsSystemWindows="true">
3333

3434
<com.google.android.material.appbar.CollapsingToolbarLayout
3535
android:id="@+id/cat_toc_collapsingtoolbarlayout"
3636
android:layout_width="match_parent"
37-
android:layout_height="match_parent"
37+
android:layout_height="@dimen/cat_toc_tall_toolbar_height"
3838
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
3939
app:titleEnabled="false">
4040

catalog/java/io/material/catalog/tableofcontents/res/values/dimens.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
-->
1717

1818
<resources>
19-
<dimen name="cat_toc_tall_toolbar_height">128dp</dimen>
20-
<dimen name="cat_toc_short_toolbar_height">128dp</dimen>
19+
<dimen name="cat_toc_tall_toolbar_height">112dp</dimen>
2120
<dimen name="cat_toc_toolbar_image_offset">16dp</dimen>
2221
<dimen name="cat_toc_grid_divider_size">1dp</dimen>
2322
<dimen name="cat_toc_item_size">180dp</dimen>

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ public class CollapsingToolbarLayout extends FrameLayout {
173173
@TitleCollapseMode private int titleCollapseMode;
174174

175175
@Nullable WindowInsetsCompat lastInsets;
176+
private int topInsetApplied = 0;
177+
private boolean forceApplySystemWindowInsetTop = false;
176178

177179
public CollapsingToolbarLayout(@NonNull Context context) {
178180
this(context, null);
@@ -273,6 +275,9 @@ public CollapsingToolbarLayout(@NonNull Context context, @Nullable AttributeSet
273275

274276
toolbarId = a.getResourceId(R.styleable.CollapsingToolbarLayout_toolbarId, -1);
275277

278+
forceApplySystemWindowInsetTop =
279+
a.getBoolean(R.styleable.CollapsingToolbarLayout_forceApplySystemWindowInsetTop, false);
280+
276281
a.recycle();
277282

278283
setWillNotDraw(false);
@@ -516,9 +521,10 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
516521

517522
final int mode = MeasureSpec.getMode(heightMeasureSpec);
518523
final int topInset = lastInsets != null ? lastInsets.getSystemWindowInsetTop() : 0;
519-
if (mode == MeasureSpec.UNSPECIFIED && topInset > 0) {
520-
// If we have a top inset and we're set to wrap_content height we need to make sure
521-
// we add the top inset to our height, therefore we re-measure
524+
if ((mode == MeasureSpec.UNSPECIFIED || forceApplySystemWindowInsetTop) && topInset > 0) {
525+
// If we have a top inset and we're set to wrap_content height or force apply,
526+
// we need to make sure we add the top inset to our height, therefore we re-measure
527+
topInsetApplied = topInset;
522528
heightMeasureSpec =
523529
MeasureSpec.makeMeasureSpec(getMeasuredHeight() + topInset, MeasureSpec.EXACTLY);
524530
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
@@ -1310,6 +1316,24 @@ public boolean isRtlTextDirectionHeuristicsEnabled() {
13101316
return collapsingTextHelper.isRtlTextDirectionHeuristicsEnabled();
13111317
}
13121318

1319+
/**
1320+
* Sets whether the top system window inset should be respected regardless of what the
1321+
* {@code layout_height} of the {@code CollapsingToolbarLayout} is set to. Experimental Feature.
1322+
*/
1323+
@RestrictTo(LIBRARY_GROUP)
1324+
public void setForceApplySystemWindowInsetTop(boolean forceApplySystemWindowInsetTop) {
1325+
this.forceApplySystemWindowInsetTop = forceApplySystemWindowInsetTop;
1326+
}
1327+
1328+
/**
1329+
* Gets whether the top system window inset should be respected regardless of what the
1330+
* {@code layout_height} of the {@code CollapsingToolbarLayout} is set to. Experimental Feature.
1331+
*/
1332+
@RestrictTo(LIBRARY_GROUP)
1333+
public boolean isForceApplySystemWindowInsetTop() {
1334+
return forceApplySystemWindowInsetTop;
1335+
}
1336+
13131337
/**
13141338
* Set the amount of visible height in pixels used to define when to trigger a scrim visibility
13151339
* change.
@@ -1338,7 +1362,7 @@ public void setScrimVisibleHeightTrigger(@IntRange(from = 0) final int height) {
13381362
public int getScrimVisibleHeightTrigger() {
13391363
if (scrimVisibleHeightTrigger >= 0) {
13401364
// If we have one explicitly set, return it
1341-
return scrimVisibleHeightTrigger;
1365+
return scrimVisibleHeightTrigger + topInsetApplied;
13421366
}
13431367

13441368
// Otherwise we'll use the default computed value

lib/java/com/google/android/material/appbar/res/values/attrs.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@
206206
</attr>
207207
<!-- The maximum number of lines to display in the expanded state. Experimental Feature. -->
208208
<attr name="maxLines" format="integer" />
209+
<!-- Whether the system window inset top should be applied regardless of
210+
what the layout_height is set to. Experimental Feature. -->
211+
<attr name="forceApplySystemWindowInsetTop" format="boolean" />
209212
</declare-styleable>
210213

211214
<declare-styleable name="CollapsingToolbarLayout_Layout">

0 commit comments

Comments
 (0)