Most visited

Recently visited

NestedScrollView

public class NestedScrollView
extends FrameLayout implements NestedScrollingParent, NestedScrollingChild, ScrollingView

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.support.v4.widget.NestedScrollView


NestedScrollView就像ScrollView一样,但它支持在Android的新版本和旧版本中同时作为嵌套滚动父代和子代。 嵌套滚动默认启用。

Summary

Nested classes

interface NestedScrollView.OnScrollChangeListener

当视图的滚动X或Y位置改变时调用回调的接口定义。

Inherited XML attributes

From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View

Inherited constants

From class android.view.ViewGroup
From class android.view.View

Inherited fields

From class android.view.View

Public constructors

NestedScrollView(Context context)
NestedScrollView(Context context, AttributeSet attrs)
NestedScrollView(Context context, AttributeSet attrs, int defStyleAttr)

Public methods

void addView(View child, int index)

添加子视图。

void addView(View child, ViewGroup.LayoutParams params)

添加具有指定布局参数的子视图。

void addView(View child, int index, ViewGroup.LayoutParams params)

添加具有指定布局参数的子视图。

void addView(View child)

添加子视图。

boolean arrowScroll(int direction)

响应向上或向下箭头点击处理滚动。

void computeScroll()

由父级调用,以请求孩子在必要时更新mScrollX和mScrollY的值。

boolean dispatchKeyEvent(KeyEvent event)

将关键事件分派到焦点路径上的下一个视图。

boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed)

向嵌套滚动父级派发一个投掷。

boolean dispatchNestedPreFling(float velocityX, float velocityY)

在此视图处理它之前,将嵌套分派给嵌套的滚动父级。

boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow)

在该视图消耗其任何部分之前,调度正在进行的嵌套滚动的一个步骤。

boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow)

发送正在进行的嵌套滚动的一个步骤。

void draw(Canvas canvas)

手动将此视图(及其所有子项)呈现给定的Canvas。

boolean executeKeyEvent(KeyEvent event)

您可以自己调用此函数以使滚动视图执行从关键事件滚动,就好像该事件已由视图层次结构分派给它一样。

void fling(int velocityY)

滚动滚动视图

boolean fullScroll(int direction)

处理滚动以响应“家庭/结束”快捷键。

int getMaxScrollAmount()
int getNestedScrollAxes()

返回此ViewGroup的嵌套滚动的当前轴。

boolean hasNestedScrollingParent()

如果此视图具有嵌套滚动父级,则返回true。

boolean isFillViewport()

指示此ScrollView的内容是否拉伸以填充视口。

boolean isNestedScrollingEnabled()

如果对此视图启用嵌套滚动,则返回true。

boolean isSmoothScrollingEnabled()
void onAttachedToWindow()

这在视图附加到窗口时被调用。

boolean onGenericMotionEvent(MotionEvent event)

实现此方法来处理通用运动事件。

boolean onInterceptTouchEvent(MotionEvent ev)

实施此方法来拦截所有触摸屏幕动作事件。

boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed)

从嵌套滚动中请求投掷。

boolean onNestedPreFling(View target, float velocityX, float velocityY)

在目标视图消耗它之前,将其反应为嵌套的拖动。

void onNestedPreScroll(View target, int dx, int dy, int[] consumed)

在目标视图占用滚动的一部分之前,对正在进行的嵌套滚动进行处理。

void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed)

反应到正在进行的嵌套滚动。

void onNestedScrollAccepted(View child, View target, int nestedScrollAxes)

响应成功声明嵌套滚动操作。

boolean onStartNestedScroll(View child, View target, int nestedScrollAxes)

响应启动可嵌套滚动操作的后代视图,并在适当的情况下声明嵌套滚动操作。

void onStopNestedScroll(View target)

反应到嵌套滚动操作结束。

boolean onTouchEvent(MotionEvent ev)

实现此方法来处理触摸屏幕动作事件。

boolean pageScroll(int direction)

响应“页面向上/向下”快捷按钮,处理滚动。

void requestChildFocus(View child, View focused)

当这个父母的孩子需要关注时调用

boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate)

当该组的小孩想要将特定矩形定位到屏幕上时调用。

void requestDisallowInterceptTouchEvent(boolean disallowIntercept)

当孩子不希望这个父母及其祖先用 onInterceptTouchEvent(MotionEvent)拦截触摸事件时 onInterceptTouchEvent(MotionEvent)

void requestLayout()

当事情发生变化时调用它,这已经使这个视图的布局无效。

void scrollTo(int x, int y)

设置视图的滚动位置。

该版本还将滚动限制在我们孩子的范围内。

void setFillViewport(boolean fillViewport)

指示此滚动视图是否应扩展其内容高度以填充视口。

void setNestedScrollingEnabled(boolean enabled)

启用或禁用此视图的嵌套滚动。

void setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener l)

