belongs to Maven artifact com.android.support:design:27.1.0
BottomSheetBehavior
public
class
BottomSheetBehavior
extends Behavior<V extends View>
java.lang.Object | ||
↳ | android.support.design.widget.CoordinatorLayout.Behavior<V extends android.view.View> | |
↳ | android.support.design.widget.BottomSheetBehavior<V extends android.view.View> |
An interaction behavior plugin for a child view of CoordinatorLayout
to make it work as
a bottom sheet.
Summary
Nested classes | |
---|---|
class |
BottomSheetBehavior.BottomSheetCallback
Callback for monitoring events about bottom sheets. |
class |
BottomSheetBehavior.SavedState
|
XML attributes | |
---|---|
BottomSheetBehavior_Layout_behavior_hideable |
|
BottomSheetBehavior_Layout_behavior_peekHeight |
|
BottomSheetBehavior_Layout_behavior_skipCollapsed |
Constants | |
---|---|
int |
PEEK_HEIGHT_AUTO
Peek at the 16:9 ratio keyline of its parent. |
int |
STATE_COLLAPSED
The bottom sheet is collapsed. |
int |
STATE_DRAGGING
The bottom sheet is dragging. |
int |
STATE_EXPANDED
The bottom sheet is expanded. |
int |
STATE_HIDDEN
The bottom sheet is hidden. |
int |
STATE_SETTLING
The bottom sheet is settling. |
Public constructors | |
---|---|
BottomSheetBehavior()
Default constructor for instantiating BottomSheetBehaviors. |
|
BottomSheetBehavior(Context context, AttributeSet attrs)
Default constructor for inflating BottomSheetBehaviors from layout. |
Public methods | |
---|---|
static
<V extends View>
BottomSheetBehavior<V>
|
from(V view)
A utility function to get the |
final
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. |
final
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)
This method is deprecated.
You should now override
|
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 |
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)
This method is deprecated.
You should now override
|
void
|
onStopNestedScroll(CoordinatorLayout coordinatorLayout, V child, View target)
This method is deprecated.
You should now override
|
boolean
|
onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event)
Respond to CoordinatorLayout touch events after this Behavior has started
|
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. |
final
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. |
final
void
|
setState(int state)
Sets the state of the bottom sheet. |
Inherited methods | |
---|---|
From
class
android.support.design.widget.CoordinatorLayout.Behavior
| |
From
class
java.lang.Object
|
XML attributes
BottomSheetBehavior_Layout_behavior_hideable
Related methods:
BottomSheetBehavior_Layout_behavior_peekHeight
Related methods:
BottomSheetBehavior_Layout_behavior_skipCollapsed
Related methods:
Constants
PEEK_HEIGHT_AUTO
int PEEK_HEIGHT_AUTO
Peek at the 16:9 ratio keyline of its parent.
This can be used as a parameter for setPeekHeight(int)
.
getPeekHeight()
will return this when the value is set.
Constant Value: -1 (0xffffffff)
STATE_COLLAPSED
int STATE_COLLAPSED
The bottom sheet is collapsed.
Constant Value: 4 (0x00000004)
STATE_DRAGGING
int STATE_DRAGGING
The bottom sheet is dragging.
Constant Value: 1 (0x00000001)
STATE_EXPANDED
int STATE_EXPANDED
The bottom sheet is expanded.
Constant Value: 3 (0x00000003)
STATE_HIDDEN
int STATE_HIDDEN
The bottom sheet is hidden.
Constant Value: 5 (0x00000005)
STATE_SETTLING
int STATE_SETTLING
The bottom sheet is settling.
Constant Value: 2 (0x00000002)
Public constructors
BottomSheetBehavior
BottomSheetBehavior ()
Default constructor for instantiating BottomSheetBehaviors.
BottomSheetBehavior
BottomSheetBehavior (Context context, AttributeSet attrs)
Default constructor for inflating BottomSheetBehaviors from layout.
Parameters | |
---|---|
context |
Context : The Context . |
attrs |
AttributeSet : The AttributeSet .
|
Public methods
from
BottomSheetBehavior<V> from (V view)
A utility function to get the BottomSheetBehavior
associated with the view
.
Parameters | |
---|---|
view |
V : The View with BottomSheetBehavior . |
Returns | |
---|---|
BottomSheetBehavior<V> |
The BottomSheetBehavior associated with the view .
|
getPeekHeight
int getPeekHeight ()
Gets the height of the bottom sheet when it is collapsed.
Related XML Attributes:
Returns | |
---|---|
int |
The height of the collapsed bottom sheet in pixels, or PEEK_HEIGHT_AUTO
if the sheet is configured to peek automatically at 16:9 ratio keyline |
getSkipCollapsed
boolean getSkipCollapsed ()
Sets whether this bottom sheet should skip the collapsed state when it is being hidden after it is expanded once.
Related XML Attributes:
Returns | |
---|---|
boolean |
Whether the bottom sheet should skip the collapsed state. |
getState
int getState ()
Gets the current state of the bottom sheet.
Returns | |
---|---|
int |
One of STATE_EXPANDED , STATE_COLLAPSED , STATE_DRAGGING ,
STATE_SETTLING , and STATE_HIDDEN .
|
isHideable
boolean isHideable ()
Gets whether this bottom sheet can hide when it is swiped down.
Related XML Attributes:
Returns | |
---|---|
boolean |
true if this bottom sheet can hide. |
onInterceptTouchEvent
boolean onInterceptTouchEvent (CoordinatorLayout parent, V child, MotionEvent event)
Respond to CoordinatorLayout touch events before they are dispatched to child views.
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 onTouchEvent(CoordinatorLayout, V, MotionEvent)
method.
This method will be called regardless of the visibility of the associated child
of the behavior. If you only wish to handle touch events when the child is visible, you
should add a check to isShown()
on the given child.
The default implementation of this method always returns false.
Parameters | |
---|---|
parent |
CoordinatorLayout : the parent view currently receiving this touch event |
child |
V : the child view associated with this Behavior |
event |
MotionEvent : the MotionEvent describing the touch event being processed |
Returns | |
---|---|
boolean |
true if this Behavior would like to intercept and take over the event stream. The default always returns false. |
onLayoutChild
boolean onLayoutChild (CoordinatorLayout parent, V child, int layoutDirection)
Called when the parent CoordinatorLayout is about the lay out the given child view.
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
onDependentViewChanged(CoordinatorLayout, 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.
Parameters | |
---|---|
parent |
CoordinatorLayout : the parent CoordinatorLayout |
child |
V : child view to lay out |
layoutDirection |
int : the resolved layout direction for the CoordinatorLayout, such as
LAYOUT_DIRECTION_LTR or
LAYOUT_DIRECTION_RTL . |
Returns | |
---|---|
boolean |
true if the Behavior performed layout of the child view, false to request default layout behavior |
onNestedPreFling
boolean onNestedPreFling (CoordinatorLayout coordinatorLayout, V child, View target, float velocityX, float velocityY)
Called when a nested scrolling child is about to start a fling.
Any Behavior associated with the direct child of the CoordinatorLayout may elect
to accept the nested scroll as part of onStartNestedScroll(CoordinatorLayout, V, 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.
Parameters | |
---|---|
coordinatorLayout |
CoordinatorLayout : the CoordinatorLayout parent of the view this Behavior is
associated with |
child |
V : the child view of the CoordinatorLayout this Behavior is associated with |
target |
View : the descendant view of the CoordinatorLayout performing the nested scroll |
velocityX |
float : horizontal velocity of the attempted fling |
velocityY |
float : vertical velocity of the attempted fling |
Returns | |
---|---|
boolean |
true if the Behavior consumed the fling |
onNestedPreScroll
void onNestedPreScroll (CoordinatorLayout coordinatorLayout, V child, View target, int dx, int dy, int[] consumed)
This method is deprecated.
You should now override
onNestedPreScroll(CoordinatorLayout, View, View, int, int, int[], int)
.
This method will still continue to be called if the type is
TYPE_TOUCH
.
Parameters | |
---|---|
coordinatorLayout |
CoordinatorLayout |
child |
V |
target |
View |
dx |
int |
dy |
int |
consumed |
int |
onRestoreInstanceState
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 onSaveInstanceState(CoordinatorLayout, V)
. This function will never
be called with a null state.
Parameters | |
---|---|
parent |
CoordinatorLayout : the parent CoordinatorLayout |
child |
V : child view to restore from |
state |
Parcelable : The frozen state that had previously been returned by
onSaveInstanceState(CoordinatorLayout, V) . |
onSaveInstanceState
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. This state should only contain information that is not persistent or can not be reconstructed later.
Behavior state is only saved when both the parent CoordinatorLayout
and
a view using this behavior have valid IDs set.
Parameters | |
---|---|
parent |
CoordinatorLayout : the parent CoordinatorLayout |
child |
V : child view to restore from |
Returns | |
---|---|
Parcelable |
Returns a Parcelable object containing the behavior's current dynamic state. |
onStartNestedScroll
boolean onStartNestedScroll (CoordinatorLayout coordinatorLayout, V child, View directTargetChild, View target, int nestedScrollAxes)
This method is deprecated.
You should now override
onStartNestedScroll(CoordinatorLayout, View, View, View, int, int)
. This
method will still continue to be called if the type is TYPE_TOUCH
.
Parameters | |
---|---|
coordinatorLayout |
CoordinatorLayout |
child |
V |
directTargetChild |
View |
target |
View |
nestedScrollAxes |
int |
Returns | |
---|---|
boolean |
onStopNestedScroll
void onStopNestedScroll (CoordinatorLayout coordinatorLayout, V child, View target)
This method is deprecated.
You should now override
onStopNestedScroll(CoordinatorLayout, View, View, int)
. This method will still
continue to be called if the type is TYPE_TOUCH
.
Parameters | |
---|---|
coordinatorLayout |
CoordinatorLayout |
child |
V |
target |
View |
onTouchEvent
boolean onTouchEvent (CoordinatorLayout parent, V child, MotionEvent event)
Respond to CoordinatorLayout touch events after this Behavior has started
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.
This method will be called regardless of the visibility of the associated child
of the behavior. If you only wish to handle touch events when the child is visible, you
should add a check to isShown()
on the given child.
Parameters | |
---|---|
parent |
CoordinatorLayout : the parent view currently receiving this touch event |
child |
V : the child view associated with this Behavior |
event |
MotionEvent : the MotionEvent describing the touch event being processed |
Returns | |
---|---|
boolean |
true if this Behavior handled this touch event and would like to continue receiving events in this stream. The default always returns false. |
setBottomSheetCallback
void setBottomSheetCallback (BottomSheetBehavior.BottomSheetCallback callback)
Sets a callback to be notified of bottom sheet events.
Parameters | |
---|---|
callback |
BottomSheetBehavior.BottomSheetCallback : The callback to notify when bottom sheet events occur.
|
setHideable
void setHideable (boolean hideable)
Sets whether this bottom sheet can hide when it is swiped down.
Related XML Attributes:
Parameters | |
---|---|
hideable |
boolean : true to make this bottom sheet hideable. |
setPeekHeight
void setPeekHeight (int peekHeight)
Sets the height of the bottom sheet when it is collapsed.
Related XML Attributes:
Parameters | |
---|---|
peekHeight |
int : 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. |
setSkipCollapsed
void setSkipCollapsed (boolean skipCollapsed)
Sets whether this bottom sheet should skip the collapsed state when it is being hidden after it is expanded once. Setting this to true has no effect unless the sheet is hideable.
Related XML Attributes:
Parameters | |
---|---|
skipCollapsed |
boolean : True if the bottom sheet should skip the collapsed state. |
setState
void setState (int state)
Sets the state of the bottom sheet. The bottom sheet will transition to that state with animation.
Parameters | |
---|---|
state |
int : One of STATE_COLLAPSED , STATE_EXPANDED , or
STATE_HIDDEN .
|
Annotations
Interfaces
- AppBarLayout.OnOffsetChangedListener
- BaseTransientBottomBar.ContentViewCallback
- BottomNavigationView.OnNavigationItemReselectedListener
- BottomNavigationView.OnNavigationItemSelectedListener
- CoordinatorLayout.AttachedBehavior
- NavigationView.OnNavigationItemSelectedListener
- SwipeDismissBehavior.OnDismissListener
- TabLayout.OnTabSelectedListener
Classes
- AppBarLayout
- AppBarLayout.Behavior
- AppBarLayout.Behavior.DragCallback
- AppBarLayout.Behavior.SavedState
- AppBarLayout.LayoutParams
- AppBarLayout.ScrollingViewBehavior
- BaseTransientBottomBar
- BaseTransientBottomBar.BaseCallback
- BottomNavigationView
- BottomSheetBehavior
- BottomSheetBehavior.BottomSheetCallback
- BottomSheetBehavior.SavedState
- BottomSheetDialog
- BottomSheetDialogFragment
- CollapsingToolbarLayout
- CollapsingToolbarLayout.LayoutParams
- CoordinatorLayout
- CoordinatorLayout.Behavior
- CoordinatorLayout.LayoutParams
- CoordinatorLayout.SavedState
- FloatingActionButton
- FloatingActionButton.Behavior
- FloatingActionButton.OnVisibilityChangedListener
- NavigationView
- NavigationView.SavedState
- Snackbar
- Snackbar.Callback
- SwipeDismissBehavior
- TabItem
- TabLayout
- TabLayout.Tab
- TabLayout.TabLayoutOnPageChangeListener
- TabLayout.ViewPagerOnTabSelectedListener
- TextInputEditText
- TextInputLayout
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-04-11 UTC.