Skip to content

Most visited

Recently visited

navigation

AppBarLayout.Behavior

public static class AppBarLayout.Behavior
extends Behavior<V extends View>

java.lang.Object
   ↳ android.support.design.widget.CoordinatorLayout.Behavior<V extends android.view.View>
     ↳ android.support.design.widget.AppBarLayout.Behavior


The default AppBarLayout.Behavior for AppBarLayout. Implements the necessary nested scroll handling with offsetting.

Summary

Nested classes

class AppBarLayout.Behavior.DragCallback

Callback to allow control over any AppBarLayout dragging. 

class AppBarLayout.Behavior.SavedState

 

Public constructors

AppBarLayout.Behavior()
AppBarLayout.Behavior(Context context, AttributeSet attrs)

Public methods

int getLeftAndRightOffset()
int getTopAndBottomOffset()
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 onLayoutChild(CoordinatorLayout parent, AppBarLayout abl, int layoutDirection)

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

boolean onMeasureChild(CoordinatorLayout parent, AppBarLayout 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, AppBarLayout 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.

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

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

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

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

void onRestoreInstanceState(CoordinatorLayout parent, AppBarLayout appBarLayout, 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, AppBarLayout abl)

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 parent, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes)

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

void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout abl, View target)

Called when a nested scroll has ended.

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

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

void setDragCallback(AppBarLayout.Behavior.DragCallback callback)

Set a callback to control any AppBarLayout dragging.

boolean setLeftAndRightOffset(int offset)
boolean setTopAndBottomOffset(int offset)

Protected methods

void layoutChild(CoordinatorLayout parent, V child, int layoutDirection)

Inherited methods

From class android.support.design.widget.CoordinatorLayout.Behavior
From class java.lang.Object

Public constructors

AppBarLayout.Behavior

AppBarLayout.Behavior ()

AppBarLayout.Behavior

AppBarLayout.Behavior (Context context, 
                AttributeSet attrs)

Parameters
context Context
attrs AttributeSet

Public methods

getLeftAndRightOffset

int getLeftAndRightOffset ()

Returns
int

getTopAndBottomOffset

int getTopAndBottomOffset ()

Returns
int

onInterceptTouchEvent