当此视图的滚动X或Y位置更改时,注册一个要调用的回调。

void setSmoothScrollingEnabled(boolean smoothScrollingEnabled)

设置是否使用箭头滚动来为其转场生成动画。

boolean shouldDelayChildPressedState()

如果应该延迟此ViewGroup的子项或后代的按下状态,则返回true。

final void smoothScrollBy(int dx, int dy)

scrollBy(int, int) ,但滚动而不是立即滚动。

final void smoothScrollTo(int x, int y)

scrollTo(int, int)一样,但可以顺利滚动而不是立即滚动。

boolean startNestedScroll(int axes)

沿给定轴开始可嵌套滚动操作。

void stopNestedScroll()

停止正在进行的嵌套滚动。

Protected methods

int computeScrollDeltaToGetChildRectOnScreen(Rect rect)

计算要在Y方向上滚动的数量,以便在屏幕上完全显示一个矩形(或者,如果比屏幕高,至少是屏幕的第一个屏幕大小)。

float getBottomFadingEdgeStrength()

返回底部渐变边的强度或强度。

float getTopFadingEdgeStrength()

返回顶部褪色边缘的强度或强度。

void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec)

要求这个视图的一个孩子测量自己,同时考虑到这个视图的MeasureSpec要求和填充。

void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed)

要求这个观点的一个孩子衡量自己,同时考虑到这个观点的MeasureSpec要求以及它的填充和边距。

void onLayout(boolean changed, int l, int t, int r, int b)

当这个视图为每个孩子分配一个大小和位置时,从布局调用。

void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

测量视图及其内容以确定测量宽度和测量高度。

void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY)

overScrollBy(int, int, int, int, int, int, int, int, boolean)调用以响应过度滚动操作的结果。

boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)

当在滚动视图的儿童中寻找焦点时,需要小心一些,不要将焦点放在滚动屏幕外的东西上。

void onRestoreInstanceState(Parcelable state)

吊钩允许视图重新应用以前由 onSaveInstanceState()生成的内部状态的表示。

Parcelable onSaveInstanceState()

钩子允许视图生成其内部状态的表示,稍后可用于创建具有相同状态的新实例。

void onScrollChanged(int l, int t, int oldl, int oldt)

这是为了响应此视图中的内部滚动而调用的(即视图滚动了其自己的内容)。

void onSizeChanged(int w, int h, int oldw, int oldh)

当这个视图的大小发生变化时,这在布局期间被调用。

Inherited methods

From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource
From interface android.support.v4.view.NestedScrollingParent
From interface android.support.v4.view.NestedScrollingChild
From interface android.support.v4.view.ScrollingView

Public constructors

NestedScrollView

NestedScrollView (Context context)

Parameters
context Context

NestedScrollView

NestedScrollView (Context context, 
                AttributeSet attrs)

Parameters
context Context
attrs AttributeSet

NestedScrollView

NestedScrollView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context
attrs AttributeSet
defStyleAttr int

Public methods

addView

void addView (View child, 
                int index)

添加子视图。 如果没有设置布局参数,则此子视图的默认参数设置在该子级上。

注意:不要调用此方法 draw(android.graphics.Canvas)onDraw(android.graphics.Canvas)dispatchDraw(android.graphics.Canvas)或任何相关方法。

Parameters
child View: the child view to add
index int: the position at which to add the child

addView

void addView (View child, 
                ViewGroup.LayoutParams params)

添加具有指定布局参数的子视图。

注意:不要调用此方法 draw(android.graphics.Canvas)onDraw(android.graphics.Canvas)dispatchDraw(android.graphics.Canvas)或任何相关方法。

Parameters
child View: the child view to add
params ViewGroup.LayoutParams: the layout parameters to set on the child

addView

void addView (View child, 
                int index, 
                ViewGroup.LayoutParams params)

添加具有指定布局参数的子视图。

注意:不要调用此方法 draw(android.graphics.Canvas)onDraw(android.graphics.Canvas)dispatchDraw(android.graphics.Canvas)或任何相关方法。

Parameters
child View: the child view to add
index int: the position at which to add the child or -1 to add last
params ViewGroup.LayoutParams: the layout parameters to set on the child

addView

void addView (View child)

添加子视图。 如果没有设置布局参数,则此子视图的默认参数设置在该子级上。

注意:不要调用此方法 draw(android.graphics.Canvas)onDraw(android.graphics.Canvas)dispatchDraw(android.graphics.Canvas)或任何相关方法。

Parameters
child View: the child view to add

arrowScroll

boolean arrowScroll (int direction)

响应向上或向下箭头点击处理滚动。

Parameters
direction int: The direction corresponding to the arrow key that was pressed
Returns
boolean True if we consumed the event, false otherwise

computeScroll

void computeScroll ()

