public class BottomSheetBehavior<V extends View> extends CoordinatorLayout.Behavior<V>
CoordinatorLayout
to make it work as
a bottom sheet.Modifier and Type | Class and Description |
---|---|
static class |
BottomSheetBehavior.BottomSheetCallback
Callback for monitoring events about bottom sheets.
|
protected static class |
BottomSheetBehavior.SavedState |
static interface |
BottomSheetBehavior.State |
Modifier and Type | Field and Description |
---|---|
static int |
PEEK_HEIGHT_AUTO
Peek at the 16:9 ratio keyline of its parent.
|
static int |
STATE_COLLAPSED
The bottom sheet is collapsed.
|
static int |
STATE_DRAGGING
The bottom sheet is dragging.
|
static int |
STATE_EXPANDED
The bottom sheet is expanded.
|
static int |
STATE_HIDDEN
The bottom sheet is hidden.
|
static int |
STATE_SETTLING
The bottom sheet is settling.
|
Constructor and Description |
---|
BottomSheetBehavior()
Default constructor for instantiating BottomSheetBehaviors.
|
BottomSheetBehavior(Context context,
AttributeSet attrs)
Default constructor for inflating BottomSheetBehaviors from layout.
|
Modifier and Type | Method and Description |
---|---|
static <V extends View> |
from(V view)
A utility function to get the
BottomSheetBehavior associated with the view . |
int |
getPeekHeight()
Gets the height of the bottom sheet when it is collapsed.
|
boolean |
getSkipCollapsed()
Sets whether this bottom sheet should skip the collapsed state when it is being hidden
after it is expanded once.
|
int |
getState()
Gets the current state of the bottom sheet.
|
boolean |
isHideable()
Gets whether this bottom sheet can hide when it is swiped down.
|
boolean |
onInterceptTouchEvent(CoordinatorLayout parent,
V child,
MotionEvent event)
Respond to CoordinatorLayout touch events before they are dispatched to child views.
|
boolean |
onLayoutChild(CoordinatorLayout parent,
V child,
int layoutDirection)
Called when the parent CoordinatorLayout is about the lay out the given child view.
|
boolean |
onNestedPreFling(CoordinatorLayout coordinatorLayout,
V child,
View target,
float velocityX,
float velocityY)
Called when a nested scrolling child is about to start a fling.
|
void |
onNestedPreScroll(CoordinatorLayout coordinatorLayout,
V child,
View target,
int dx,
int dy,
int[] consumed)
Called when a nested scroll in progress is about to update, before the target has
consumed any of the scrolled distance.
|
void |
onRestoreInstanceState(CoordinatorLayout parent,
V child,
Parcelable state)
Hook allowing a behavior to re-apply a representation of its internal state that had
previously been generated by
CoordinatorLayout.Behavior.onSaveInstanceState(android.support.design.widget.CoordinatorLayout, V) . |
Parcelable |
onSaveInstanceState(CoordinatorLayout parent,
V child)
Hook allowing a behavior to generate a representation of its internal state
that can later be used to create a new instance with that same state.
|
boolean |
onStartNestedScroll(CoordinatorLayout coordinatorLayout,
V child,
View directTargetChild,
View target,
int nestedScrollAxes)
Called when a descendant of the CoordinatorLayout attempts to initiate a nested scroll.
|
void |
onStopNestedScroll(CoordinatorLayout coordinatorLayout,
V child,
View target)
Called when a nested scroll has ended.
|
boolean |
onTouchEvent(CoordinatorLayout parent,
V child,
MotionEvent event)
Respond to CoordinatorLayout touch events after this Behavior has started
intercepting them. |
void |
setBottomSheetCallback(BottomSheetBehavior.BottomSheetCallback callback)
Sets a callback to be notified of bottom sheet events.
|
void |
setHideable(boolean hideable)
Sets whether this bottom sheet can hide when it is swiped down.
|
void |
setPeekHeight(int peekHeight)
Sets the height of the bottom sheet when it is collapsed.
|
void |
setSkipCollapsed(boolean skipCollapsed)
Sets whether this bottom sheet should skip the collapsed state when it is being hidden
after it is expanded once.
|
void |
setState(int state)
Sets the state of the bottom sheet.
|
blocksInteractionBelow, getInsetDodgeRect, getScrimColor, getScrimOpacity, getTag, isDirty, layoutDependsOn, onApplyWindowInsets, onAttachedToLayoutParams, onDependentViewChanged, onDependentViewRemoved, onDetachedFromLayoutParams, onMeasureChild, onNestedFling, onNestedScroll, onNestedScrollAccepted, onRequestChildRectangleOnScreen, setTag
public static final int STATE_DRAGGING
public static final int STATE_SETTLING
public static final int STATE_EXPANDED
public static final int STATE_COLLAPSED
public static final int STATE_HIDDEN
public static final int PEEK_HEIGHT_AUTO
This can be used as a parameter for setPeekHeight(int)
.
getPeekHeight()
will return this when the value is set.
public BottomSheetBehavior()
public BottomSheetBehavior(Context context, AttributeSet attrs)
context
- The Context
.attrs
- The AttributeSet
.public Parcelable onSaveInstanceState(CoordinatorLayout parent, V child)
CoordinatorLayout.Behavior
Behavior state is only saved when both the parent CoordinatorLayout
and
a view using this behavior have valid IDs set.
onSaveInstanceState
in class CoordinatorLayout.Behavior<V extends View>
parent
- the parent CoordinatorLayoutchild
- child view to restore fromCoordinatorLayout.onRestoreInstanceState(android.os.Parcelable)
,
View.onSaveInstanceState()
public void onRestoreInstanceState(CoordinatorLayout parent, V child, Parcelable state)
CoordinatorLayout.Behavior
CoordinatorLayout.Behavior.onSaveInstanceState(android.support.design.widget.CoordinatorLayout, V)
. This function will never
be called with a null state.onRestoreInstanceState
in class CoordinatorLayout.Behavior<V extends View>
parent
- the parent CoordinatorLayoutchild
- child view to restore fromstate
- The frozen state that had previously been returned by
CoordinatorLayout.Behavior.onSaveInstanceState(android.support.design.widget.CoordinatorLayout, V)
.CoordinatorLayout.onSaveInstanceState()
public boolean onLayoutChild(CoordinatorLayout parent, V child, int layoutDirection)
CoordinatorLayout.Behavior
This method can be used to perform custom or modified layout of a child view
in place of the default child layout behavior. The Behavior's implementation can
delegate to the standard CoordinatorLayout measurement behavior by calling
parent.onLayoutChild
.
If a Behavior implements
CoordinatorLayout.Behavior.onDependentViewChanged(CoordinatorLayout, android.view.View, android.view.View)
to change the position of a view in response to a dependent view changing, it
should also implement onLayoutChild
in such a way that respects those
dependent views. onLayoutChild
will always be called for a dependent view
after its dependency has been laid out.
onLayoutChild
in class CoordinatorLayout.Behavior<V extends View>
parent
- the parent CoordinatorLayoutchild
- child view to lay outlayoutDirection
- the resolved layout direction for the CoordinatorLayout, such as
ViewCompat.LAYOUT_DIRECTION_LTR
or
ViewCompat.LAYOUT_DIRECTION_RTL
.public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event)
CoordinatorLayout.Behavior
Behaviors can use this to monitor inbound touch events until one decides to intercept the rest of the event stream to take an action on its associated child view. This method will return false until it detects the proper intercept conditions, then return true once those conditions have occurred.
Once a Behavior intercepts touch events, the rest of the event stream will
be sent to the CoordinatorLayout.Behavior.onTouchEvent(android.support.design.widget.CoordinatorLayout, V, android.view.MotionEvent)
method.
The default implementation of this method always returns false.
onInterceptTouchEvent
in class CoordinatorLayout.Behavior<V extends View>
parent
- the parent view currently receiving this touch eventchild
- the child view associated with this Behaviorevent
- the MotionEvent describing the touch event being processedpublic boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event)
CoordinatorLayout.Behavior
intercepting
them.
Behaviors may intercept touch events in order to help the CoordinatorLayout manipulate its child views. For example, a Behavior may allow a user to drag a UI pane open or closed. This method should perform actual mutations of view layout state.
onTouchEvent
in class CoordinatorLayout.Behavior<V extends View>
parent
- the parent view currently receiving this touch eventchild
- the child view associated with this Behaviorevent
- the MotionEvent describing the touch event being processedpublic boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, V child, View directTargetChild, View target, int nestedScrollAxes)
CoordinatorLayout.Behavior
Any Behavior associated with any direct child of the CoordinatorLayout may respond to this event and return true to indicate that the CoordinatorLayout should act as a nested scrolling parent for this scroll. Only Behaviors that return true from this method will receive subsequent nested scroll events.
onStartNestedScroll
in class CoordinatorLayout.Behavior<V extends View>
coordinatorLayout
- the CoordinatorLayout parent of the view this Behavior is
associated withchild
- the child view of the CoordinatorLayout this Behavior is associated withdirectTargetChild
- the child view of the CoordinatorLayout that either is or
contains the target of the nested scroll operationtarget
- the descendant view of the CoordinatorLayout initiating the nested scrollnestedScrollAxes
- the axes that this nested scroll applies to. See
ViewCompat.SCROLL_AXIS_HORIZONTAL
,
ViewCompat.SCROLL_AXIS_VERTICAL
NestedScrollingParent.onStartNestedScroll(View, View, int)
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, V child, View target, int dx, int dy, int[] consumed)
CoordinatorLayout.Behavior
Any Behavior associated with the direct child of the CoordinatorLayout may elect
to accept the nested scroll as part of CoordinatorLayout.Behavior.onStartNestedScroll(android.support.design.widget.CoordinatorLayout, V, android.view.View, android.view.View, int)
. Each Behavior
that returned true will receive subsequent nested scroll events for that nested scroll.
onNestedPreScroll
is called each time the nested scroll is updated
by the nested scrolling child, before the nested scrolling child has consumed the scroll
distance itself. Each Behavior responding to the nested scroll will receive the
same values. The CoordinatorLayout will report as consumed the maximum number
of pixels in either direction that any Behavior responding to the nested scroll reported
as consumed.
onNestedPreScroll
in class CoordinatorLayout.Behavior<V extends View>
coordinatorLayout
- the CoordinatorLayout parent of the view this Behavior is
associated withchild
- the child view of the CoordinatorLayout this Behavior is associated withtarget
- the descendant view of the CoordinatorLayout performing the nested scrolldx
- the raw horizontal number of pixels that the user attempted to scrolldy
- the raw vertical number of pixels that the user attempted to scrollconsumed
- out parameter. consumed[0] should be set to the distance of dx that
was consumed, consumed[1] should be set to the distance of dy that
was consumedNestedScrollingParent.onNestedPreScroll(View, int, int, int[])
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, V child, View target)
CoordinatorLayout.Behavior
Any Behavior associated with any direct child of the CoordinatorLayout may elect
to accept the nested scroll as part of CoordinatorLayout.Behavior.onStartNestedScroll(android.support.design.widget.CoordinatorLayout, V, android.view.View, android.view.View, int)
. Each Behavior
that returned true will receive subsequent nested scroll events for that nested scroll.
onStopNestedScroll
marks the end of a single nested scroll event
sequence. This is a good place to clean up any state related to the nested scroll.
onStopNestedScroll
in class CoordinatorLayout.Behavior<V extends View>
coordinatorLayout
- the CoordinatorLayout parent of the view this Behavior is
associated withchild
- the child view of the CoordinatorLayout this Behavior is associated withtarget
- the descendant view of the CoordinatorLayout that initiated
the nested scrollNestedScrollingParent.onStopNestedScroll(View)
public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, V child, View target, float velocityX, float velocityY)
CoordinatorLayout.Behavior
Any Behavior associated with the direct child of the CoordinatorLayout may elect
to accept the nested scroll as part of CoordinatorLayout.Behavior.onStartNestedScroll(android.support.design.widget.CoordinatorLayout, V, android.view.View, android.view.View, int)
. Each Behavior
that returned true will receive subsequent nested scroll events for that nested scroll.
onNestedPreFling
is called when the current nested scrolling child view
detects the proper conditions for a fling, but it has not acted on it yet. A
Behavior can return true to indicate that it consumed the fling. If at least one
Behavior returns true, the fling should not be acted upon by the child.
onNestedPreFling
in class CoordinatorLayout.Behavior<V extends View>
coordinatorLayout
- the CoordinatorLayout parent of the view this Behavior is
associated withchild
- the child view of the CoordinatorLayout this Behavior is associated withtarget
- the descendant view of the CoordinatorLayout performing the nested scrollvelocityX
- horizontal velocity of the attempted flingvelocityY
- vertical velocity of the attempted flingNestedScrollingParent.onNestedPreFling(View, float, float)
public final void setPeekHeight(int peekHeight)
peekHeight
- The height of the collapsed bottom sheet in pixels, or
PEEK_HEIGHT_AUTO
to configure the sheet to peek automatically
at 16:9 ratio keyline.public final int getPeekHeight()
PEEK_HEIGHT_AUTO
if the sheet is configured to peek automatically at 16:9 ratio keylinepublic void setHideable(boolean hideable)
hideable
- true
to make this bottom sheet hideable.public boolean isHideable()
true
if this bottom sheet can hide.public void setSkipCollapsed(boolean skipCollapsed)
skipCollapsed
- True if the bottom sheet should skip the collapsed state.public boolean getSkipCollapsed()
public void setBottomSheetCallback(BottomSheetBehavior.BottomSheetCallback callback)
callback
- The callback to notify when bottom sheet events occur.public final void setState(int state)
state
- One of STATE_COLLAPSED
, STATE_EXPANDED
, or
STATE_HIDDEN
.public final int getState()
STATE_EXPANDED
, STATE_COLLAPSED
, STATE_DRAGGING
,
and STATE_SETTLING
.public static <V extends View> BottomSheetBehavior<V> from(V view)
BottomSheetBehavior
associated with the view
.view
- The View
with BottomSheetBehavior
.BottomSheetBehavior
associated with the view
.