CoordinatorLayout.Behavior


public abstract class CoordinatorLayout.Behavior<V extends View>


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.

Parameters
<V extends View>

The View type that this Behavior operates on

Summary

Public constructors

Default constructor for instantiating Behaviors.

Default constructor for inflating Behaviors from layout.

Public methods

boolean

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

boolean
getInsetDodgeRect(
    @NonNull CoordinatorLayout parent,
    @NonNull V child,
    @NonNull Rect rect
)

Called when a view is set to dodge view insets.

@ColorInt int

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

@FloatRange(from = 0, to = 1) float

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

static @Nullable Object

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

boolean
layoutDependsOn(
    @NonNull CoordinatorLayout parent,
    @NonNull V child,
    @NonNull View dependency
)

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

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

Called when the window insets have changed.

void

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

boolean
onDependentViewChanged(
    @NonNull CoordinatorLayout parent,
    @NonNull V child,
    @NonNull View dependency
)

Respond to a change in a child's dependent view

void
onDependentViewRemoved(
    @NonNull CoordinatorLayout parent,
    @NonNull V child,
    @NonNull View dependency
)

Respond to a child's dependent view being removed.

void

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

boolean
onInterceptTouchEvent(
    @NonNull CoordinatorLayout parent,
    @NonNull V child,
    @NonNull MotionEvent ev
)

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

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
onMeasureChild(
    @NonNull CoordinatorLayout parent,
    @NonNull V child,
    int parentWidthMeasureSpec,
    int widthUsed,
    int parentHeightMeasureSpec,
    int heightUsed
)

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

boolean
onNestedFling(
    @NonNull CoordinatorLayout coordinatorLayout,
    @NonNull V child,
    @NonNull View target,
    float velocityX,
    float velocityY,
    boolean consumed
)

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

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.

void
onNestedPreScroll(
    @NonNull CoordinatorLayout coordinatorLayout,
    @NonNull V child,
    @NonNull View target,
    int dx,
    int dy,
    @NonNull int[] consumed
)

This method is deprecated.

You should now override onNestedPreScroll.

void
onNestedPreScroll(
    @NonNull CoordinatorLayout coordinatorLayout,
    @NonNull V child,
    @NonNull View target,
    int dx,
    int dy,
    @NonNull int[] consumed,
    int type
)

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

void
onNestedScroll(
    @NonNull CoordinatorLayout coordinatorLayout,
    @NonNull V child,
    @NonNull View target,
    int dxConsumed,
    int dyConsumed,
    int dxUnconsumed,
    int dyUnconsumed
)

This method is deprecated.

You should now override onNestedScroll.

void
onNestedScroll(
    @NonNull CoordinatorLayout coordinatorLayout,
    @NonNull V child,
    @NonNull View target,
    int dxConsumed,
    int dyConsumed,
    int dxUnconsumed,
    int dyUnconsumed,
    int type
)

This method is deprecated.

You should now override onNestedScroll.

void
onNestedScroll(
    @NonNull CoordinatorLayout coordinatorLayout,
    @NonNull V child,
    @NonNull View target,
    int dxConsumed,
    int dyConsumed,
    int dxUnconsumed,
    int dyUnconsumed,
    int type,
    @NonNull int[] consumed
)

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

void
onNestedScrollAccepted(
    @NonNull CoordinatorLayout coordinatorLayout,
    @NonNull V child,
    @NonNull View directTargetChild,
    @NonNull View target,
    int axes
)

This method is deprecated.

You should now override onNestedScrollAccepted.

void
onNestedScrollAccepted(
    @NonNull CoordinatorLayout coordinatorLayout,
    @NonNull V child,
    @NonNull View directTargetChild,
    @NonNull View target,
    int axes,
    int type
)

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

boolean
onRequestChildRectangleOnScreen(
    @NonNull CoordinatorLayout coordinatorLayout,
    @NonNull V child,
    @NonNull Rect rectangle,
    boolean immediate
)

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

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 had previously been generated by onSaveInstanceState.

@Nullable Parcelable

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(
    @NonNull CoordinatorLayout coordinatorLayout,
    @NonNull V child,
    @NonNull View directTargetChild,
    @NonNull View target,
    int axes
)

This method is deprecated.

You should now override 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.

