-
public class BottomSheetBehavior<V extends View> extends CoordinatorLayout.Behavior<V>
An interaction behavior plugin for a child view of CoordinatorLayout to make it work as a bottom sheet.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public abstract class
BottomSheetBehavior.BottomSheetCallback
Callback for monitoring events about bottom sheets.
public @interface
BottomSheetBehavior.State
public @interface
BottomSheetBehavior.SaveFlags
-
Field Summary
Fields Modifier and Type Field Description public final static int
STATE_DRAGGING
public final static int
STATE_SETTLING
public final static int
STATE_EXPANDED
public final static int
STATE_COLLAPSED
public final static int
STATE_HIDDEN
public final static int
STATE_HALF_EXPANDED
public final static int
PEEK_HEIGHT_AUTO
public final static int
SAVE_PEEK_HEIGHT
public final static int
SAVE_FIT_TO_CONTENTS
public final static int
SAVE_HIDEABLE
public final static int
SAVE_SKIP_COLLAPSED
public final static int
SAVE_ALL
public final static int
SAVE_NONE
private int
saveFlags
protected boolean
fitToContents
private int
peekHeight
private boolean
gestureInsetBottomIgnored
int
expandedOffset
float
halfExpandedRatio
boolean
hideable
protected boolean
skipCollapsed
protected boolean
draggable
int
state
-
Constructor Summary
Constructors Constructor Description BottomSheetBehavior()
BottomSheetBehavior(Context context, AttributeSet attrs)
-
Method Summary
Modifier and Type Method Description int
getSaveFlags()
Returns the save flags. void
setSaveFlags(int flags)
Sets save flags to be preserved in bottomsheet on configuration change. void
setFitToContents(boolean fitToContents)
Sets whether the height of the expanded sheet is determined by the height of its contents, orif it is expanded in two stages (half the height of the parent container, full height of parentcontainer). int
getPeekHeight()
Gets the height of the bottom sheet when it is collapsed. void
setPeekHeight(int peekHeight)
Sets the height of the bottom sheet when it is collapsed. void
setGestureInsetBottomIgnored(boolean gestureInsetBottomIgnored)
Sets whether this bottom sheet should adjust it's position based on the system gesture area onAndroid Q and above. int
getExpandedOffset()
Returns the current expanded offset. void
setExpandedOffset(int offset)
Determines the top offset of the BottomSheet in the STATE_EXPANDED state whenfitsToContent is false. float
getHalfExpandedRatio()
Gets the ratio for the height of the BottomSheet in the STATE_HALF_EXPANDED state. void
setHalfExpandedRatio(@FloatRange(from = 0.0f, to = 1.0f) float ratio)
Determines the height of the BottomSheet in the STATE_HALF_EXPANDED state. void
setHideable(boolean hideable)
Sets whether this bottom sheet can hide when it is swiped down. boolean
getSkipCollapsed()
Sets whether this bottom sheet should skip the collapsed state when it is being hidden after itis expanded once. void
setSkipCollapsed(boolean skipCollapsed)
Sets whether this bottom sheet should skip the collapsed state when it is being hidden after itis expanded once. void
setDraggable(boolean draggable)
Sets whether this bottom sheet is can be collapsed/expanded by dragging. int
getState()
Gets the current state of the bottom sheet. void
setState(int state)
Sets the state of the bottom sheet. Parcelable
onSaveInstanceState(@NonNull() CoordinatorLayout parent, @NonNull() V child)
Hook allowing a behavior to generate a representation of its internal state that can later beused to create a new instance with that same state. void
onRestoreInstanceState(@NonNull() CoordinatorLayout parent, @NonNull() V child, @NonNull() Parcelable state)
Hook allowing a behavior to re-apply a representation of its internal state that hadpreviously been generated by onSaveInstanceState. void
onAttachedToLayoutParams(@NonNull() CoordinatorLayout.LayoutParams layoutParams)
Called when the Behavior has been attached to a LayoutParams instance. void
onDetachedFromLayoutParams()
Called when the Behavior has been detached from its holding LayoutParams instance. boolean
onLayoutChild(@NonNull() CoordinatorLayout parent, @NonNull() V child, int layoutDirection)
Called when the parent CoordinatorLayout is about the lay out the given child view. boolean
onInterceptTouchEvent(@NonNull() CoordinatorLayout parent, @NonNull() V child, @NonNull() MotionEvent event)
Respond to CoordinatorLayout touch events before they are dispatched to child views. boolean
onTouchEvent(@NonNull() CoordinatorLayout parent, @NonNull() V child, @NonNull() MotionEvent event)
Respond to CoordinatorLayout touch events after this Behavior has started intercepting them. boolean
onStartNestedScroll(@NonNull() CoordinatorLayout coordinatorLayout, @NonNull() V child, @NonNull() View directTargetChild, @NonNull() View target, int axes, int type)
Called when a descendant of the CoordinatorLayout attempts to initiate a nested scroll. void
onNestedPreScroll(@NonNull() CoordinatorLayout coordinatorLayout, @NonNull() V child, @NonNull() View target, int dx, int dy, @NonNull() Array<int> consumed, int type)
Called when a nested scroll in progress is about to update, before the target has consumedany of the scrolled distance. void
onStopNestedScroll(@NonNull() CoordinatorLayout coordinatorLayout, @NonNull() V child, @NonNull() View target, int type)
Called when a nested scroll has ended. void
onNestedScroll(@NonNull() CoordinatorLayout coordinatorLayout, @NonNull() V child, @NonNull() View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type, @NonNull() Array<int> consumed)
Called when a nested scroll in progress has updated and the target has scrolled or attemptedto scroll. boolean
onNestedPreFling(@NonNull() CoordinatorLayout coordinatorLayout, @NonNull() V child, @NonNull() View target, float velocityX, float velocityY)
Called when a nested scrolling child is about to start a fling. boolean
isFitToContents()
boolean
isHideable()
Gets whether this bottom sheet can hide when it is swiped down. boolean
isDraggable()
void
setBottomSheetCallback(BottomSheetBehavior.BottomSheetCallback callback)
Sets a callback to be notified of bottom sheet events. void
addBottomSheetCallback(@NonNull() BottomSheetBehavior.BottomSheetCallback callback)
Adds a callback to be notified of bottom sheet events. void
removeBottomSheetCallback(@NonNull() BottomSheetBehavior.BottomSheetCallback callback)
Removes a previously added callback. boolean
isGestureInsetBottomIgnored()
Returns whether this bottom sheet should adjust it's position based on the system gesturearea. static <V extends View> BottomSheetBehavior<V>
from(@NonNull() V view)
A utility function to get the BottomSheetBehavior associated with the {@code view}
.-
Methods inherited from class tds.androidx.coordinatorlayout.widget.CoordinatorLayout.Behavior
blocksInteractionBelow, getInsetDodgeRect, getScrimColor, getScrimOpacity, getTag, layoutDependsOn, onApplyWindowInsets, onAttachedToLayoutParams, onDependentViewChanged, onDependentViewRemoved, onInterceptTouchEvent, onLayoutChild, onMeasureChild, onNestedFling, onNestedPreFling, onNestedPreScroll, onNestedPreScroll, onNestedScroll, onNestedScroll, onNestedScroll, onNestedScrollAccepted, onNestedScrollAccepted, onRequestChildRectangleOnScreen, onRestoreInstanceState, onStartNestedScroll, onStartNestedScroll, onStopNestedScroll, onStopNestedScroll, onTouchEvent, setTag
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Constructor Detail
-
BottomSheetBehavior
BottomSheetBehavior()
-
BottomSheetBehavior
BottomSheetBehavior(Context context, AttributeSet attrs)
-
-
Method Detail
-
getSaveFlags
int getSaveFlags()
Returns the save flags.
-
setSaveFlags
void setSaveFlags(int flags)
Sets save flags to be preserved in bottomsheet on configuration change.
- Parameters:
flags
- bitwise int of SAVE_PEEK_HEIGHT, SAVE_FIT_TO_CONTENTS, , SAVE_SKIP_COLLAPSED, SAVE_ALL and SAVE_NONE.
-
setFitToContents
void setFitToContents(boolean fitToContents)
Sets whether the height of the expanded sheet is determined by the height of its contents, orif it is expanded in two stages (half the height of the parent container, full height of parentcontainer). Default value is true.
- Parameters:
fitToContents
- whether or not to fit the expanded sheet to its contents.
-
getPeekHeight
int getPeekHeight()
Gets the height of the bottom sheet when it is collapsed.
-
setPeekHeight
void setPeekHeight(int peekHeight)
Sets the height of the bottom sheet when it is collapsed.
-
setGestureInsetBottomIgnored
void setGestureInsetBottomIgnored(boolean gestureInsetBottomIgnored)
Sets whether this bottom sheet should adjust it's position based on the system gesture area onAndroid Q and above.
Note: the bottom sheet will only adjust it's position if it would be unable to be scrolledupwards because the peekHeight is less than the gesture inset margins,(because that would causea gesture conflict), gesture navigation is enabled, and this
{@code ignoreGestureInsetBottom}
flag is false.
-
getExpandedOffset
int getExpandedOffset()
Returns the current expanded offset. If
{@code fitToContents}
is true, it will automaticallypick the offset depending on the height of the content.
-
setExpandedOffset
void setExpandedOffset(int offset)
Determines the top offset of the BottomSheet in the STATE_EXPANDED state whenfitsToContent is false. The default value is 0, which results in the sheet matching theparent's top.
-
getHalfExpandedRatio
@FloatRange(from = 0.0f, to = 1.0f) float getHalfExpandedRatio()
Gets the ratio for the height of the BottomSheet in the STATE_HALF_EXPANDED state.
-
setHalfExpandedRatio
void setHalfExpandedRatio(@FloatRange(from = 0.0f, to = 1.0f) float ratio)
Determines the height of the BottomSheet in the STATE_HALF_EXPANDED state. Thematerial guidelines recommended a value of 0.5, which results in the sheet filling half of theparent. The height of the BottomSheet will be smaller as this ratio is decreased and taller asit is increased. The default value is 0.5.
- Parameters:
ratio
- a float between 0 and 1, representing the STATE_HALF_EXPANDED ratio.
-
setHideable
void setHideable(boolean hideable)
Sets whether this bottom sheet can hide when it is swiped down.
- Parameters:
hideable
-{@code true}
to make this bottom sheet hideable.
-
getSkipCollapsed
boolean getSkipCollapsed()
Sets whether this bottom sheet should skip the collapsed state when it is being hidden after itis expanded once.
-
setSkipCollapsed
void setSkipCollapsed(boolean skipCollapsed)
Sets whether this bottom sheet should skip the collapsed state when it is being hidden after itis expanded once. Setting this to true has no effect unless the sheet is hideable.
- Parameters:
skipCollapsed
- True if the bottom sheet should skip the collapsed state.
-
setDraggable
void setDraggable(boolean draggable)
Sets whether this bottom sheet is can be collapsed/expanded by dragging. Note: When disablingdragging, an app will require to implement a custom way to expand/collapse the bottom sheet
- Parameters:
draggable
-{@code false}
to prevent dragging the sheet to collapse and expand
-
getState
int getState()
Gets the current state of the bottom sheet.
-
setState
void setState(int state)
Sets the state of the bottom sheet. The bottom sheet will transition to that state withanimation.
- Parameters:
state
- One of STATE_COLLAPSED, STATE_EXPANDED, STATE_HIDDEN,or STATE_HALF_EXPANDED.
-
onSaveInstanceState
@NonNull() Parcelable onSaveInstanceState(@NonNull() CoordinatorLayout parent, @NonNull() V child)
Hook allowing a behavior to generate a representation of its internal state that can later beused to create a new instance with that same state. This state should only containinformation that is not persistent or can not be reconstructed later.
Behavior state is only saved when both the parent CoordinatorLayout anda view using this behavior have valid IDs set.
- Parameters:
parent
- the parent CoordinatorLayoutchild
- child view to restore from
-
onRestoreInstanceState
void onRestoreInstanceState(@NonNull() CoordinatorLayout parent, @NonNull() V child, @NonNull() Parcelable state)
Hook allowing a behavior to re-apply a representation of its internal state that hadpreviously been generated by onSaveInstanceState. This function will never be calledwith a null state.
-
onAttachedToLayoutParams
void onAttachedToLayoutParams(@NonNull() CoordinatorLayout.LayoutParams layoutParams)
Called when the Behavior has been attached to a LayoutParams instance.
This will be called after the LayoutParams has been instantiated and can bemodified.
-
onDetachedFromLayoutParams
void onDetachedFromLayoutParams()
Called when the Behavior has been detached from its holding LayoutParams instance.
This will only be called if the Behavior has been explicitly removed from theLayoutParams instance via setBehavior. It will not be calledif the associated view is removed from the CoordinatorLayout or similar.
-
onLayoutChild
boolean onLayoutChild(@NonNull() CoordinatorLayout parent, @NonNull() 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 viewin place of the default child layout behavior. The Behavior's implementation can delegate tothe standard CoordinatorLayout measurement behavior by calling parent.onLayoutChild.
If a Behavior implements onDependentViewChanged to change the position of aview 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 viewafter its dependency has been laid out.- Parameters:
parent
- the parent CoordinatorLayoutchild
- child view to lay outlayoutDirection
- the resolved layout direction for the CoordinatorLayout, such as LAYOUT_DIRECTION_LTR or LAYOUT_DIRECTION_RTL.
-
onInterceptTouchEvent
boolean onInterceptTouchEvent(@NonNull() CoordinatorLayout parent, @NonNull() V child, @NonNull() 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 tointercept the rest of the event stream to take an action on its associated child view. Thismethod will return false until it detects the proper intercept conditions, then return trueonce those conditions have occurred.
Once a Behavior intercepts touch events, the rest of the event stream willbe sent to the onTouchEvent method.
This method will be called regardless of the visibility of the associated childof the behavior. If you only wish to handle touch events when the child is visible, youshould add a check to isShown on the given child.
The default implementation of this method always returns false.
- Parameters:
parent
- the parent view currently receiving this touch eventchild
- the child view associated with this Behavior
-
onTouchEvent
boolean onTouchEvent(@NonNull() CoordinatorLayout parent, @NonNull() V child, @NonNull() MotionEvent event)
Respond to CoordinatorLayout touch events after this Behavior has started intercepting them.
Behaviors may intercept touch events in order to help the CoordinatorLayoutmanipulate its child views. For example, a Behavior may allow a user to drag a UI pane openor closed. This method should perform actual mutations of view layout state.
This method will be called regardless of the visibility of the associated childof the behavior. If you only wish to handle touch events when the child is visible, youshould add a check to isShown on the given child.
- Parameters:
parent
- the parent view currently receiving this touch eventchild
- the child view associated with this Behavior
-
onStartNestedScroll
boolean onStartNestedScroll(@NonNull() CoordinatorLayout coordinatorLayout, @NonNull() V child, @NonNull() View directTargetChild, @NonNull() View target, int axes, int type)
Called when a descendant of the CoordinatorLayout attempts to initiate a nested scroll.
Any Behavior associated with any direct child of the CoordinatorLayout may respondto this event and return true to indicate that the CoordinatorLayout should act as a nestedscrolling parent for this scroll. Only Behaviors that return true from this method willreceive subsequent nested scroll events.
- Parameters:
coordinatorLayout
- the CoordinatorLayout parent of the view this Behavior is associatedwithchild
- the child view of the CoordinatorLayout this Behavior is associated withdirectTargetChild
- the child view of the CoordinatorLayout that either is or containsthe target of the nested scroll operationtarget
- the descendant view of the CoordinatorLayout initiating the nested scrollaxes
- the axes that this nested scroll applies to.type
- the type of input which cause this scroll event
-
onNestedPreScroll
void onNestedPreScroll(@NonNull() CoordinatorLayout coordinatorLayout, @NonNull() V child, @NonNull() View target, int dx, int dy, @NonNull() Array<int> consumed, int type)
Called when a nested scroll in progress is about to update, before the target has consumedany of the scrolled distance.
Any Behavior associated with the direct child of the CoordinatorLayout may electto accept the nested scroll as part of onStartNestedScroll. Each Behavior thatreturned true will receive subsequent nested scroll events for that nested scroll.
onNestedPreScroll
is called each time the nested scroll is updatedby the nested scrolling child, before the nested scrolling child has consumed the scrolldistance itself. Each Behavior responding to the nested scroll will receive the samevalues. The CoordinatorLayout will report as consumed the maximum number of pixels ineither direction that any Behavior responding to the nested scroll reported as consumed.- Parameters:
coordinatorLayout
- the CoordinatorLayout parent of the view this Behavior is associatedwithchild
- 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.type
- the type of input which cause this scroll event
-
onStopNestedScroll
void onStopNestedScroll(@NonNull() CoordinatorLayout coordinatorLayout, @NonNull() V child, @NonNull() View target, int type)
Called when a nested scroll has ended.
Any Behavior associated with any direct child of the CoordinatorLayout may electto accept the nested scroll as part of onStartNestedScroll. Each Behavior thatreturned true will receive subsequent nested scroll events for that nested scroll.
onStopNestedScroll
marks the end of a single nested scroll eventsequence. This is a good place to clean up any state related to the nested scroll.- Parameters:
coordinatorLayout
- the CoordinatorLayout parent of the view this Behavior is associatedwithchild
- the child view of the CoordinatorLayout this Behavior is associated withtarget
- the descendant view of the CoordinatorLayout that initiated the nested scrolltype
- the type of input which cause this scroll event
-
onNestedScroll
void onNestedScroll(@NonNull() CoordinatorLayout coordinatorLayout, @NonNull() V child, @NonNull() View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type, @NonNull() Array<int> consumed)
Called when a nested scroll in progress has updated and the target has scrolled or attemptedto scroll.
Any Behavior associated with the direct child of the CoordinatorLayout may electto accept the nested scroll as part of onStartNestedScroll. Each Behavior thatreturned true will receive subsequent nested scroll events for that nested scroll.
onNestedScroll
is called each time the nested scroll is updated by thenested scrolling child, with both consumed and unconsumed components of the scroll suppliedin pixels. Each Behavior responding to the nested scroll will receive the samevalues.- Parameters:
coordinatorLayout
- the CoordinatorLayout parent of the view this Behavior is associatedwithchild
- the child view of the CoordinatorLayout this Behavior is associated withtarget
- the descendant view of the CoordinatorLayout performing the nested scrolldxConsumed
- horizontal pixels consumed by the target's own scrolling operationdyConsumed
- vertical pixels consumed by the target's own scrolling operationdxUnconsumed
- horizontal pixels not consumed by the target's own scrolling operation,but requested by the userdyUnconsumed
- vertical pixels not consumed by the target's own scrolling operation, butrequested by the usertype
- the type of input which cause this scroll eventconsumed
- output.
-
onNestedPreFling
boolean onNestedPreFling(@NonNull() CoordinatorLayout coordinatorLayout, @NonNull() V child, @NonNull() 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 electto accept the nested scroll as part of onStartNestedScroll. Each Behavior thatreturned true will receive subsequent nested scroll events for that nested scroll.
onNestedPreFling
is called when the current nested scrolling child viewdetects the proper conditions for a fling, but it has not acted on it yet. A Behavior canreturn 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
- the CoordinatorLayout parent of the view this Behavior is associatedwithchild
- 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 fling
-
isFitToContents
boolean isFitToContents()
-
isHideable
boolean isHideable()
Gets whether this bottom sheet can hide when it is swiped down.
-
isDraggable
boolean isDraggable()
-
setBottomSheetCallback
@Deprecated() void setBottomSheetCallback(BottomSheetBehavior.BottomSheetCallback callback)
Sets a callback to be notified of bottom sheet events.
- Parameters:
callback
- The callback to notify when bottom sheet events occur.
-
addBottomSheetCallback
void addBottomSheetCallback(@NonNull() BottomSheetBehavior.BottomSheetCallback callback)
Adds a callback to be notified of bottom sheet events.
- Parameters:
callback
- The callback to notify when bottom sheet events occur.
-
removeBottomSheetCallback
void removeBottomSheetCallback(@NonNull() BottomSheetBehavior.BottomSheetCallback callback)
Removes a previously added callback.
- Parameters:
callback
- The callback to remove.
-
isGestureInsetBottomIgnored
boolean isGestureInsetBottomIgnored()
Returns whether this bottom sheet should adjust it's position based on the system gesturearea.
-
from
@NonNull() static <V extends View> BottomSheetBehavior<V> from(@NonNull() V view)
A utility function to get the BottomSheetBehavior associated with the
{@code view}
.- Parameters:
view
- The View with BottomSheetBehavior.
-
-
-
-