Google is committed to advancing racial equity for Black communities. See how.

Behavior

abstract class Behavior<V : View!>
kotlin.Any
   ↳ androidx.coordinatorlayout.widget.CoordinatorLayout.Behavior

Interaction behavior plugin for child views of CoordinatorLayout.

A Behavior implements one or more interactions that a user can take on a child view. These interactions may include drags, swipes, flings, or any other gestures.

Summary

Public constructors

Default constructor for instantiating Behaviors.

<init>(context: Context!, attrs: AttributeSet!)

Default constructor for inflating Behaviors from layout.

Public methods
open Boolean
blocksInteractionBelow(@NonNull parent: CoordinatorLayout, @NonNull child: V)

Determine whether interaction with views behind the given child in the child order should be blocked.

open Boolean
getInsetDodgeRect(@NonNull parent: CoordinatorLayout, @NonNull child: V, @NonNull rect: Rect)

Called when a view is set to dodge view insets.

open Int
getScrimColor(@NonNull parent: CoordinatorLayout, @NonNull child: V)

Supply a scrim color that will be painted behind the associated child view.

open Float
getScrimOpacity(@NonNull parent: CoordinatorLayout, @NonNull child: V)

Determine the current opacity of the scrim behind a given child view

open static Any?
getTag(@NonNull child: View)

Get the behavior-specific tag object with the given child view.

open Boolean
layoutDependsOn(@NonNull parent: CoordinatorLayout, @NonNull child: V, @NonNull dependency: View)

Determine whether the supplied child view has another specific sibling view as a layout dependency.

open WindowInsetsCompat
onApplyWindowInsets(@NonNull coordinatorLayout: CoordinatorLayout, @NonNull child: V, @NonNull insets: WindowInsetsCompat)

Called when the window insets have changed.

open Unit

Called when the Behavior has been attached to a LayoutParams instance.

open Boolean
onDependentViewChanged(@NonNull parent: CoordinatorLayout, @NonNull child: V, @NonNull dependency: View)

Respond to a change in a child's dependent view

open Unit
onDependentViewRemoved(@NonNull parent: CoordinatorLayout, @NonNull child: V, @NonNull dependency: View)

Respond to a child's dependent view being removed.

open Unit

Called when the Behavior has been detached from its holding LayoutParams instance.

open Boolean
onInterceptTouchEvent(@NonNull parent: CoordinatorLayout, @NonNull child: V, @NonNull ev: MotionEvent)

Respond to CoordinatorLayout touch events before they are dispatched to child views.

open Boolean
onLayoutChild(@NonNull parent: CoordinatorLayout, @NonNull child: V, layoutDirection: Int)

Called when the parent CoordinatorLayout is about the lay out the given child view.

open Boolean
onMeasureChild(@NonNull parent: CoordinatorLayout, @NonNull child: V, parentWidthMeasureSpec: Int, widthUsed: Int, parentHeightMeasureSpec: Int, heightUsed: Int)

Called when the parent CoordinatorLayout is about to measure the given child view.

open Boolean
onNestedFling(@NonNull coordinatorLayout: CoordinatorLayout, @NonNull child: V, @NonNull target: View, velocityX: Float, velocityY: Float, consumed: Boolean)

Called when a nested scrolling child is starting a fling or an action that would be a fling.

open Boolean
onNestedPreFling(@NonNull coordinatorLayout: CoordinatorLayout, @NonNull child: V, @NonNull target: View, velocityX: Float, velocityY: Float)

Called when a nested scrolling child is about to start a fling.

open Unit
onNestedPreScroll(@NonNull coordinatorLayout: CoordinatorLayout, @NonNull child: V, @NonNull target: View, dx: Int, dy: Int, @NonNull consumed: IntArray)

open Unit
onNestedPreScroll(@NonNull coordinatorLayout: CoordinatorLayout, @NonNull child: V, @NonNull target: View, dx: Int, dy: Int, @NonNull consumed: IntArray, type: Int)

Called when a nested scroll in progress is about to update, before the target has consumed any of the scrolled distance.

open Unit
onNestedScroll(@NonNull coordinatorLayout: CoordinatorLayout, @NonNull child: V, @NonNull target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int)