boolean onInterceptTouchEvent (CoordinatorLayout parent, 
                V child, 
                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 to intercept the rest of the event stream to take an action on its associated child view. This method will return false until it detects the proper intercept conditions, then return true once those conditions have occurred.

Once a Behavior intercepts touch events, the rest of the event stream will be sent to the onTouchEvent(CoordinatorLayout, V, MotionEvent) method.

This method will be called regardless of the visibility of the associated child of the behavior. If you only wish to handle touch events when the child is visible, you should add a check to isShown() on the given child.

The default implementation of this method always returns false.

Parameters
parent CoordinatorLayout: the parent view currently receiving this touch event
child V: the child view associated with this Behavior
ev MotionEvent: the MotionEvent describing the touch event being processed
Returns
boolean true if this Behavior would like to intercept and take over the event stream. The default always returns false.

onLayoutChild

boolean onLayoutChild (CoordinatorLayout parent, 
                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 view in place of the default child layout behavior. The Behavior's implementation can delegate to the standard CoordinatorLayout measurement behavior by calling parent.onLayoutChild.

If a Behavior implements onDependentViewChanged(CoordinatorLayout, android.view.View, android.view.View) to change the position of a view 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 view after its dependency has been laid out.

Parameters
parent CoordinatorLayout: the parent CoordinatorLayout
child V: child view to lay out
layoutDirection int: the resolved layout direction for the CoordinatorLayout, such as LAYOUT_DIRECTION_LTR or LAYOUT_DIRECTION_RTL.
Returns
boolean true if the Behavior performed layout of the child view, false to request default layout behavior

onLayoutChild

boolean onLayoutChild (CoordinatorLayout parent, 
                AppBarLayout abl, 
                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 view in place of the default child layout behavior. The Behavior's implementation can delegate to the standard CoordinatorLayout measurement behavior by calling parent.onLayoutChild.

If a Behavior implements onDependentViewChanged(CoordinatorLayout, android.view.View, android.view.View) to change the position of a view 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 view after its dependency has been laid out.

Parameters
parent CoordinatorLayout: the parent CoordinatorLayout
abl AppBarLayout: child view to lay out
layoutDirection int: the resolved layout direction for the CoordinatorLayout, such as LAYOUT_DIRECTION_LTR or LAYOUT_DIRECTION_RTL.
Returns
boolean true if the Behavior performed layout of the child view, false to request default layout behavior

onMeasureChild

boolean onMeasureChild (CoordinatorLayout parent, 
                AppBarLayout 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 view in place of the default child measurement behavior. The Behavior's implementation can delegate to the standard CoordinatorLayout measurement behavior by calling parent.onMeasureChild.

Parameters
parent CoordinatorLayout: the parent CoordinatorLayout
child AppBarLayout: the child to measure
parentWidthMeasureSpec int: the width requirements for this view
widthUsed int: extra space that has been used up by the parent horizontally (possibly by other children of the parent)
parentHeightMeasureSpec int: the height requirements for this view
heightUsed int: extra space that has been used up by the parent vertically (possibly by other children of the parent)
Returns
boolean true if the Behavior measured the child view, false if the CoordinatorLayout should perform its default measurement

onNestedFling

boolean onNestedFling (CoordinatorLayout coordinatorLayout, 
                AppBarLayout 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.

Any Behavior associated with the direct child of the CoordinatorLayout may elect to accept the nested scroll as part of onStartNestedScroll(CoordinatorLayout, V, View, View, int). Each Behavior that returned true will receive subsequent nested scroll events for that nested scroll.

onNestedFling is called when the current nested scrolling child view detects 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 method should return true if it consumes the fling, so that a child that did not itself take an action in response can choose not to show an overfling indication.

Parameters
coordinatorLayout CoordinatorLayout: the CoordinatorLayout parent of the view this Behavior is associated with
child AppBarLayout: the child view of the CoordinatorLayout this Behavior is associated with
target View: the descendant view of the CoordinatorLayout performing the nested scroll
velocityX float: horizontal velocity of the attempted fling
velocityY float: vertical velocity of the attempted fling
consumed boolean: true if the nested child view consumed the fling
Returns
boolean true if the Behavior consumed the fling

onNestedPreScroll

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

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

Any Behavior associated with the direct child of the CoordinatorLayout may elect to accept the nested scroll as part of onStartNestedScroll(CoordinatorLayout, V, View, View, int). Each Behavior that returned true will receive subsequent nested scroll events for that nested scroll.

onNestedPreScroll is called each time the nested scroll is updated by the nested scrolling child, before the nested scrolling child has consumed the scroll distance itself. Each Behavior responding to the nested scroll will receive the same values. The CoordinatorLayout will report as consumed the maximum number of pixels in either direction that any Behavior responding to the nested scroll reported as consumed.

Parameters
coordinatorLayout CoordinatorLayout: the CoordinatorLayout parent of the view this Behavior is associated with
child AppBarLayout: the child view of the CoordinatorLayout this Behavior is associated with
target View: the descendant view of the CoordinatorLayout performing the nested scroll
dx int: the raw horizontal number of pixels that the user attempted to scroll
dy int: the raw vertical number of pixels that the user attempted to scroll
consumed int: out parameter. consumed[0] should be set to the distance of dx that was consumed, consumed[1] should be set to the distance of dy that was consumed

onNestedScroll

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

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

Any Behavior associated with the direct child of the CoordinatorLayout may elect to accept the nested scroll as part of onStartNestedScroll(CoordinatorLayout, V, View, View, int). Each Behavior that returned true will receive subsequent nested scroll events for that nested scroll.

onNestedScroll is called each time the nested scroll is updated by the nested scrolling child, with both consumed and unconsumed components of the scroll supplied in pixels. Each Behavior responding to the nested scroll will receive the same values.

Parameters
coordinatorLayout CoordinatorLayout: the CoordinatorLayout parent of the view this Behavior is associated with
child AppBarLayout: the child view of the CoordinatorLayout this Behavior is associated with
target View: the descendant view of the CoordinatorLayout performing the nested scroll
dxConsumed int: horizontal pixels consumed by the target's own scrolling operation
dyConsumed int: vertical pixels consumed by the target's own scrolling operation
dxUnconsumed int: horizontal pixels not consumed by the target's own scrolling operation, but requested by the user
dyUnconsumed int: vertical pixels not consumed by the target's own scrolling operation, but requested by the user

onRestoreInstanceState

void onRestoreInstanceState (CoordinatorLayout parent, 
                AppBarLayout appBarLayout, 
                Parcelable state)

Hook allowing a behavior to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState(CoordinatorLayout, V). This function will never be called with a null state.

Parameters
parent CoordinatorLayout: the parent CoordinatorLayout
appBarLayout AppBarLayout: child view to restore from
state Parcelable: The frozen state that had previously been returned by onSaveInstanceState(CoordinatorLayout, V).

onSaveInstanceState

Parcelable onSaveInstanceState (CoordinatorLayout parent, 
                AppBarLayout abl)

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. This state should only contain information that is not persistent or can not be reconstructed later.

Behavior state is only saved when both the parent CoordinatorLayout and a view using this behavior have valid IDs set.

Parameters
parent CoordinatorLayout: the parent CoordinatorLayout
abl AppBarLayout: child view to restore from
Returns
Parcelable Returns a Parcelable object containing the behavior's current dynamic state.

onStartNestedScroll

boolean onStartNestedScroll (CoordinatorLayout parent, 
                AppBarLayout child, 
                View directTargetChild, 
                View target, 
                int nestedScrollAxes)

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

Any Behavior associated with any direct child of the CoordinatorLayout may respond to this event and return true to indicate that the CoordinatorLayout should act as a nested scrolling parent for this scroll. Only Behaviors that return true from this method will receive subsequent nested scroll events.

Parameters
parent CoordinatorLayout: the CoordinatorLayout parent of the view this Behavior is associated with
child AppBarLayout: the child view of the CoordinatorLayout this Behavior is associated with
directTargetChild View: the child view of the CoordinatorLayout that either is or contains the target of the nested scroll operation
target View: the descendant view of the CoordinatorLayout initiating the nested scroll
nestedScrollAxes int: the axes that this nested scroll applies to. See SCROLL_AXIS_HORIZONTAL, SCROLL_AXIS_VERTICAL
Returns
boolean true if the Behavior wishes to accept this nested scroll

onStopNestedScroll

void onStopNestedScroll (CoordinatorLayout coordinatorLayout, 
                AppBarLayout abl, 
                View target)

Called when a nested scroll has ended.

Any Behavior associated with any direct child of the CoordinatorLayout may elect to accept the nested scroll as part of onStartNestedScroll(CoordinatorLayout, V, View, View, int). Each Behavior that returned true will receive subsequent nested scroll events for that nested scroll.

onStopNestedScroll marks the end of a single nested scroll event sequence. This is a good place to clean up any state related to the nested scroll.

Parameters
coordinatorLayout CoordinatorLayout: the CoordinatorLayout parent of the view this Behavior is associated with
abl AppBarLayout: the child view of the CoordinatorLayout this Behavior is associated with
target View: the descendant view of the CoordinatorLayout that initiated the nested scroll

onTouchEvent

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

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

Behaviors may intercept touch events in order to help the CoordinatorLayout manipulate its child views. For example, a Behavior may allow a user to drag a UI pane open or closed. This method should perform actual mutations of view layout state.

This method will be called regardless of the visibility of the associated child of the behavior. If you only wish to handle touch events when the child is visible, you should add a check to isShown() on the given child.

Parameters
parent CoordinatorLayout: the parent view currently receiving this touch event
child V: the child view associated with this Behavior
ev MotionEvent: the MotionEvent describing the touch event being processed
Returns
boolean true if this Behavior handled this touch event and would like to continue receiving events in this stream. The default always returns false.

setDragCallback

void setDragCallback (AppBarLayout.Behavior.DragCallback callback)

Set a callback to control any AppBarLayout dragging.

Parameters
callback AppBarLayout.Behavior.DragCallback: the callback to use, or null to use the default behavior.

setLeftAndRightOffset

boolean setLeftAndRightOffset (int offset)

Parameters
offset int
Returns
boolean

setTopAndBottomOffset

boolean setTopAndBottomOffset (int offset)

Parameters
offset int
Returns
boolean

Protected methods

layoutChild

void layoutChild (CoordinatorLayout parent, 
                V child, 
                int layoutDirection)

Parameters
parent CoordinatorLayout
child V
layoutDirection int
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.