Skip to content

Commit 14415a1

Browse files
committed
[AppBarLayout] Make a new setter for scroll effect and make SCROLL_EFFECT_COMPRESS public
resolves #2894 PiperOrigin-RevId: 471289613
1 parent ae788ac commit 14415a1

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,14 +1199,24 @@ public static class LayoutParams extends LinearLayout.LayoutParams {
11991199
* No effect should be placed on this view. It will scroll 1:1 with the AppBarLayout/scrolling
12001200
* content.
12011201
*/
1202-
private static final int SCROLL_EFFECT_NONE = 0;
1202+
public static final int SCROLL_EFFECT_NONE = 0;
12031203

12041204
/**
12051205
* An effect that will "compress" this view as it hits the scroll ceiling (typically the top of
12061206
* the screen). This is a parallax effect that masks this view and decreases its scroll ratio
12071207
* in relation to the AppBarLayout's offset.
12081208
*/
1209-
private static final int SCROLL_EFFECT_COMPRESS = 1;
1209+
public static final int SCROLL_EFFECT_COMPRESS = 1;
1210+
1211+
/**
1212+
* The scroll effect to be applied when the AppBarLayout's offset changes.
1213+
*
1214+
* @hide
1215+
*/
1216+
@RestrictTo(LIBRARY_GROUP)
1217+
@IntDef({SCROLL_EFFECT_NONE, SCROLL_EFFECT_COMPRESS})
1218+
@Retention(RetentionPolicy.SOURCE)
1219+
public @interface ScrollEffect {}
12101220

12111221
private ChildScrollEffect scrollEffect;
12121222

@@ -1219,7 +1229,7 @@ public LayoutParams(Context c, AttributeSet attrs) {
12191229

12201230
int scrollEffectInt =
12211231
a.getInt(R.styleable.AppBarLayout_Layout_layout_scrollEffect, SCROLL_EFFECT_NONE);
1222-
setScrollEffect(createScrollEffectFromInt(scrollEffectInt));
1232+
setScrollEffect(scrollEffectInt);
12231233

12241234
if (a.hasValue(R.styleable.AppBarLayout_Layout_layout_scrollInterpolator)) {
12251235
int resId = a.getResourceId(R.styleable.AppBarLayout_Layout_layout_scrollInterpolator, 0);
@@ -1313,6 +1323,17 @@ public void setScrollEffect(@Nullable ChildScrollEffect scrollEffect) {
13131323
this.scrollEffect = scrollEffect;
13141324
}
13151325

1326+
/**
1327+
* Set the scroll effect to be applied when the AppBarLayout's offset changes.
1328+
*
1329+
* @param scrollEffect An {@code AppBarLayoutChildScrollEffect} implementation. If
1330+
* {@link #SCROLL_EFFECT_NONE} is passed, the scroll effect will be cleared and no
1331+
* effect will be applied.
1332+
*/
1333+
public void setScrollEffect(@ScrollEffect int scrollEffect) {
1334+
this.scrollEffect = createScrollEffectFromInt(scrollEffect);
1335+
}
1336+
13161337
/**
13171338
* Set the interpolator to when scrolling the view associated with this {@link LayoutParams}.
13181339
*

lib/javatests/com/google/android/material/appbar/AppBarLayoutTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,24 @@ public void testDownNestedScrollRange_whenFirstChildNotScrollable_returnZero() {
177177
assertThat(appBarLayout.getDownNestedScrollRange()).isEqualTo(0);
178178
}
179179

180+
@Test
181+
public void testSetScrollEffectNone_returnsNull() {
182+
AppBarLayout.LayoutParams lp =
183+
(AppBarLayout.LayoutParams) firstScrollableChild.getLayoutParams();
184+
lp.setScrollEffect(LayoutParams.SCROLL_EFFECT_NONE);
185+
186+
assertThat(lp.getScrollEffect()).isEqualTo(null);
187+
}
188+
189+
@Test
190+
public void testSetScrollEffectCompress() {
191+
AppBarLayout.LayoutParams lp =
192+
(AppBarLayout.LayoutParams) firstScrollableChild.getLayoutParams();
193+
lp.setScrollEffect(LayoutParams.SCROLL_EFFECT_COMPRESS);
194+
195+
assertThat(lp.getScrollEffect()).isInstanceOf(AppBarLayout.CompressChildScrollEffect.class);
196+
}
197+
180198
private static int getChildScrollRange(View child) {
181199
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
182200
return getChildFullHeight(child, lp)

0 commit comments

Comments
 (0)