Skip to content

Commit

Permalink
[BottomSheetBehavior] Add attribute for significant velocity threshold
Browse files Browse the repository at this point in the history
resolves #2034

PiperOrigin-RevId: 463458591
  • Loading branch information
imhappi authored and drchen committed Jul 28, 2022
1 parent e284e57 commit d845db8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 11 deletions.
19 changes: 10 additions & 9 deletions docs/components/BottomSheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,15 +407,16 @@ Element | Attribute | Related method(s) | Def
More info about these attributes and how to use them in the
[setting behavior](#setting-behavior) section.

Behavior | Related method(s) | Default value
-------------------------------- | ------------------------------------------------- | -------------
`app:behavior_peekHeight` | `setPeekHeight`<br/>`getPeekHeight` | `auto`
`app:behavior_hideable` | `setHideable`<br/>`isHideable` | `false` for standard<br/>`true` for modal
`app:behavior_skipCollapsed` | `setSkipCollapsed`<br/>`getSkipCollapsed` | `false`
`app:behavior_fitToContents` | `setFitToContents`<br/>`isFitToContents` | `true`
`app:behavior_draggable` | `setDraggable`<br/>`isDraggable` | `true`
`app:behavior_halfExpandedRatio` | `setHalfExpandedRatio`<br/>`getHalfExpandedRatio` | `0.5`
`app:behavior_expandedOffset` | `setExpandedOffset`<br/>`getExpandedOffset` | `0dp`
Behavior | Related method(s) | Default value
------------------------------------------- | ------------------------------------------------------------------------- | -------------
`app:behavior_peekHeight` | `setPeekHeight`<br/>`getPeekHeight` | `auto`
`app:behavior_hideable` | `setHideable`<br/>`isHideable` | `false` for standard<br/>`true` for modal
`app:behavior_skipCollapsed` | `setSkipCollapsed`<br/>`getSkipCollapsed` | `false`
`app:behavior_fitToContents` | `setFitToContents`<br/>`isFitToContents` | `true`
`app:behavior_draggable` | `setDraggable`<br/>`isDraggable` | `true`
`app:behavior_halfExpandedRatio` | `setHalfExpandedRatio`<br/>`getHalfExpandedRatio` | `0.5`
`app:behavior_expandedOffset` | `setExpandedOffset`<br/>`getExpandedOffset` | `0dp`
`app:behavior_significantVelocityThreshold` | `setSignificantVelocityThreshold` <br/> `getSignificantVelocityThreshold` | `500 pixels/s`

To save behavior on configuration change:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void onLayout(@NonNull View bottomSheet) {}

@SaveFlags private int saveFlags = SAVE_NONE;

private static final int SIGNIFICANT_VEL_THRESHOLD = 500;
@VisibleForTesting static final int DEFAULT_SIGNIFICANT_VEL_THRESHOLD = 500;

private static final float HIDE_THRESHOLD = 0.5f;

Expand All @@ -218,6 +218,8 @@ void onLayout(@NonNull View bottomSheet) {}

private float maximumVelocity;

private int significantVelocityThreshold;

/** Peek height set by the user. */
private int peekHeight;

Expand Down Expand Up @@ -382,6 +384,11 @@ public BottomSheetBehavior(@NonNull Context context, @Nullable AttributeSet attr
R.styleable.BottomSheetBehavior_Layout_behavior_expandedOffset, 0));
}

setSignificantVelocityThreshold(
a.getInt(
R.styleable.BottomSheetBehavior_Layout_behavior_significantVelocityThreshold,
DEFAULT_SIGNIFICANT_VEL_THRESHOLD));

// Reading out if we are handling padding, so we can apply it to the content.
paddingBottomSystemWindowInsets =
a.getBoolean(R.styleable.BottomSheetBehavior_Layout_paddingBottomSystemWindowInsets, false);
Expand Down Expand Up @@ -1124,6 +1131,28 @@ public boolean isDraggable() {
return draggable;
}

/*
* Sets the velocity threshold considered significant enough to trigger a slide
* to the next stable state.
*
* @param significantVelocityThreshold The velocity threshold that warrants a vertical swipe.
* @see #getSignificantVelocityThreshold()
* @attr ref com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_significantVelocityThreshold
*/
public void setSignificantVelocityThreshold(int significantVelocityThreshold) {
this.significantVelocityThreshold = significantVelocityThreshold;
}

/*
* Returns the significant velocity threshold.
*
* @see #setSignificantVelocityThreshold(int)
* @attr ref com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_significantVelocityThreshold
*/
public int getSignificantVelocityThreshold() {
return this.significantVelocityThreshold;
}

/**
* Sets save flags to be preserved in bottomsheet on configuration change.
*
Expand Down Expand Up @@ -1686,7 +1715,7 @@ public void onViewReleased(@NonNull View releasedChild, float xvel, float yvel)
} else if (hideable && shouldHide(releasedChild, yvel)) {
// Hide if the view was either released low or it was a significant vertical swipe
// otherwise settle to closest expanded state.
if ((Math.abs(xvel) < Math.abs(yvel) && yvel > SIGNIFICANT_VEL_THRESHOLD)
if ((Math.abs(xvel) < Math.abs(yvel) && yvel > significantVelocityThreshold)
|| releasedLow(releasedChild)) {
targetState = STATE_HIDDEN;
} else if (fitToContents) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<public name="behavior_halfExpandedRatio" type="attr"/>
<public name="behavior_hideable" type="attr"/>
<public name="behavior_saveFlags" type="attr"/>
<public name="behavior_significantVelocityThreshold" type="attr"/>
<public name="behavior_skipCollapsed" type="attr"/>
<public name="enableEdgeToEdge" type="attr"/>
<public name="gestureInsetBottomIgnored" type="attr"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
<!-- The top offset of the BottomSheet in the expanded-state when fitsToContent is false.
The default value is 0, which results in the sheet matching the parent's top. -->
<attr name="behavior_expandedOffset" format="reference|dimension"/>
<!-- The vertical velocity threshold considered significant enough to warrant a
vertical swipe. -->
<attr name="behavior_significantVelocityThreshold" format="dimension"/>
<!-- Shape appearance style reference for BottomSheet. Attribute declaration is in the shape
package. -->
<attr name="shapeAppearance"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.google.android.material.test.R;

import static com.google.android.material.bottomsheet.BottomSheetBehavior.DEFAULT_SIGNIFICANT_VEL_THRESHOLD;
import static com.google.common.truth.Truth.assertThat;

import android.os.Bundle;
Expand Down Expand Up @@ -66,6 +67,22 @@ public void createBottomSheet_withIntegerRefOffset_hasCorrectOffset() {
assertThat(behavior.getExpandedOffset()).isEqualTo(220);
}

@Test
public void setSignificantVelocityThreshold() {
AttributeSet attributes =
Robolectric.buildAttributeSet()
.build();
BottomSheetBehavior<View> behavior = new BottomSheetBehavior<>(activity, attributes);
// Test the default value.
assertThat(behavior.getSignificantVelocityThreshold())
.isEqualTo(DEFAULT_SIGNIFICANT_VEL_THRESHOLD);

int significantVelocityValue = 1;
behavior.setSignificantVelocityThreshold(significantVelocityValue);

assertThat(behavior.getSignificantVelocityThreshold()).isEqualTo(significantVelocityValue);
}

private static class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle bundle) {
Expand Down

0 comments on commit d845db8

Please sign in to comment.