由父级调用,以请求孩子在必要时更新mScrollX和mScrollY的值。 如果孩子使用Scroller对象动画滚动,则通常会执行此操作。

dispatchKeyEvent

boolean dispatchKeyEvent (KeyEvent event)

将关键事件分派到焦点路径上的下一个视图。 此路径从视图树的顶部向下延伸到当前聚焦的视图。 如果这个观点有重点,它会发送给自己。 否则,它将沿着焦点路径调度下一个节点。 这个方法也会触发任何关键的监听器。

Parameters
event KeyEvent: The key event to be dispatched.
Returns
boolean True if the event was handled, false otherwise.

dispatchNestedFling

boolean dispatchNestedFling (float velocityX, 
                float velocityY, 
                boolean consumed)

向嵌套滚动父级派发一个投掷。

应该使用此方法来指示嵌套滚动的孩子已经检测到适用于投掷的适当条件。 通常这意味着触摸滚动以velocity的滚动方向结束,沿着可滚动的轴线满足或超过minimum fling velocity

如果嵌套的滚动子视图通常会抛出,但它位于其自己的内容的边缘,则可以使用此方法代替该嵌套的滚动父级。 父母可以有选择地消费一下,或者观察孩子的情绪。

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
Returns
boolean true if the nested scrolling parent consumed or otherwise reacted to the fling

dispatchNestedPreFling

boolean dispatchNestedPreFling (float velocityX, 
                float velocityY)

在此视图处理它之前,将嵌套分派给嵌套的滚动父级。

嵌套的预先掷骰事件是嵌套的掷骰事件,触摸截取是触摸的,嵌套的预滚动是嵌套滚动。 dispatchNestedPreFling在子视图消耗它之前抵消了嵌套式dispatchNestedPreFling中的父视图的机会以完全消耗该dispatchNestedPreFling 如果此方法返回true ,则嵌套父视图会消耗该true ,并且该视图不应该作为结果滚动。

为了获得更好的用户体验,嵌套滚动链中只有一个视图应该一次使用这个文件。 如果父视图消耗了这个方法,这个方法将返回false。 自定义视图实现应该以两种方式解决这个问题:

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

视图也不应该在当前不支持滚动的坐标轴上为嵌套父视图提供快速速度; 一个ScrollView不应该向其父母提供水平的ScrollView速度,因为沿着该轴的滚动是不允许的,并且沿着该运动传送速度没有意义。

Parameters
velocityX float: Horizontal fling velocity in pixels per second
velocityY float: Vertical fling velocity in pixels per second
Returns
boolean true if a nested scrolling parent consumed the fling

dispatchNestedPreScroll

boolean dispatchNestedPreScroll (int dx, 
                int dy, 
                int[] consumed, 
                int[] offsetInWindow)

在该视图消耗其任何部分之前,调度正在进行的嵌套滚动的一个步骤。

嵌套的预滚动事件是嵌套的滚动事件是什么触摸拦截要触摸。 dispatchNestedPreScroll为嵌套滚动操作中的父视图提供了在子视图消耗它之前消耗部分或全部滚动操作的机会。

Parameters
dx int: Horizontal scroll distance in pixels
dy int: Vertical scroll distance in pixels
consumed int: Output. If not null, consumed[0] will contain the consumed component of dx and consumed[1] the consumed dy.
offsetInWindow int: 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.
Returns
boolean true if the parent consumed some or all of the scroll delta

dispatchNestedScroll

boolean dispatchNestedScroll (int dxConsumed, 
                int dyConsumed, 
                int dxUnconsumed, 
                int dyUnconsumed, 
                int[] offsetInWindow)

发送正在进行的嵌套滚动的一个步骤。

支持嵌套滚动的视图实现应该调用它来报告正在进行的滚动信息到当前嵌套的滚动父项。 如果嵌套滚动当前没有进行或嵌套滚动不是enabled这个视图,这个方法什么都不做。

在使用滚动事件本身的组件之前,兼容的View实现应该也调用 dispatchNestedPreScroll

Parameters
dxConsumed int: Horizontal distance in pixels consumed by this view during this scroll step
dyConsumed int: Vertical distance in pixels consumed by this view during this scroll step
dxUnconsumed int: Horizontal scroll distance in pixels not consumed by this view
dyUnconsumed int: Horizontal scroll distance in pixels not consumed by this view
offsetInWindow int: 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.
Returns
boolean true if the event was dispatched, false if it could not be dispatched.

draw

void draw (Canvas canvas)

手动将此视图(及其所有子项)呈现给定的Canvas。 在调用这个函数之前,视图必须已经完成了一个完整的布局。 在实现视图时,实现onDraw(android.graphics.Canvas)而不是重写此方法。 如果您确实需要重写此方法,请调用超类版本。

Parameters
canvas Canvas: The Canvas to which the View is rendered.

executeKeyEvent

boolean executeKeyEvent (KeyEvent event)