open Unit
onNestedScroll(@NonNull coordinatorLayout: CoordinatorLayout, @NonNull child: V, @NonNull target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, type: Int)

open Unit
onNestedScroll(@NonNull coordinatorLayout: CoordinatorLayout, @NonNull child: V, @NonNull target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, type: Int, @NonNull consumed: IntArray)

Called when a nested scroll in progress has updated and the target has scrolled or attempted to scroll.

open Unit
onNestedScrollAccepted(@NonNull coordinatorLayout: CoordinatorLayout, @NonNull child: V, @NonNull directTargetChild: View, @NonNull target: View, axes: Int)

open Unit
onNestedScrollAccepted(@NonNull coordinatorLayout: CoordinatorLayout, @NonNull child: V, @NonNull directTargetChild: View, @NonNull target: View, axes: Int, type: Int)

Called when a nested scroll has been accepted by the CoordinatorLayout.

open Boolean
onRequestChildRectangleOnScreen(@NonNull coordinatorLayout: CoordinatorLayout, @NonNull child: V, @NonNull rectangle: Rect, immediate: Boolean)

Called when a child of the view associated with this behavior wants a particular rectangle to be positioned onto the screen.

open Unit
onRestoreInstanceState(@NonNull parent: CoordinatorLayout, @NonNull child: V, @NonNull state: Parcelable)

Hook allowing a behavior to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState.

open Parcelable?
onSaveInstanceState(@NonNull parent: CoordinatorLayout, @NonNull child: V)

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.

open Boolean
onStartNestedScroll(@NonNull coordinatorLayout: CoordinatorLayout, @NonNull child: V, @NonNull directTargetChild: View, @NonNull target: View, axes: Int)

open Boolean
onStartNestedScroll(@NonNull coordinatorLayout: CoordinatorLayout, @NonNull child: V, @NonNull directTargetChild: View, @NonNull target: View, axes: Int, type: Int)

Called when a descendant of the CoordinatorLayout attempts to initiate a nested scroll.

open Unit
onStopNestedScroll(@NonNull coordinatorLayout: CoordinatorLayout, @NonNull child: V, @NonNull target: View)

open Unit
onStopNestedScroll(@NonNull coordinatorLayout: CoordinatorLayout, @NonNull child: V, @NonNull target: View, type: Int)

Called when a nested scroll has ended.

open Boolean
onTouchEvent(@NonNull parent: CoordinatorLayout, @NonNull child: V, @NonNull ev: MotionEvent)

Respond to CoordinatorLayout touch events after this Behavior has started intercepting them.

open static Unit
setTag(@NonNull child: View, @Nullable tag: Any?)

Associate a Behavior-specific tag object with the given child view.

Public constructors

<init>

Behavior()

Default constructor for instantiating Behaviors.

<init>

Behavior(
    context: Context!,
    attrs: AttributeSet!)

Default constructor for inflating Behaviors from layout. The Behavior will have the opportunity to parse specially defined layout parameters. These parameters will appear on the child view tag.

Parameters
context Context!:
attrs AttributeSet!:

Public methods

blocksInteractionBelow

open fun blocksInteractionBelow(
    @NonNull parent: CoordinatorLayout,
    @NonNull child: V
): Boolean

Determine whether interaction with views behind the given child in the child order should be blocked.

The default implementation returns true if getScrimOpacity(CoordinatorLayout, View) would return > 0.0f.

Parameters
parent CoordinatorLayout: the parent view of the given child
child V: the child view to test
Return
Boolean true if getScrimOpacity(CoordinatorLayout, View) would return > 0.0f.

getInsetDodgeRect

open fun getInsetDodgeRect(
    @NonNull parent: CoordinatorLayout,
    @NonNull child: V,
    @NonNull rect: Rect
): Boolean

Called when a view is set to dodge view insets.

This method allows a behavior to update the rectangle that should be dodged. The rectangle should be in the parent's coordinate system and within the child's bounds. If not, a IllegalArgumentException is thrown.

Parameters
parent 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
rect Rect: the rect to update with the dodge rectangle
Return
Boolean true the rect was updated, false if we should use the child's bounds