void
onStopNestedScroll(
    @NonNull CoordinatorLayout coordinatorLayout,
    @NonNull V child,
    @NonNull View target
)

This method is deprecated.

You should now override onStopNestedScroll.

void
onStopNestedScroll(
    @NonNull CoordinatorLayout coordinatorLayout,
    @NonNull V child,
    @NonNull View target,
    int type
)

Called when a nested scroll has ended.

boolean
onTouchEvent(
    @NonNull CoordinatorLayout parent,
    @NonNull V child,
    @NonNull MotionEvent ev
)

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

static void
setTag(@NonNull View child, @Nullable Object tag)

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

Public constructors

Behavior

Added in 1.1.0
public Behavior()

Default constructor for instantiating Behaviors.

Behavior

Added in 1.1.0
public Behavior(@NonNull Context context, @Nullable AttributeSet attrs)

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
@NonNull Context context
@Nullable AttributeSet attrs

Public methods

blocksInteractionBelow

Added in 1.1.0
public boolean blocksInteractionBelow(@NonNull CoordinatorLayout parent, @NonNull V child)

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

The default implementation returns true if getScrimOpacity would return >0.0f.

Parameters
@NonNull CoordinatorLayout parent

the parent view of the given child

@NonNull V child

the child view to test

Returns
boolean

true if getScrimOpacity would return >0.0f.

getInsetDodgeRect

Added in 1.1.0
public boolean getInsetDodgeRect(
    @NonNull CoordinatorLayout parent,
    @NonNull V child,
    @NonNull Rect rect
)

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
@NonNull CoordinatorLayout parent

the CoordinatorLayout parent of the view this Behavior is associated with

@NonNull V child

the child view of the CoordinatorLayout this Behavior is associated with

@NonNull Rect rect

the rect to update with the dodge rectangle

Returns
boolean

true the rect was updated, false if we should use the child's bounds

getScrimColor

Added in 1.1.0
public @ColorInt int getScrimColor(@NonNull CoordinatorLayout parent, @NonNull V child)

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

A scrim may be used to indicate that the other elements beneath it are not currently interactive or actionable, drawing user focus and attention to the views above the scrim.

The default implementation returns BLACK.

Parameters
@NonNull CoordinatorLayout parent

the parent view of the given child

@NonNull V child

the child view above the scrim

Returns
@ColorInt int

the desired scrim color in 0xAARRGGBB format. The default return value is BLACK.

See also
getScrimOpacity

getScrimOpacity

Added in 1.1.0
public @FloatRange(from = 0, to = 1) float getScrimOpacity(@NonNull CoordinatorLayout parent, @NonNull V child)

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

A scrim may be used to indicate that the other elements beneath it are not currently interactive or actionable, drawing user focus and attention to the views above the scrim.

The default implementation returns 0.0f.

Parameters
@NonNull CoordinatorLayout parent

the parent view of the given child

@NonNull V child

the child view above the scrim

Returns
@FloatRange(from = 0, to = 1) float

the desired scrim opacity from 0.0f to 1.0f. The default return value is 0.0f.

getTag

Added in 1.1.0
public static @Nullable Object getTag(@NonNull View child)

Get the behavior-specific tag object with the given child view. This object is stored with the child view's LayoutParams.

Parameters
@NonNull View child

child view to get tag with

Returns
@Nullable Object

the previously stored tag object

layoutDependsOn

Added in 1.1.0
public boolean layoutDependsOn(
    @NonNull CoordinatorLayout parent,
    @NonNull V child,
    @NonNull View dependency
)

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

This method will be called at least once in response to a layout request. If it returns true for a given child and dependency view pair, the parent CoordinatorLayout will:

  1. Always lay out this child after the dependent child is laid out, regardless of child order.
  2. Call onDependentViewChanged when the dependency view's layout or position changes.
Parameters
@NonNull CoordinatorLayout parent

the parent view of the given child

@NonNull V child

the child view to test

@NonNull View dependency

the proposed dependency of child

Returns
boolean

true if child's layout depends on the proposed dependency's layout, false otherwise

onApplyWindowInsets

Added in 1.1.0
public @NonNull WindowInsetsCompat onApplyWindowInsets(
    @NonNull CoordinatorLayout coordinatorLayout,
    @