您可以自己调用此函数以使滚动视图执行从关键事件滚动,就好像该事件已由视图层次结构分派给它一样。

Parameters
event KeyEvent: The key event to execute.
Returns
boolean Return true if the event was handled, else false.

fling

void fling (int velocityY)

滚动滚动视图

Parameters
velocityY int: 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

boolean fullScroll (int direction)

处理滚动以响应“家庭/结束”快捷键。 此方法将视图滚动到顶部或底部,并将焦点指向新可见区域中的最顶层/最底层组件。 如果没有组件是焦点的合适候选者,则此滚动视图回收焦点。

Parameters
direction int: the scroll direction: FOCUS_UP to go the top of the view or FOCUS_DOWN to go the bottom
Returns
boolean true if the key event is consumed by this method, false otherwise

getMaxScrollAmount

int getMaxScrollAmount ()

Returns
int The maximum amount this scroll view will scroll in response to an arrow event.

getNestedScrollAxes

int getNestedScrollAxes ()

返回此ViewGroup的嵌套滚动的当前轴。

返回 SCROLL_AXIS_NONE以外的ViewGroup目前充当层次结构中一个或多个后代视图的嵌套滚动父项。

Returns
int Flags indicating the current axes of nested scrolling

hasNestedScrollingParent

boolean hasNestedScrollingParent ()

如果此视图具有嵌套滚动父级,则返回true。

嵌套滚动父项的存在表示该视图已启动嵌套滚动,并且被视图层次结构的更上层的祖先视图接受。

Returns
boolean whether this view has a nested scrolling parent

isFillViewport

boolean isFillViewport ()

指示此ScrollView的内容是否拉伸以填充视口。

Returns
boolean True if the content fills the viewport, false otherwise.

isNestedScrollingEnabled

boolean isNestedScrollingEnabled ()

如果对此视图启用嵌套滚动,则返回true。

如果启用了嵌套滚动并且此View类实现支持它,则该视图将在适用时充当嵌套滚动子视图,将有关正在进行的滚动操作的数据转发给兼容且协作的嵌套滚动父级。

Returns
boolean true if nested scrolling is enabled

isSmoothScrollingEnabled

boolean isSmoothScrollingEnabled ()

Returns
boolean Whether arrow scrolling will animate its transition.

onAttachedToWindow

void onAttachedToWindow ()

这在视图附加到窗口时被调用。 此时它有一个Surface并将开始绘制。 请注意,此功能保证在onDraw(android.graphics.Canvas)之前onDraw(android.graphics.Canvas) ,但可以在第一次onDraw之前的任何时候调用 - 包括onMeasure(int, int)之前或之后。

onGenericMotionEvent

boolean onGenericMotionEvent (MotionEvent event)

实现此方法来处理通用运动事件。

通用运动事件描述操纵杆运动,鼠标悬停,触控板触摸,滚轮运动和其他输入事件。 运动事件的source指定了接收的输入类别。 此方法的实现必须在处理事件之前检查源中的位。 以下代码示例显示了这是如何完成的。

带有源类SOURCE_CLASS_POINTER通用运动事件被传递到指针下的视图。 所有其他通用运动事件都会传送到重点视图。

 public boolean onGenericMotionEvent(MotionEvent event) {
     if (event.isFromSource(InputDevice.SOURCE_CLASS_JOYSTICK)) {
         if (event.getAction() == MotionEvent.ACTION_MOVE) {
             // process the joystick movement...
             return true;
         }
     }
     if (event.isFromSource(InputDevice.SOURCE_CLASS_POINTER)) {
         switch (event.getAction()) {
             case MotionEvent.ACTION_HOVER_MOVE:
                 // process the mouse hover movement...
                 return true;
             case MotionEvent.ACTION_SCROLL:
                 // process the scroll wheel movement...
                 return true;
         }
     }
     return super.onGenericMotionEvent(event);
 }

Parameters
event MotionEvent: The generic motion event being processed.
Returns
boolean True if the event was handled, false otherwise.

onInterceptTouchEvent

boolean onInterceptTouchEvent (MotionEvent ev)

实施此方法来拦截所有触摸屏幕动作事件。 这允许您在事件发送给您的孩子时观看事件,并在任何时候掌握当前手势的所有权。

使用这个函数需要注意,因为它与View.onTouchEvent(MotionEvent)有一个相当复杂的交互,并且使用它需要以正确的方式来实现这个方法。 活动将按以下顺序收到:

  1. You will receive the down event here.
  2. The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.
  3. For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().
  4. If you return true from here, you will not receive any following events: the target view will receive the same event but with the action ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here.

Parameters
ev MotionEvent: The motion event being dispatched down the hierarchy.
Returns
boolean Return true to steal motion events from the children and have them dispatched to this ViewGroup through onTouchEvent(). The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here.

onNestedFling

boolean onNestedFling (View target, 
                float velocityX, 
                float velocityY, 
                boolean consumed)

