From 14415a1308876f03af0611d0f7d26c2f3e47d58b Mon Sep 17 00:00:00 2001 From: rightnao Date: Wed, 31 Aug 2022 10:30:41 -0700 Subject: [PATCH] [AppBarLayout] Make a new setter for scroll effect and make SCROLL_EFFECT_COMPRESS public resolves https://github.com/material-components/material-components-android/issues/2894 PiperOrigin-RevId: 471289613 --- .../android/material/appbar/AppBarLayout.java | 27 ++++++++++++++++--- .../material/appbar/AppBarLayoutTest.java | 18 +++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/lib/java/com/google/android/material/appbar/AppBarLayout.java b/lib/java/com/google/android/material/appbar/AppBarLayout.java index 46fc5b5cb08..01d489abf52 100644 --- a/lib/java/com/google/android/material/appbar/AppBarLayout.java +++ b/lib/java/com/google/android/material/appbar/AppBarLayout.java @@ -1199,14 +1199,24 @@ public static class LayoutParams extends LinearLayout.LayoutParams { * No effect should be placed on this view. It will scroll 1:1 with the AppBarLayout/scrolling * content. */ - private static final int SCROLL_EFFECT_NONE = 0; + public static final int SCROLL_EFFECT_NONE = 0; /** * An effect that will "compress" this view as it hits the scroll ceiling (typically the top of * the screen). This is a parallax effect that masks this view and decreases its scroll ratio * in relation to the AppBarLayout's offset. */ - private static final int SCROLL_EFFECT_COMPRESS = 1; + public static final int SCROLL_EFFECT_COMPRESS = 1; + + /** + * The scroll effect to be applied when the AppBarLayout's offset changes. + * + * @hide + */ + @RestrictTo(LIBRARY_GROUP) + @IntDef({SCROLL_EFFECT_NONE, SCROLL_EFFECT_COMPRESS}) + @Retention(RetentionPolicy.SOURCE) + public @interface ScrollEffect {} private ChildScrollEffect scrollEffect; @@ -1219,7 +1229,7 @@ public LayoutParams(Context c, AttributeSet attrs) { int scrollEffectInt = a.getInt(R.styleable.AppBarLayout_Layout_layout_scrollEffect, SCROLL_EFFECT_NONE); - setScrollEffect(createScrollEffectFromInt(scrollEffectInt)); + setScrollEffect(scrollEffectInt); if (a.hasValue(R.styleable.AppBarLayout_Layout_layout_scrollInterpolator)) { int resId = a.getResourceId(R.styleable.AppBarLayout_Layout_layout_scrollInterpolator, 0); @@ -1313,6 +1323,17 @@ public void setScrollEffect(@Nullable ChildScrollEffect scrollEffect) { this.scrollEffect = scrollEffect; } + /** + * Set the scroll effect to be applied when the AppBarLayout's offset changes. + * + * @param scrollEffect An {@code AppBarLayoutChildScrollEffect} implementation. If + * {@link #SCROLL_EFFECT_NONE} is passed, the scroll effect will be cleared and no + * effect will be applied. + */ + public void setScrollEffect(@ScrollEffect int scrollEffect) { + this.scrollEffect = createScrollEffectFromInt(scrollEffect); + } + /** * Set the interpolator to when scrolling the view associated with this {@link LayoutParams}. * diff --git a/lib/javatests/com/google/android/material/appbar/AppBarLayoutTest.java b/lib/javatests/com/google/android/material/appbar/AppBarLayoutTest.java index 8aa6119e841..e923b8776ad 100644 --- a/lib/javatests/com/google/android/material/appbar/AppBarLayoutTest.java +++ b/lib/javatests/com/google/android/material/appbar/AppBarLayoutTest.java @@ -177,6 +177,24 @@ public void testDownNestedScrollRange_whenFirstChildNotScrollable_returnZero() { assertThat(appBarLayout.getDownNestedScrollRange()).isEqualTo(0); } + @Test + public void testSetScrollEffectNone_returnsNull() { + AppBarLayout.LayoutParams lp = + (AppBarLayout.LayoutParams) firstScrollableChild.getLayoutParams(); + lp.setScrollEffect(LayoutParams.SCROLL_EFFECT_NONE); + + assertThat(lp.getScrollEffect()).isEqualTo(null); + } + + @Test + public void testSetScrollEffectCompress() { + AppBarLayout.LayoutParams lp = + (AppBarLayout.LayoutParams) firstScrollableChild.getLayoutParams(); + lp.setScrollEffect(LayoutParams.SCROLL_EFFECT_COMPRESS); + + assertThat(lp.getScrollEffect()).isInstanceOf(AppBarLayout.CompressChildScrollEffect.class); + } + private static int getChildScrollRange(View child) { final LayoutParams lp = (LayoutParams) child.getLayoutParams(); return getChildFullHeight(child, lp)