NestedScrollingChild
interface NestedScrollingChild
androidx.core.view.NestedScrollingChild |
This interface should be implemented by View
subclasses that wish to support dispatching nested scrolling operations to a cooperating parent ViewGroup
.
Classes implementing this interface should create a final instance of a NestedScrollingChildHelper
as a field and delegate any View methods to the NestedScrollingChildHelper
methods of the same signature.
Views invoking nested scrolling functionality should always do so from the relevant ViewCompat
, ViewGroupCompat
or ViewParentCompat
compatibility shim static methods. This ensures interoperability with nested scrolling views on Android 5.0 Lollipop and newer.
Summary
Public methods | |
---|---|
abstract Boolean |
dispatchNestedFling(velocityX: Float, velocityY: Float, consumed: Boolean) Dispatch a fling to a nested scrolling parent. |
abstract Boolean |
dispatchNestedPreFling(velocityX: Float, velocityY: Float) Dispatch a fling to a nested scrolling parent before it is processed by this view. |
abstract Boolean |
dispatchNestedPreScroll(dx: Int, dy: Int, @Nullable consumed: IntArray?, @Nullable offsetInWindow: IntArray?) Dispatch one step of a nested scroll in progress before this view consumes any portion of it. |
abstract Boolean |
dispatchNestedScroll(dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, @Nullable offsetInWindow: IntArray?) Dispatch one step of a nested scroll in progress. |
abstract Boolean |
Returns true if this view has a nested scrolling parent. |
abstract Boolean |
Returns true if nested scrolling is enabled for this view. |
abstract Unit |
setNestedScrollingEnabled(enabled: Boolean) Enable or disable nested scrolling for this view. |
abstract Boolean |
startNestedScroll(axes: Int) Begin a nestable scroll operation along the given axes. |
abstract Unit |
Stop a nested scroll in progress. |
Public methods
dispatchNestedFling
abstract fun dispatchNestedFling(
velocityX: Float,
velocityY: Float,
consumed: Boolean
): Boolean
Dispatch a fling to a nested scrolling parent.
This method should be used to indicate that a nested scrolling child has detected suitable conditions for a fling. Generally this means that a touch scroll has ended with a velocity
in the direction of scrolling that meets or exceeds the minimum fling velocity
along a scrollable axis.
If a nested scrolling child view would normally fling but it is at the edge of its own content, it can use this method to delegate the fling to its nested scrolling parent instead. The parent may optionally consume the fling or observe a child fling.
Parameters | |
---|---|
velocityX |
Float: Horizontal fling velocity in pixels per second |
velocityY |
Float: Vertical fling velocity in pixels per second |
consumed |
Boolean: true if the child consumed the fling, false otherwise |
Return | |
---|---|
Boolean |
true if the nested scrolling parent consumed or otherwise reacted to the fling |
dispatchNestedPreFling
abstract fun dispatchNestedPreFling(
velocityX: Float,
velocityY: Float
): Boolean
Dispatch a fling to a nested scrolling parent before it is processed by this view.
Nested pre-fling events are to nested fling events what touch intercept is to touch and what nested pre-scroll is to nested scroll. dispatchNestedPreFling
offsets an opportunity for the parent view in a nested fling to fully consume the fling before the child view consumes it. If this method returns true
, a nested parent view consumed the fling and this view should not scroll as a result.
For a better user experience, only one view in a nested scrolling chain should consume the fling at a time. If a parent view consumed the fling this method will return false. Custom view implementations should account for this in two ways:
- If a custom view is paged and needs to settle to a fixed page-point, do not call
dispatchNestedPreFling
; consume the fling and settle to a valid position regardless. - If a nested parent does consume the fling, this view should not scroll at all, even to settle back to a valid idle position.
Views should also not offer fling velocities to nested parent views along an axis where scrolling is not currently supported; a ScrollView
should not offer a horizontal fling velocity to its parents since scrolling along that axis is not permitted and carrying velocity along that motion does not make sense.
Parameters | |
---|---|
velocityX |
Float: Horizontal fling velocity in pixels per second |
velocityY |
Float: Vertical fling velocity in pixels per second |
Return | |
---|---|
Boolean |
true if a nested scrolling parent consumed the fling |
dispatchNestedPreScroll
abstract fun dispatchNestedPreScroll(
dx: Int,
dy: Int,
@Nullable consumed: IntArray?,
@Nullable offsetInWindow: IntArray?
): Boolean
Dispatch one step of a nested scroll in progress before this view consumes any portion of it.
Nested pre-scroll events are to nested scroll events what touch intercept is to touch. dispatchNestedPreScroll
offers an opportunity for the parent view in a nested scrolling operation to consume some or all of the scroll operation before the child view consumes it.
Parameters | |
---|---|
dx |
Int: Horizontal scroll distance in pixels |
dy |
Int: Vertical scroll distance in pixels |
consumed |
IntArray?: Output. If not null, consumed[0] will contain the consumed component of dx and consumed[1] the consumed dy. |
offsetInWindow |
IntArray?: Optional. If not null, on return this will contain the offset in local view coordinates of this view from before this operation to after it completes. View implementations may use this to adjust expected input coordinate tracking. |
Return | |
---|---|
Boolean |
true if the parent consumed some or all of the scroll delta |
dispatchNestedScroll
abstract fun dispatchNestedScroll(
dxConsumed: Int,
dyConsumed: Int,
dxUnconsumed: Int,
dyUnconsumed: Int,
@Nullable offsetInWindow: IntArray?
): Boolean
Dispatch one step of a nested scroll in progress.
Implementations of views that support nested scrolling should call this to report info about a scroll in progress to the current nested scrolling parent. If a nested scroll is not currently in progress or nested scrolling is not enabled
for this view this method does nothing.
Compatible View implementations should also call dispatchNestedPreScroll
before consuming a component of the scroll event themselves.
Parameters | |
---|---|
dxConsumed |
Int: Horizontal distance in pixels consumed by this view during this scroll step |
dyConsumed |