从嵌套滚动中请求投掷。

这种方法表示嵌套滚动的孩子已经检测到适合于一阵子的条件。 通常这意味着触摸滚动以velocity的滚动方向结束,沿滚动轴达到或超过minimum fling velocity

如果嵌套的滚动子视图通常会抛出,但它位于其自己的内容的边缘,则可以使用此方法代替该嵌套的滚动父级。 父母可以有选择地消费一下,或者观察孩子的情绪。

Parameters
target View: View that initiated the nested scroll
velocityX float: Horizontal velocity in pixels per second
velocityY float: Vertical velocity in pixels per second
consumed boolean: true if the child consumed the fling, false otherwise
Returns
boolean true if this parent consumed or otherwise reacted to the fling

onNestedPreFling

boolean onNestedPreFling (View target, 
                float velocityX, 
                float velocityY)

在目标视图消耗它之前,将其反应为嵌套的拖动。

这种方法意味着一个嵌套的滚动孩子已经检测到沿着每个轴具有给定速度的飞掷。 通常这意味着触摸滚动以velocity的滚动方向结束,沿着可滚动的轴线满足或超过minimum fling velocity

如果嵌套滚动父级将动作作为pre-scroll一部分消耗,则它也可以使用预先动作来完成相同的动作。 通过从这种方法返回true ,父母表明孩子不应该true自己的内部内容。

Parameters
target View: View that initiated the nested scroll
velocityX float: Horizontal velocity in pixels per second
velocityY float: Vertical velocity in pixels per second
Returns
boolean true if this parent consumed the fling ahead of the target view

onNestedPreScroll

void onNestedPreScroll (View target, 
                int dx, 
                int dy, 
                int[] consumed)

在目标视图占用滚动的一部分之前,对正在进行的嵌套滚动进行处理。

当经常使用嵌套滚动时,父视图可能希望有机会在嵌套滚动子代之前使用滚动。 一个例子是一个包含可滚动列表的抽屉。 用户将希望能够在列表本身开始滚动之前将列表完全滚动到视图中。

onNestedPreScroll在嵌套滚动子进行调用时调用dispatchNestedPreScroll(int, int, int[], int[]) 该实现应报告dx,dy所报告的滚动像素是如何在consumed阵列中消耗的。 索引0对应于dx,索引1对应于dy。 该参数永远不会为空。 消耗[0]和消耗[1]的初始值将始终为0。

Parameters
target View: View that initiated the nested scroll
dx int: Horizontal scroll distance in pixels
dy int: Vertical scroll distance in pixels
consumed int: Output. The horizontal and vertical scroll distance consumed by this parent

onNestedScroll

void onNestedScroll (View target, 
                int dxConsumed, 
                int dyConsumed, 
                int dxUnconsumed, 
                int dyUnconsumed)

反应到正在进行的嵌套滚动。

当ViewParent的当前嵌套滚动子视图调度一个嵌套滚动事件时,将调用此方法。 要接收对此方法的调用,ViewParent必须先前返回true以便拨打onStartNestedScroll(View, View, int)

将滚动距离的消耗部分和未消耗部分都报告给ViewParent。 例如,实现可以选择使用消费部分来匹配或追踪多个子元素的滚动位置。 未消耗部分可用于允许连续拖动多个滚动或可拖动元素,诸如在垂直抽屉内滚动列表,其中抽屉在达到内部滚动内容的边缘时开始拖动。

Parameters
target View: The descendent view controlling the nested scroll
dxConsumed int: Horizontal scroll distance in pixels already consumed by target
dyConsumed int: Vertical scroll distance in pixels already consumed by target
dxUnconsumed int: Horizontal scroll distance in pixels not consumed by target
dyUnconsumed int: Vertical scroll distance in pixels not consumed by target

onNestedScrollAccepted

void onNestedScrollAccepted (View child, 
                View target, 
                int nestedScrollAxes)

响应成功声明嵌套滚动操作。

此方法将在onStartNestedScroll返回true后onStartNestedScroll 它为视图及其超类为嵌套滚动执行初始配置提供了机会。 如果存在这种方法的实现,则应始终调用其超类的此方法的实现。

Parameters
child View: Direct child of this ViewParent containing target
target View: View that initiated the nested scroll
nestedScrollAxes int: Flags consisting of SCROLL_AXIS_HORIZONTAL, SCROLL_AXIS_VERTICAL or both

onStartNestedScroll

boolean onStartNestedScroll (View child, 
                View target, 
                int nestedScrollAxes)

响应启动可嵌套滚动操作的后代视图,并在适当的情况下声明嵌套滚动操作。

该方法将被调用以响应调用startNestedScroll(int)的后代视图。 视图层次结构中的每个父节点将有机会通过返回true来响应并声明嵌套滚动操作。

