Skip to content

Commit

Permalink
[AppBarLayout] Make a new setter for scroll effect and make SCROLL_EF…
Browse files Browse the repository at this point in the history
…FECT_COMPRESS public

resolves #2894

PiperOrigin-RevId: 471289613
  • Loading branch information
imhappi committed Aug 31, 2022
1 parent ae788ac commit 14415a1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
27 changes: 24 additions & 3 deletions lib/java/com/google/android/material/appbar/AppBarLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 14415a1

Please sign in to comment.