NestedScrollView
public class NestedScrollView extends FrameLayout implements NestedScrollingParent3, NestedScrollingChild3, ScrollingView
| java.lang.Object | ||||
| ↳ | android.view.View | |||
| ↳ | android.view.ViewGroup | |||
| ↳ | android.widget.FrameLayout | |||
| ↳ | androidx.core.widget.NestedScrollView |
NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.
Summary
Nested types |
|---|
public interface NestedScrollView.OnScrollChangeListenerInterface definition for a callback to be invoked when the scroll X or Y positions of a view change. |
Public constructors |
|---|
NestedScrollView(@NonNull Context context) |
NestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs) |
NestedScrollView( |
Public methods |
|
|---|---|
void |
|
void |
|
void |
addView(View child, ViewGroup.LayoutParams params) |
void |
addView(View child, int index, ViewGroup.LayoutParams params) |
boolean |
arrowScroll(int direction)Handle scrolling in response to an up or down arrow click. |
void |
|
boolean |
dispatchKeyEvent(KeyEvent event) |
boolean |
dispatchNestedFling(float velocityX, float velocityY, boolean consumed) |
boolean |
dispatchNestedPreFling(float velocityX, float velocityY) |
boolean |
dispatchNestedPreScroll( |
boolean |
dispatchNestedPreScroll(Dispatch one step of a nested scroll in progress before this view consumes any portion of it. |
boolean |
dispatchNestedScroll( |
boolean |
dispatchNestedScroll(Dispatch one step of a nested scroll in progress. |
void |
dispatchNestedScroll(Dispatch one step of a nested scroll in progress. |
void |
|
boolean |
executeKeyEvent(@NonNull KeyEvent event)You can call this function yourself to have the scroll view perform scrolling from a key event, just as if the event had been dispatched to it by the view hierarchy. |
void |
fling(int velocityY)Fling the scroll view |
boolean |
fullScroll(int direction)Handles scrolling in response to a "home/end" shortcut press. |
int |
|
int |
|
boolean |
|
boolean |
hasNestedScrollingParent(int type)Returns true if this view has a nested scrolling parent for the given input type. |
boolean |
Indicates whether this ScrollView's content is stretched to fill the viewport. |
boolean |
|
boolean |
|
void |
|
boolean |
onGenericMotionEvent(@NonNull MotionEvent motionEvent) |
boolean |
|
boolean |
onNestedFling( |
boolean |
onNestedPreFling(@NonNull View target, float velocityX, float velocityY) |
void |
onNestedPreScroll( |
void |
onNestedPreScroll(React to a nested scroll in progress before the target view consumes a portion of the scroll. |
void |
onNestedScroll( |
void |
onNestedScroll(React to a nested scroll in progress. |
void |
onNestedScroll(React to a nested scroll in progress. |
void |
onNestedScrollAccepted(@NonNull View child, @NonNull View target, int axes) |
void |
onNestedScrollAccepted(React to the successful claiming of a nested scroll operation. |
boolean |
onStartNestedScroll(@NonNull View child, @NonNull View target, int axes) |
boolean |
onStartNestedScroll(React to a descendant view initiating a nestable scroll operation, claiming the nested scroll operation if appropriate. |
void |
onStopNestedScroll(@NonNull View target) |
void |
onStopNestedScroll(@NonNull View target, int type)React to a nested scroll operation ending. |
boolean |
onTouchEvent(@NonNull MotionEvent motionEvent) |
boolean |
pageScroll(int direction)Handles scrolling in response to a "page up/down" shortcut press. |
void |
requestChildFocus(View child, View focused) |
boolean |
requestChildRectangleOnScreen( |
void |
requestDisallowInterceptTouchEvent(boolean disallowIntercept) |
void |
|
void |
scrollTo(int x, int y)This version also clamps the scrolling to the bounds of our child. |
void |
setFillViewport(boolean fillViewport)Set whether this ScrollView should stretch its content height to fill the viewport or not. |
void |
setNestedScrollingEnabled(boolean enabled) |
void |
Register a callback to be invoked when the scroll X or Y positions of this view change. |
void |
setSmoothScrollingEnabled(boolean smoothScrollingEnabled)Set whether arrow scrolling will animate its transition. |
boolean |
|
final void |
smoothScrollBy(int dx, int dy)Like |
final void |
smoothScrollBy(int dx, int dy, int scrollDurationMs)Like |
final void |
smoothScrollTo(int x, int y)Like |
final void |
smoothScrollTo(int x, int y, int scrollDurationMs)Like |
boolean |
startNestedScroll(int axes) |
boolean |
startNestedScroll(int axes, int type)Begin a nestable scroll operation along the given axes, for the given input type. |
void |
|
void |
stopNestedScroll(int type)Stop a nested scroll in progress for the given input type. |
Protected methods |
|
|---|---|
int |
Compute the amount to scroll in the Y direction in order to get a rectangle completely on the screen (or, if taller than the screen, at least the first screen size chunk of it). |
float |
|
float |
|
void |
measureChild( |
void |
measureChildWithMargins( |
void |
onLayout(boolean changed, int l, int t, int r, int b) |
void |
onMeasure(int widthMeasureSpec, int heightMeasureSpec) |
void |
onOverScrolled( |
boolean |
onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)When looking for focus in children of a scroll view, need to be a little more careful not to give focus to something that is scrolled off screen. |
void |
onRestoreInstanceState(Parcelable state) |
@NonNull Parcelable |
|
void |
onScrollChanged(int l, int t, int oldl, int oldt) |
void |
onSizeChanged(int w, int h, int oldw, int oldh) |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Inherited methods |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Public constructors
NestedScrollView
public NestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs)
NestedScrollView
public NestedScrollView(
@NonNull Context context,
@Nullable AttributeSet attrs,
int defStyleAttr
)
Public methods
arrowScroll
public boolean arrowScroll(int direction)
Handle scrolling in response to an up or down arrow click.
| Parameters | |
|---|---|
int direction |
The direction corresponding to the arrow key that was pressed |
| Returns | |
|---|---|
boolean |
True if we consumed the event, false otherwise |
dispatchNestedFling
public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed)
dispatchNestedPreScroll
public boolean dispatchNestedPreScroll(
int dx,
int dy,
@Nullable int[] consumed,
@Nullable int[] offsetInWindow
)
dispatchNestedPreScroll
public boolean dispatchNestedPreScroll(
int dx,
int dy,
@Nullable int[] consumed,
@Nullable int[] offsetInWindow,
int type
)
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 | |
|---|---|
int dx |
Horizontal scroll distance in pixels |
int dy |
Vertical scroll distance in pixels |
@Nullable int[] consumed |
Output. If not null, consumed[0] will contain the consumed component of dx and consumed[1] the consumed dy. |
@Nullable int[] offsetInWindow |
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. |
int type |
the type of input which cause this scroll event |
| Returns | |
|---|---|
boolean |
true if the parent consumed some or all of the scroll delta |
| See also | |
|---|---|
dispatchNestedScroll |
dispatchNestedScroll
public boolean dispatchNestedScroll(
int dxConsumed,
int dyConsumed,
int dxUnconsumed,
int dyUnconsumed,
@Nullable int[] offsetInWindow
)
dispatchNestedScroll
public boolean dispatchNestedScroll(
int dxConsumed,
int dyConsumed,
int dxUnconsumed,
int dyUnconsumed,
@Nullable int[] offsetInWindow,
int type
)
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 | |
|---|---|
int dxConsumed |
Horizontal distance in pixels consumed by this view during this scroll step |
int dyConsumed |
Vertical distance in pixels consumed by this view during this scroll step |
int dxUnconsumed |
Horizontal scroll distance in pixels not consumed by this view |
int dyUnconsumed |
Horizontal scroll distance in pixels not consumed by this view |
@Nullable int[] offsetInWindow |
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. |
int type |
the type of input which cause this scroll event |
| Returns | |
|---|---|
boolean |
true if the event was dispatched, false if it could not be dispatched. |
| See also | |
|---|---|
dispatchNestedPreScroll |
dispatchNestedScroll
public void dispatchNestedScroll(
int dxConsumed,
int dyConsumed,
int dxUnconsumed,
int dyUnconsumed,
@Nullable int[] offsetInWindow,
int type,
@NonNull int[] consumed
)
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.
The original nested scrolling child (where the input events were received to start the scroll) must provide a non-null consumed parameter with values {0, 0}.
| Parameters | |
|---|---|
int dxConsumed |
Horizontal distance in pixels consumed by this view during this scroll step |
int dyConsumed |
Vertical distance in pixels consumed by this view during this scroll step |
int dxUnconsumed |
Horizontal scroll distance in pixels not consumed by this view |
int dyUnconsumed |
Horizontal scroll distance in pixels not consumed by this view |
@Nullable int[] offsetInWindow |
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. |
int type |
the type of input which cause this scroll event |
@NonNull int[] consumed |
Output. Upon this method returning, will contain the original values plus any scroll distances consumed by all of this view's nested scrolling parents up the view hierarchy. Index 0 for the x dimension, and index 1 for the y dimension |
| See also | |
|---|---|
onNestedScroll |
executeKeyEvent
public boolean executeKeyEvent(@NonNull KeyEvent event)
You can call this function yourself to have the scroll view perform scrolling from a key event, just as if the event had been dispatched to it by the view hierarchy.
| Returns | |
|---|---|
boolean |
Return true if the event was handled, else false. |
fling
public void fling(int velocityY)
Fling the scroll view
| Parameters | |
|---|---|
int velocityY |
The initial velocity in the Y direction. Positive numbers mean that the finger/cursor is moving down the screen, which means we want to scroll towards the top. |
fullScroll
public boolean fullScroll(int direction)
Handles scrolling in response to a "home/end" shortcut press. This method will scroll the view to the top or bottom and give the focus to the topmost/bottommost component in the new visible area. If no component is a good candidate for focus, this scrollview reclaims the focus.
| Parameters | |
|---|---|
int direction |
the scroll direction: |
| Returns | |
|---|---|
boolean |
true if the key event is consumed by this method, false otherwise |
getMaxScrollAmount
public int getMaxScrollAmount()
| Returns | |
|---|---|
int |
The maximum amount this scroll view will scroll in response to an arrow event. |
hasNestedScrollingParent
public boolean hasNestedScrollingParent(int type)
Returns true if this view has a nested scrolling parent for the given input type.
The presence of a nested scrolling parent indicates that this view has initiated a nested scroll and it was accepted by an ancestor view further up the view hierarchy.
| Parameters | |
|---|---|
int type |
the type of input which cause this scroll event |
| Returns | |
|---|---|
boolean |
whether this view has a nested scrolling parent |
isFillViewport
public boolean isFillViewport()
Indicates whether this ScrollView's content is stretched to fill the viewport.
name android:fillViewport
| Returns | |
|---|---|
boolean |
True if the content fills the viewport, false otherwise. |
isSmoothScrollingEnabled
public boolean isSmoothScrollingEnabled()
| Returns | |
|---|---|
boolean |
Whether arrow scrolling will animate its transition. |
onNestedFling
public boolean onNestedFling(
@NonNull View target,
float velocityX,
float velocityY,
boolean consumed
)
onNestedPreFling
public boolean onNestedPreFling(@NonNull View target, float velocityX, float velocityY)
onNestedPreScroll
public void onNestedPreScroll(
@NonNull View target,
int dx,
int dy,
@NonNull int[] consumed
)
onNestedPreScroll
public void onNestedPreScroll(
@NonNull View target,
int dx,
int dy,
@NonNull int[] consumed,
int type
)
React to a nested scroll in progress before the target view consumes a portion of the scroll.
When working with nested scrolling often the parent view may want an opportunity to consume the scroll before the nested scrolling child does. An example of this is a drawer that contains a scrollable list. The user will want to be able to scroll the list fully into view before the list itself begins scrolling.
onNestedPreScroll is called when a nested scrolling child invokes dispatchNestedPreScroll. The implementation should report how any pixels of the scroll reported by dx, dy were consumed in the consumed array. Index 0 corresponds to dx and index 1 corresponds to dy. This parameter will never be null. Initial values for consumed[0] and consumed[1] will always be 0.
| Parameters | |
|---|---|
@NonNull View target |
View that initiated the nested scroll |
int dx |
Horizontal scroll distance in pixels |
int dy |
Vertical scroll distance in pixels |
@NonNull int[] consumed |
Output. The horizontal and vertical scroll distance consumed by this parent |
int type |
the type of input which cause this scroll event |
onNestedScroll
public void onNestedScroll(
@NonNull View target,
int dxConsumed,
int dyConsumed,
int dxUnconsumed,
int dyUnconsumed
)
onNestedScroll
public void onNestedScroll(
@NonNull View target,
int dxConsumed,
int dyConsumed,
int dxUnconsumed,
int dyUnconsumed,
int type
)
React to a nested scroll in progress.
This method will be called when the ViewParent's current nested scrolling child view dispatches a nested scroll event. To receive calls to this method the ViewParent must have previously returned true for a call to onStartNestedScroll.
Both the consumed and unconsumed portions of the scroll distance are reported to the ViewParent. An implementation may choose to use the consumed portion to match or chase scroll position of multiple child elements, for example. The unconsumed portion may be used to allow continuous dragging of multiple scrolling or draggable elements, such as scrolling a list within a vertical drawer where the drawer begins dragging once the edge of inner scrolling content is reached.
| Parameters | |
|---|---|
@NonNull View target |
The descendent view controlling the nested scroll |
int dxConsumed |
Horizontal scroll distance in pixels already consumed by target |
int dyConsumed |
Vertical scroll distance in pixels already consumed by target |
int dxUnconsumed |
Horizontal scroll distance in pixels not consumed by target |
int dyUnconsumed |
Vertical scroll distance in pixels not consumed by target |
int type |
the type of input which cause this scroll event |
onNestedScroll
public void onNestedScroll(
@NonNull View target,
int dxConsumed,
int dyConsumed,
int dxUnconsumed,
int dyUnconsumed,
int type,
@NonNull int[] consumed
)
React to a nested scroll in progress.
This method will be called when the ViewParent's current nested scrolling child view dispatches a nested scroll event. To receive calls to this method the ViewParent must have previously returned true for a call to onStartNestedScroll.
Both the consumed and unconsumed portions of the scroll distance are reported to the ViewParent. An implementation may choose to use the consumed portion to match or chase scroll position of multiple child elements, for example. The unconsumed portion may be used to allow continuous dragging of multiple scrolling or draggable elements, such as scrolling a list within a vertical drawer where the drawer begins dragging once the edge of inner scrolling content is reached.
This method is called when a nested scrolling child invokes dispatchNestedScroll} or one of methods it overloads.
An implementation must report how many pixels of the the x and y scroll distances were consumed by this nested scrolling parent by adding the consumed distances to the consumed parameter. If this View also implements NestedScrollingChild3, consumed should also be passed up to it's nested scrolling parent so that the parent may also add any scroll distance it consumes. Index 0 corresponds to dx and index 1 corresponds to dy.
| Parameters | |
|---|---|
@NonNull View target |
The descendant view controlling the nested scroll |
int dxConsumed |
Horizontal scroll distance in pixels already consumed by target |
int dyConsumed |
Vertical scroll distance in pixels already consumed by target |
int dxUnconsumed |
Horizontal scroll distance in pixels not consumed by target |
int dyUnconsumed |
Vertical scroll distance in pixels not consumed by target |
int type |
the type of input which cause this scroll event |
@NonNull int[] consumed |
Output. Upon this method returning, will contain the scroll distances consumed by this nested scrolling parent and the scroll distances consumed by any other parent up the view hierarchy |
| See also | |
|---|---|
dispatchNestedScroll |
onNestedScrollAccepted
public void onNestedScrollAccepted(@NonNull View child, @NonNull View target, int axes)
onNestedScrollAccepted
public void onNestedScrollAccepted(
@NonNull View child,
@NonNull View target,
int axes,
int type
)
React to the successful claiming of a nested scroll operation.
This method will be called after onStartNestedScroll returns true. It offers an opportunity for the view and its superclasses to perform initial configuration for the nested scroll. Implementations of this method should always call their superclass's implementation of this method if one is present.
| Parameters | |
|---|---|
@NonNull View child |
Direct child of this ViewParent containing target |
@NonNull View target |
View that initiated the nested scroll |
int axes |
Flags consisting of |
int type |
the type of input which cause this scroll event |
| See also | |
|---|---|
onStartNestedScroll |
|
onStopNestedScroll |
onStartNestedScroll
public boolean onStartNestedScroll(@NonNull View child, @NonNull View target, int axes)
onStartNestedScroll
public boolean onStartNestedScroll(
@NonNull View child,
@NonNull View target,
int axes,
int type
)
React to a descendant view initiating a nestable scroll operation, claiming the nested scroll operation if appropriate.
This method will be called in response to a descendant view invoking startNestedScroll. Each parent up the view hierarchy will be given an opportunity to respond and claim the nested scrolling operation by returning true.
This method may be overridden by ViewParent implementations to indicate when the view is willing to support a nested scrolling operation that is about to begin. If it returns true, this ViewParent will become the target view's nested scrolling parent for the duration of the scroll operation in progress. When the nested scroll is finished this ViewParent will receive a call to onStopNestedScroll.
| Parameters | |
|---|---|
@NonNull View child |
Direct child of this ViewParent containing target |
@NonNull View target |
View that initiated the nested scroll |
int axes |
Flags consisting of |
int type |
the type of input which cause this scroll event |
| Returns | |
|---|---|
boolean |
true if this ViewParent accepts the nested scroll operation |
onStopNestedScroll
public void onStopNestedScroll(@NonNull View target, int type)
React to a nested scroll operation ending.
Perform cleanup after a nested scrolling operation. This method will be called when a nested scroll stops, for example when a nested touch scroll ends with a ACTION_UP or ACTION_CANCEL event. Implementations of this method should always call their superclass's implementation of this method if one is present.
pageScroll
public boolean pageScroll(int direction)
Handles scrolling in response to a "page up/down" shortcut press. This method will scroll the view by one page up or down and give the focus to the topmost/bottommost component in the new visible area. If no component is a good candidate for focus, this scrollview reclaims the focus.
| Parameters | |
|---|---|
int direction |
the scroll direction: |
| Returns | |
|---|---|
boolean |
true if the key event is consumed by this method, false otherwise |
requestChildRectangleOnScreen
public boolean requestChildRectangleOnScreen(
@NonNull View child,
Rect rectangle,
boolean immediate
)
requestDisallowInterceptTouchEvent
public void requestDisallowInterceptTouchEvent(boolean disallowIntercept)
scrollTo
public void scrollTo(int x, int y)
This version also clamps the scrolling to the bounds of our child.
setFillViewport
public void setFillViewport(boolean fillViewport)
Set whether this ScrollView should stretch its content height to fill the viewport or not.
name android:fillViewport
| Parameters | |
|---|---|
boolean fillViewport |
True to stretch the content's height to the viewport's boundaries, false otherwise. |
setOnScrollChangeListener
public void setOnScrollChangeListener(
@Nullable NestedScrollView.OnScrollChangeListener l
)
Register a callback to be invoked when the scroll X or Y positions of this view change.
This version of the method works on all versions of Android, back to API v4.
| Parameters | |
|---|---|
@Nullable NestedScrollView.OnScrollChangeListener l |
The listener to notify when the scroll X or Y position changes. |
| See also | |
|---|---|
getScrollX |
|
getScrollY |
setSmoothScrollingEnabled
public void setSmoothScrollingEnabled(boolean smoothScrollingEnabled)
Set whether arrow scrolling will animate its transition.
| Parameters | |
|---|---|
boolean smoothScrollingEnabled |
whether arrow scrolling will animate its transition |
smoothScrollBy
public final void smoothScrollBy(int dx, int dy)
Like scrollBy, but scroll smoothly instead of immediately.
| Parameters | |
|---|---|
int dx |
the number of pixels to scroll by on the X axis |
int dy |
the number of pixels to scroll by on the Y axis |
smoothScrollBy
public final void smoothScrollBy(int dx, int dy, int scrollDurationMs)
Like scrollBy, but scroll smoothly instead of immediately.
| Parameters | |
|---|---|
int dx |
the number of pixels to scroll by on the X axis |
int dy |
the number of pixels to scroll by on the Y axis |
int scrollDurationMs |
the duration of the smooth scroll operation in milliseconds |
smoothScrollTo
public final void smoothScrollTo(int x, int y)
Like scrollTo, but scroll smoothly instead of immediately.
| Parameters | |
|---|---|
int x |
the position where to scroll on the X axis |
int y |
the position where to scroll on the Y axis |
smoothScrollTo
public final void smoothScrollTo(int x, int y, int scrollDurationMs)
Like scrollTo, but scroll smoothly instead of immediately.
| Parameters | |
|---|---|
int x |
the position where to scroll on the X axis |
int y |
the position where to scroll on the Y axis |
int scrollDurationMs |
the duration of the smooth scroll operation in milliseconds |
startNestedScroll
public boolean startNestedScroll(int axes, int type)
Begin a nestable scroll operation along the given axes, for the given input type.
A view starting a nested scroll promises to abide by the following contract:
The view will call startNestedScroll upon initiating a scroll operation. In the case of a touch scroll type this corresponds to the initial ACTION_DOWN. In the case of touch scrolling the nested scroll will be terminated automatically in the same manner as requestDisallowInterceptTouchEvent. In the event of programmatic scrolling the caller must explicitly call stopNestedScroll to indicate the end of the nested scroll.
If startNestedScroll returns true, a cooperative parent was found. If it returns false the caller may ignore the rest of this contract until the next scroll. Calling startNestedScroll while a nested scroll is already in progress will return true.
At each incremental step of the scroll the caller should invoke dispatchNestedPreScroll once it has calculated the requested scrolling delta. If it returns true the nested scrolling parent at least partially consumed the scroll and the caller should adjust the amount it scrolls by.
After applying the remainder of the scroll delta the caller should invoke dispatchNestedScroll, passing both the delta consumed and the delta unconsumed. A nested scrolling parent may treat these values differently. See onNestedScroll.
| Parameters | |
|---|---|
int axes |
Flags consisting of a combination of |
int type |
the type of input which cause this scroll event |
| Returns | |
|---|---|
boolean |
true if a cooperative parent was found and nested scrolling has been enabled for the current gesture. |
stopNestedScroll
public void stopNestedScroll(int type)
Stop a nested scroll in progress for the given input type.
Calling this method when a nested scroll is not currently in progress is harmless.
| Parameters | |
|---|---|
int type |
the type of input which cause this scroll event |
| See also | |
|---|---|
startNestedScroll |
Protected methods
computeScrollDeltaToGetChildRectOnScreen
protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect)
Compute the amount to scroll in the Y direction in order to get a rectangle completely on the screen (or, if taller than the screen, at least the first screen size chunk of it).
| Parameters | |
|---|---|
Rect rect |
The rect. |
| Returns | |
|---|---|
int |
The scroll delta. |
measureChild
protected void measureChild(
@NonNull View child,
int parentWidthMeasureSpec,
int parentHeightMeasureSpec
)
measureChildWithMargins
protected void measureChildWithMargins(
View child,
int parentWidthMeasureSpec,
int widthUsed,
int parentHeightMeasureSpec,
int heightUsed
)
onOverScrolled
protected void onOverScrolled(
int scrollX,
int scrollY,
boolean clampedX,
boolean clampedY
)
onRequestFocusInDescendants
protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)
When looking for focus in children of a scroll view, need to be a little more careful not to give focus to something that is scrolled off screen. This is more expensive than the default ViewGroup implementation, otherwise this behavior might have been made the default.