ViewParent实现可能会重写此方法,以指示视图何时愿意支持即将开始的嵌套滚动操作。 如果它返回true,则在正在进行滚动操作期间,此ViewParent将成为目标视图的嵌套滚动父级。 当嵌套滚动完成后,此ViewParent将接收到onStopNestedScroll(View)的呼叫。

Parameters
child View: Direct child of this ViewParent containing target
target View: View that initiated the nested scroll
nestedScrollAxes int: Flags consisting of SCROLL_AXIS_HORIZONTAL, SCROLL_AXIS_VERTICAL or both
Returns
boolean true if this ViewParent accepts the nested scroll operation

onStopNestedScroll

void onStopNestedScroll (View target)

反应到嵌套滚动操作结束。

在嵌套滚动操作后执行清理。 当嵌套滚动停止时,将调用此方法,例如,当嵌套触摸滚动以ACTION_UPACTION_CANCEL事件结束时。 如果存在这种方法的实现,则应始终调用其超类的此方法的实现。

Parameters
target View: View that initiated the nested scroll

onTouchEvent

boolean onTouchEvent (MotionEvent ev)

实现此方法来处理触摸屏幕动作事件。

如果此方法用于检测点击操作,建议通过执行并调用performClick()来执行操作。 这将确保一致的系统行为,包括:

  • obeying click sound preferences
  • dispatching OnClickListener calls
  • handling ACTION_CLICK when accessibility features are enabled

Parameters
ev MotionEvent: The motion event.
Returns
boolean True if the event was handled, false otherwise.

pageScroll

boolean pageScroll (int direction)

响应“页面向上/向下”快捷按钮,处理滚动。 此方法将向上或向下滚动一页,并将焦点放在新可见区域的最顶层/最底层组件。 如果没有组件是焦点的合适候选者,则此滚动视图回收焦点。

Parameters
direction int: the scroll direction: FOCUS_UP to go one page up or FOCUS_DOWN to go one page down
Returns
boolean true if the key event is consumed by this method, false otherwise

requestChildFocus

void requestChildFocus (View child, 
                View focused)

当这个父母的孩子需要关注时调用

Parameters
child View: The child of this ViewParent that wants focus. This view will contain the focused view. It is not necessarily the view that actually has focus.
focused View: The view that is a descendant of child that actually has focus

requestChildRectangleOnScreen

boolean requestChildRectangleOnScreen (View child, 
                Rect rectangle, 
                boolean immediate)

当该组的小孩想要将特定矩形定位到屏幕上时调用。 ViewGroup重写这可以相信:

  • child will be a direct child of this group
  • rectangle will be in the child's content coordinates

ViewGroup压倒一切应该维护合同:

  • nothing will change if the rectangle is already visible
  • the view port will be scrolled only just enough to make the rectangle visible
    • Parameters
      child View: The direct child making the request.
      rectangle Rect: The rectangle in the child's coordinates the child wishes to be on the screen.
      immediate boolean: True to forbid animated or delayed scrolling, false otherwise
      Returns
      boolean Whether the group scrolled to handle the operation

requestDisallowInterceptTouchEvent

void requestDisallowInterceptTouchEvent (boolean disallowIntercept)

当孩子不希望这个父母及其祖先用 onInterceptTouchEvent(MotionEvent)拦截触摸事件时 onInterceptTouchEvent(MotionEvent)

