CoordinatorLayout.Behavior

public static abstract class CoordinatorLayout.Behavior
extends Object

java.lang.Object
   ↳ androidx.coordinatorlayout.widget.CoordinatorLayout.Behavior<V extends android.view.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.

Summary

Public constructors

Behavior()

Default constructor for instantiating Behaviors.

Behavior(Context context, AttributeSet attrs)

Default constructor for inflating Behaviors from layout.

Public methods

boolean blocksInteractionBelow(CoordinatorLayout parent, V child)

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

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

Called when a view is set to dodge view insets.

int getScrimColor(CoordinatorLayout parent, V child)

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

float getScrimOpacity(CoordinatorLayout parent, 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.

static Object getTag(View child)

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

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

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

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

Called when the window insets have changed.

void onAttachedToLayoutParams(CoordinatorLayout.LayoutParams params)

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

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

Respond to a change in a child's dependent view

This method is called whenever a dependent view changes in size or position outside of the standard layout flow.

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

Respond to a child's dependent view being removed.

void onDetachedFromLayoutParams()

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

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

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 onMeasureChild(CoordinatorLayout parent, 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(CoordinatorLayout coordinatorLayout, V child, 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(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, int type)

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

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 ViewCompat.TYPE_TOUCH.

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

This method is deprecated. You should now override onNestedScroll(CoordinatorLayout, View, View, int, int, int, int, int, int[]). This method will still continue to be called if onNestedScroll(CoordinatorLayout, View, View, int, int, int, int, int, int[]) is not overridden.

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

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

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

This method is deprecated. You should now override onNestedScroll(CoordinatorLayout, View, View, int, int, int, int, int, int[]). This method will still continue to be called if neither onNestedScroll(CoordinatorLayout, View, View, int, int, int, int, int, int[]) nor CoordinatorLayout.onNestedScroll(View, int, int, int, int, int) are overridden and the type is ViewCompat.TYPE_TOUCH.

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

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

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

This method is deprecated. You should now override onNestedScrollAccepted(CoordinatorLayout, View, View, View, int, int). This method will still continue to be called if the type is ViewCompat.TYPE_TOUCH.

boolean onRequestChildRectangleOnScreen(CoordinatorLayout coordinatorLayout, V child, 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(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).

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 axes, int type)

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

boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, V child, View directTargetChild, View target, int axes)

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 ViewCompat.TYPE_TOUCH.

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

Called when a nested scroll has ended.

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 ViewCompat.TYPE_TOUCH.

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

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

static void setTag(View child, Object tag)

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

Inherited methods

Public constructors

Behavior

public Behavior ()

Default constructor for instantiating Behaviors.

Behavior

public Behavior (Context context, 
                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.

Public methods

blocksInteractionBelow

public boolean blocksInteractionBelow (CoordinatorLayout parent, 
                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(CoordinatorLayout, View) would return > 0.0f.

Parameters
parent CoordinatorLayout: the parent view of the given child

child V: the child view to test

Returns
boolean true if getScrimOpacity(CoordinatorLayout, View) would return > 0.0f.

getInsetDodgeRect

public boolean getInsetDodgeRect (CoordinatorLayout parent, 
                V child, 
                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
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

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

getScrimColor

public int getScrimColor (CoordinatorLayout parent, 
                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 Color.BLACK.

Parameters
parent CoordinatorLayout: the parent view of the given child

child V: the child view above the scrim

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

getScrimOpacity

public float getScrimOpacity (CoordinatorLayout parent, 
                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
parent CoordinatorLayout: the parent view of the given child

child V: the child view above the scrim

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

Value is between 0.0 and 1.0 inclusive.

getTag

public static Object getTag (View child)

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

Parameters
child View: child view to get tag with

Returns
Object the previously stored tag object

layoutDependsOn

public boolean layoutDependsOn (CoordinatorLayout parent, 
                V child, 
                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(CoordinatorLayout, V, View) when the dependency view's layout or position changes.

Parameters
parent CoordinatorLayout: the parent view of the given child

child V: the child view to test

dependency View: the proposed dependency of child

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

onApplyWindowInsets

public WindowInsetsCompat onApplyWindowInsets (CoordinatorLayout coordinatorLayout, 
                V child, 
                WindowInsetsCompat insets)

Called when the window insets have changed.

Any Behavior associated with the direct child of the CoordinatorLayout may elect to handle the window inset change on behalf of it's associated view.

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

insets WindowInsetsCompat: the new window insets.

Returns
WindowInsetsCompat The insets supplied, minus any insets that were consumed

onAttachedToLayoutParams

public void onAttachedToLayoutParams (CoordinatorLayout.LayoutParams params)

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

This will be called after the LayoutParams has been instantiated and can be modified.

Parameters
params CoordinatorLayout.LayoutParams: the LayoutParams instance that this Behavior has been attached to

onDependentViewChanged

public boolean onDependentViewChanged (CoordinatorLayout parent, 
                V child, 
                View dependency)

Respond to a change in a child's dependent view

This method is called whenever a dependent view changes in size or position outside of the standard layout flow. A Behavior may use this method to appropriately update the child view in response.

A view's dependency is determined by layoutDependsOn(CoordinatorLayout, View, View) or if child has set another view as it's anchor.

Note that if a Behavior changes the layout of a child via this method, it should also be able to reconstruct the correct position in onLayoutChild. onDependentViewChanged will not be called during normal layout since the layout of each child view will always happen in dependency order.

If the Behavior changes the child view's size or position, it should return true. The default implementation returns false.

Parameters
parent CoordinatorLayout: the parent view of the given child

<