Package 

Class 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.

    • Constructor Detail

      • CoordinatorLayout.Behavior

        CoordinatorLayout.Behavior()
        Default constructor for instantiating Behaviors.
      • CoordinatorLayout.Behavior

        CoordinatorLayout.Behavior(Context context, AttributeSet attrs)
        Default constructor for inflating Behaviors from layout.
    • Method Detail

      • onAttachedToLayoutParams

         void onAttachedToLayoutParams(@NonNull() 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 bemodified.

        Parameters:
        params - the LayoutParams instance that this Behavior has been attached to
      • 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.

      • onInterceptTouchEvent

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

        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 event
        child - the child view associated with this Behavior
        ev - the MotionEvent describing the touch event being processed
      • onTouchEvent

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

        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 event
        child - the child view associated with this Behavior
        ev - the MotionEvent describing the touch event being processed
      • getScrimColor

        @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 currentlyinteractive or actionable, drawing user focus and attention to the views above the scrim.

        The default implementation returns BLACK.

        Parameters:
        parent - the parent view of the given child
        child - the child view above the scrim
      • getScrimOpacity

        @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 currentlyinteractive or actionable, drawing user focus and attention to the views above the scrim.

        The default implementation returns 0.0f.

        Parameters:
        parent - the parent view of the given child
        child - the child view above the scrim
      • blocksInteractionBelow

         boolean blocksInteractionBelow(@NonNull() CoordinatorLayout parent, @NonNull() V child)

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

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

        Parameters:
        parent - the parent view of the given child
        child - the child view to test
      • layoutDependsOn

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

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

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

        • Always lay out this child after the dependent child is laid out, regardlessof child order.
        • Call onDependentViewChanged when the dependency view's layout orposition changes.
        Parameters:
        parent - the parent view of the given child
        child - the child view to test
        dependency - the proposed dependency of child
      • onDependentViewChanged

         boolean onDependentViewChanged(@NonNull() CoordinatorLayout parent, @NonNull() V child, @NonNull() 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 outsideof the standard layout flow. A Behavior may use this method to appropriately update the childview in response.

        A view's dependency is determined by layoutDependsOn or if {@code child} has set anotherview as it's anchor.

        Note that if a Behavior changes the layout of a child via this method, it shouldalso be able to reconstruct the correct position in onLayoutChild.onDependentViewChanged will not be called during normal layout sincethe 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 - the parent view of the given child
        child - the child view to manipulate
        dependency - the dependent view that changed
      • onDependentViewRemoved

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

        Respond to a child's dependent view being removed.

        This method is called after a dependent view has been removed from the parent.A Behavior may use this method to appropriately update the child view in response.

        A view's dependency is determined by layoutDependsOn or if {@code child} has set anotherview as it's anchor.

        Parameters:
        parent - the parent view of the given child
        child - the child view to manipulate
        dependency - the dependent view that has been removed
      • onMeasureChild

         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.

        This method can be used to perform custom or modified measurement of a child viewin place of the default child measurement behavior. The Behavior's implementation candelegate to the standard CoordinatorLayout measurement behavior by calling parent.onMeasureChild.

        Parameters:
        parent - the parent CoordinatorLayout
        child - the child to measure
        parentWidthMeasureSpec - the width requirements for this view
        widthUsed - extra space that has been used up by the parent horizontally (possibly byother children of the parent)
        parentHeightMeasureSpec - the height requirements for this view
        heightUsed - extra space that has been used up by the parent vertically (possibly byother children of the parent)
      • 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 implementonLayoutChild 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 CoordinatorLayout
        child - child view to lay out
        layoutDirection - the resolved layout direction for the CoordinatorLayout, such as LAYOUT_DIRECTION_LTR or LAYOUT_DIRECTION_RTL.
      • setTag

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

        Associate a Behavior-specific tag object with the given child view. This object will bestored with the child view's LayoutParams.

        Parameters:
        child - child view to set tag with
        tag - tag object to set
      • getTag

        @Nullable() static Object getTag(@NonNull() View child)

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

        Parameters:
        child - child view to get tag with
      • 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 associatedwith
        child - the child view of the CoordinatorLayout this Behavior is associated with
        directTargetChild - the child view of the CoordinatorLayout that either is or containsthe target of the nested scroll operation
        target - the descendant view of the CoordinatorLayout initiating the nested scroll
        axes - the axes that this nested scroll applies to.
        type - the type of input which cause this scroll event
      • 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.

        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.

        Parameters:
        coordinatorLayout - the CoordinatorLayout parent of the view this Behavior is associatedwith
        child - the child view of the CoordinatorLayout this Behavior is associated with
        directTargetChild - the child view of the CoordinatorLayout that either is or containsthe target of the nested scroll operation
        target - the descendant view of the CoordinatorLayout initiating the nested scroll
        axes - the axes that this nested scroll applies to.
        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 associatedwith
        child - the child view of the CoordinatorLayout this Behavior is associated with
        target - the descendant view of the CoordinatorLayout that initiated the nested scroll
        type - 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 associatedwith
        child - the child view of the CoordinatorLayout this Behavior is associated with
        target - the descendant view of the CoordinatorLayout performing the nested scroll
        dxConsumed - horizontal pixels consumed by the target's own scrolling operation
        dyConsumed - vertical pixels consumed by the target's own scrolling operation
        dxUnconsumed - horizontal pixels not consumed by the target's own scrolling operation,but requested by the user
        dyUnconsumed - vertical pixels not consumed by the target's own scrolling operation, butrequested by the user
        type - the type of input which cause this scroll event
        consumed - output.
      • 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 associatedwith
        child - the child view of the CoordinatorLayout this Behavior is associated with
        target - the descendant view of the CoordinatorLayout performing the nested scroll
        dx - the raw horizontal number of pixels that the user attempted to scroll
        dy - the raw vertical number of pixels that the user attempted to scroll
        consumed - out parameter.
        type - the type of input which cause this scroll event
      • onNestedFling

         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.

        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.

        onNestedFling is called when the current nested scrolling child viewdetects the proper conditions for a fling. It reports if the child itself consumed the fling.If it did not, the child is expected to show some sort of overscroll indication. This methodshould return true if it consumes the fling, so that a child that did not itself take anaction in response can choose not to show an overfling indication.

        Parameters:
        coordinatorLayout - the CoordinatorLayout parent of the view this Behavior is associatedwith
        child - the child view of the CoordinatorLayout this Behavior is associated with
        target - the descendant view of the CoordinatorLayout performing the nested scroll
        velocityX - horizontal velocity of the attempted fling
        velocityY - vertical velocity of the attempted fling
        consumed - true if the nested child view consumed the fling
      • 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 associatedwith
        child - the child view of the CoordinatorLayout this Behavior is associated with
        target - the descendant view of the CoordinatorLayout performing the nested scroll
        velocityX - horizontal velocity of the attempted fling
        velocityY - vertical velocity of the attempted fling
      • onApplyWindowInsets

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

        Called when the window insets have changed.

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

        Parameters:
        coordinatorLayout - the CoordinatorLayout parent of the view this Behavior is associatedwith
        child - the child view of the CoordinatorLayout this Behavior is associated with
        insets - the new window insets.
      • onRequestChildRectangleOnScreen

         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 tobe positioned onto the screen.

        The contract for this method is the same as requestChildRectangleOnScreen.

        Parameters:
        coordinatorLayout - the CoordinatorLayout parent of the view this Behavior is associatedwith
        child - the child view of the CoordinatorLayout this Behavior is associated with
        rectangle - The rectangle which the child wishes to be on the screen in the child'scoordinates
        immediate - true to forbid animated or delayed scrolling, false otherwise
      • 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.

        Parameters:
        parent - the parent CoordinatorLayout
        child - child view to restore from
        state - The frozen state that had previously been returned by .
      • onSaveInstanceState

        @Nullable() 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 CoordinatorLayout
        child - child view to restore from
      • getInsetDodgeRect

         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. Ifnot, a IllegalArgumentException is thrown.

        Parameters:
        parent - the CoordinatorLayout parent of the view this Behavior is associated with
        child - the child view of the CoordinatorLayout this Behavior is associated with
        rect - the rect to update with the dodge rectangle