这位家长应该将此通知传递给其父母。 这位家长必须在接触期间服从这个要求(也就是说,只有在这位家长收到了或取消后才清除标志。

Parameters
disallowIntercept boolean: True if the child does not want the parent to intercept touch events.

requestLayout

void requestLayout ()

当事情发生变化时调用它,这已经使这个视图的布局无效。 这将安排视图树的布局传递。 当视图层次结构当前处于布局阶段( isInLayout()时,不应该调用此视图层次结构( isInLayout() 。如果布局发生,则可以在当前布局阶段结束时(然后布局将再次运行)或当前帧结束后绘制并发生下一个布局。

覆盖此方法的子类应调用超类方法以正确处理可能的布局错误请求。

scrollTo

void scrollTo (int x, 
                int y)

设置视图的滚动位置。 这将导致对onScrollChanged(int, int, int, int)的呼叫,该视图将失效。

该版本还将滚动限制在我们孩子的范围内。

Parameters
x int: the x position to scroll to
y int: the y position to scroll to

setFillViewport

void setFillViewport (boolean fillViewport)

指示此滚动视图是否应扩展其内容高度以填充视口。

Parameters
fillViewport boolean: True to stretch the content's height to the viewport's boundaries, false otherwise.

setNestedScrollingEnabled

void setNestedScrollingEnabled (boolean enabled)

启用或禁用此视图的嵌套滚动。

如果此属性设置为true,则视图将被允许在当前层次结构中使用兼容父视图启动嵌套滚动操作。 如果这个视图没有实现嵌套滚动,这将不起作用。 嵌套滚动正在进行时禁用嵌套滚动具有stopping嵌套滚动的效果。

Parameters
enabled boolean: true to enable nested scrolling, false to disable

setOnScrollChangeListener

void setOnScrollChangeListener (NestedScrollView.OnScrollChangeListener l)

当此视图的滚动X或Y位置更改时,注册一个要调用的回调。

此版本的方法适用于所有版本的Android,回到API v4。

Parameters
l NestedScrollView.OnScrollChangeListener: The listener to notify when the scroll X or Y position changes.

也可以看看:

setSmoothScrollingEnabled

void setSmoothScrollingEnabled (boolean smoothScrollingEnabled)

设置是否使用箭头滚动来为其转场生成动画。

Parameters
smoothScrollingEnabled boolean: whether arrow scrolling will animate its transition

shouldDelayChildPressedState

boolean shouldDelayChildPressedState ()

如果应该延迟此ViewGroup的子项或后代的按下状态,则返回true。 通常,这应该对可以滚动的容器(例如List)完成。 这可以防止在用户实际尝试滚动内容时出现按下状态。 出于兼容性原因,默认实现返回true。 不滚动的子类通常应该重写此方法并返回false。

Returns
boolean

smoothScrollBy

void smoothScrollBy (int dx, 
                int dy)

scrollBy(int, int) ,但滚动而不是立即滚动。

Parameters
dx int: the number of pixels to scroll by on the X axis
dy int: the number of pixels to scroll by on the Y axis

smoothScrollTo

void smoothScrollTo (int x, 
                int y)

scrollTo(int, int) ,但滚动而不是立即滚动。

Parameters
x int: the position where to scroll on the X axis
y int: the position where to scroll on the Y axis

startNestedScroll

boolean startNestedScroll (int axes)

沿给定轴开始可嵌套滚动操作。

开始嵌套滚动的视图承诺遵守以下合同:

该视图将在启动滚动操作时调用startNestedScroll。 在触摸滚动的情况下,这对应于最初的ACTION_DOWN 在触摸滚动的情况下,嵌套滚动将以与requestDisallowInterceptTouchEvent(boolean)相同的方式自动终止。 在编程滚动的情况下,调用者必须显式调用stopNestedScroll()来指示嵌套滚动的结束。

如果startNestedScroll返回true,则找到合作父项。 如果返回false,则在下一次滚动之前,调用方可能会忽略此合约的其余部分。 当嵌套滚动正在进行时调用startNestedScroll将返回true。

在滚动的每个增量步骤中,调用者在计算出所请求的滚动增量后应调用dispatchNestedPreScroll 如果它返回true,则嵌套滚动父级至少部分消耗滚动,并且调用者应该调整它滚动的数量。

在应用滚动增量的其余部分后,调用者应该调用dispatchNestedScroll ,同时传递消耗的delta和未消耗的delta。 嵌套的滚动父级可能会以不同的方式处理这些值。 onNestedScroll(View, int, int, int, int)

Parameters
axes int: Flags consisting of a combination of SCROLL_AXIS_HORIZONTAL and/or SCROLL_AXIS_VERTICAL.
Returns
boolean true if a cooperative parent was found and nested scrolling has been enabled for the current gesture.

stopNestedScroll

void stopNestedScroll ()

停止正在进行的嵌套滚动。

当嵌套滚动当前不在进行中时调用此方法是无害的。

Protected methods

computeScrollDeltaToGetChildRectOnScreen

int computeScrollDeltaToGetChildRectOnScreen (Rect rect)

计算要在Y方向上滚动的数量,以便在屏幕上完全显示一个矩形(或者,如果比屏幕高,至少是屏幕的第一个屏幕大小)。

Parameters
rect Rect: The rect.
Returns
int The scroll delta.

getBottomFadingEdgeStrength

float getBottomFadingEdgeStrength ()

返回底部渐变边的强度或强度。 强度是介于0.0(无褪色)和1.0(全褪色)之间的值。 默认实现返回0.0或1.0,但两者之间没有值。 滚动发生时,子类应该重写此方法以提供更平滑的淡入淡出转换。

Returns
float the intensity of the bottom fade as a float between 0.0f and 1.0f

getTopFadingEdgeStrength

float getTopFadingEdgeStrength ()

返回顶部褪色边缘的强度或强度。 强度是介于0.0(无褪色)和1.0(全褪色)之间的值。 默认实现返回0.0或1.0,但两者之间没有值。 滚动发生时,子类应该重写此方法以提供更平滑的淡入淡出转换。

Returns
float the intensity of the top fade as a float between 0.0f and 1.0f

measureChild

void measureChild (View child, 
                int parentWidthMeasureSpec, 
                int parentHeightMeasureSpec)

要求这个视图的一个孩子测量自己,同时考虑到这个视图的MeasureSpec要求和填充。 繁重的工作由getChildMeasureSpec完成。

Parameters
child View: The child to measure
parentWidthMeasureSpec int: The width requirements for this view
parentHeightMeasureSpec int: The height requirements for this view

measureChildWithMargins

void measureChildWithMargins (View child, 
                int parentWidthMeasureSpec, 
                int widthUsed, 
                int parentHeightMeasureSpec, 
                int heightUsed)

要求这个观点的一个孩子衡量自己,同时考虑到这个观点的MeasureSpec要求以及它的填充和边距。 孩子必须拥有MarginLayoutParams这个繁重的工作是在getChildMeasureSpec中完成的。

Parameters
child View: 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)

onLayout

void onLayout (boolean changed, 
                int l, 
                int t, 
                int r, 
                int b)

当这个视图为每个孩子分配一个大小和位置时,从布局调用。 带孩子的派生类应该覆盖这个方法,并调用他们每个孩子的布局。

Parameters
changed boolean: This is a new size or position for this view
l int: Left position, relative to parent
t int: Top position, relative to parent
r int: Right position, relative to parent
b int: Bottom position, relative to parent

onMeasure

void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

测量视图及其内容以确定测量宽度和测量高度。 此方法由measure(int, int)调用, measure(int, int)子类覆盖以提供其内容的准确和有效的度量。

合同:覆盖此方法时,您必须致电setMeasuredDimension(int, int)来存储此视图的测量宽度和高度。 如果不这样做,将触发IllegalStateException ,由measure(int, int)引发。 调用超类' onMeasure(int, int)是一种有效的用法。

Measure的基类实现默认为背景大小,除非MeasureSpec允许更大的大小。 子类应该覆盖onMeasure(int, int)以提供更好的内容度量。

如果此方法被覆盖,则子类的责任是确保测量的高度和宽度至少为视图的最小高度和宽度( getSuggestedMinimumHeight()getSuggestedMinimumWidth() )。

Parameters
widthMeasureSpec int: horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec int: vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

onOverScrolled

void onOverScrolled (int scrollX, 
                int scrollY, 
                boolean clampedX, 
                boolean clampedY)

overScrollBy(int, int, int, int, int, int, int, int, boolean)调用以响应过度滚动操作的结果。

Parameters
scrollX int: New X scroll value in pixels
scrollY int: New Y scroll value in pixels
clampedX boolean: True if scrollX was clamped to an over-scroll boundary
clampedY boolean: True if scrollY was clamped to an over-scroll boundary

onRequestFocusInDescendants

boolean onRequestFocusInDescendants (int direction, 
                Rect previouslyFocusedRect)

当在滚动视图的儿童中寻找焦点时,需要小心一些,不要将焦点放在滚动屏幕外的东西上。 这比默认的ViewGroup实现更昂贵,否则这种行为可能已经成为默认值。

Parameters
direction int: One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
previouslyFocusedRect Rect: The rectangle (in this View's coordinate system) to give a finer grained hint about where focus is coming from. May be null if there is no hint.
Returns
boolean Whether focus was taken.

onRestoreInstanceState

void onRestoreInstanceState (Parcelable state)

吊钩允许视图重新应用以前由onSaveInstanceState()生成的其内部状态的表示。 这个函数永远不会被调用为null状态。

Parameters
state Parcelable: The frozen state that had previously been returned by onSaveInstanceState().

onSaveInstanceState

Parcelable onSaveInstanceState ()

钩子允许视图生成其内部状态的表示,稍后可用于创建具有相同状态的新实例。 此状态应仅包含不持久或以后不能重建的信息。 例如,您永远不会将当前位置存储在屏幕上,因为当视图的新实例放置在其视图层次结构中时会再次计算该位置。

您可能在此处存储的某些示例:文本视图中的当前光标位置(但通常不是文本本身,因为它存储在内容提供程序或其他永久性存储中),即当前在列表视图中选择的项目。

Returns
Parcelable Returns a Parcelable object containing the view's current dynamic state, or null if there is nothing interesting to save. The default implementation returns null.

onScrollChanged

void onScrollChanged (int l, 
                int t, 
                int oldl, 
                int oldt)

这是为了响应此视图中的内部滚动而调用的(即视图滚动了其自己的内容)。 这通常是因为scrollBy(int, int)scrollTo(int, int)已被调用。

Parameters
l int: Current horizontal scroll origin.
t int: Current vertical scroll origin.
oldl int: Previous horizontal scroll origin.
oldt int: Previous vertical scroll origin.

onSizeChanged

void onSizeChanged (int w, 
                int h, 
                int oldw, 
                int oldh)

当这个视图的大小发生变化时,这在布局期间被调用。 如果您刚刚添加到视图层次结构中,则会使用旧值0调用。

Parameters
w int: Current width of this view.
h int: Current height of this view.
oldw int: Old width of this view.
oldh int: Old height of this view.

Hooray!