NestedScrollView 源码学习(一)

NestedScrollView

1. ParentView

1. NestedScrollingParent

2. NestedScrollingParentHelper

NestedScrollingParent 是一个嵌套滑动父View的相关方法的接口对象。NestedScrollingParentHelper 是一个嵌套滑动父View的事件辅助类,主要是暴露出来与子view交互。

Method
  • onStartNestedScroll
    子View要开始滑动时,会寻找要跟它嵌套滑动的父view。当返回 true 时,表示这个父View将跟他一起执行嵌套滑动。
   /*
    * @param child Direct child of this ViewParent containing target
    * @param target View that initiated the nested scroll
    * @param nestedScrollAxes Flags consisting of {@link ViewCompat#SCROLL_AXIS_HORIZONTAL},
    *                         {@link ViewCompat#SCROLL_AXIS_VERTICAL} or both
    * @return true if this ViewParent accepts the nested scroll opera tion
    */
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes);
  • onNestedScrollAccepted
    当父View要跟随嵌套滑动后,此方法就提供一个机会,让父View和子View初始化嵌套滑动的配置,比如 横向滑动,纵向滑动,还是都有。
/**
     * @param child Direct child of this ViewParent containing target
     * @param target View that initiated the nested scroll
     * @param nestedScrollAxes Flags consisting of {@link ViewCompat#SCROLL_AXIS_HORIZONTAL},
     *                         {@link ViewCompat#SCROLL_AXIS_VERTICAL} or both
     * @see #onStartNestedScroll(View, View, int)
     * @see #onStopNestedScroll(View)
     */
    public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes);
  • onStopNestedScroll
    当嵌套滑动结束(结束就是 touch 的ACTION_UP,ACTION_CANCEL)时就会调用。
/**
     * React to a nested scroll operation ending.
     *
     * <p>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 {@link MotionEvent#ACTION_UP} or {@link MotionEvent#ACTION_CANCEL} event.
     * Implementations of this method should always call their superclass's implementation of this
     * method if one is present.</p>
     *
     * @param target View that initiated the nested scroll
     */
    public void onStopNestedScroll(View target);
  • onNestedScroll
    当正在执行嵌套滑动的的子View调度了嵌套滑动事件时,就会调用此方法。将已消耗的滑动距离和剩余的滑动距离都告诉父View。
    /**
     * @param target The descendent view controlling the nested scroll
     * @param dxConsumed Horizontal scroll distance in pixels already consumed by target
     * @param dyConsumed Vertical scroll distance in pixels already consumed by target
     * @param dxUnconsumed Horizontal scroll distance in pixels not consumed by target
     * @param dyUnconsumed Vertical scroll distance in pixels not consumed by target
     */
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
            int dxUnconsumed, int dyUnconsumed);
  • onNestedPreScroll
    当子View执行 dispatchNestedPreScroll(子view中的方法接口)时当用此方法。当正在执行嵌套滑动时,在子view滑动前,父view可以在方法中做些操作。
/**
    * @param target View that initiated the nested scroll
    * @param dx Horizontal scroll distance in pixels
    * @param dy Vertical scroll distance in pixels
    * @param consumed Output. The horizontal and vertical scroll distance consumed by this parent
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed);
    */
  • getNestedScrollAxes
    返回父view当前的坐标轴
/**
    * Return the current axes of nested scrolling for this NestedScrollingParent.
    *
    * <p>A NestedScrollingParent returning something other than {@link ViewCompat#SCROLL_AXIS_NONE}
    * is currently acting as a nested scrolling parent for one or more descendant views in
    * the hierarchy.</p>
    *
    * @return Flags indicating the current axes of nested scrolling
    * @see ViewCompat#SCROLL_AXIS_HORIZONTAL
    * @see ViewCompat#SCROLL_AXIS_VERTICAL
    * @see ViewCompat#SCROLL_AXIS_NONE
    */
   public int getNestedScrollAxes();
  • 还有些 Fling(快速滑动)的接口方法,这里就不讲了,大家可以自己看下源码。

2. ChildView

1. NestedScrollingChild

2. NestedScrollingChildHelper

NestedScrollingChild 是一个嵌套滑动子View的相关方法的接口对象。NestedScrollingChildHelper 是一个嵌套滑动子View的事件辅助类,主要是暴露出来与父view交互。

Method
  • setNestedScrollingEnabled
    设置子view是否可以滑动,如果 true 就是可以,否则不可以。
    当子view正在嵌套滑动时,设置为false,效果将与 stopNestedScroll() 一样。
/**
     * Enable or disable nested scrolling for this view.
     *
     * @param enabled true to enable nested scrolling, false to disable
     *
     * @see #isNestedScrollingEnabled()
     */
    public void setNestedScrollingEnabled(boolean enabled);
  • isNestedScrollingEnabled
    返回子view 是否可以嵌套滑动
/**
     * Returns true if nested scrolling is enabled for this view.
     *
     * @return true if nested scrolling is enabled
     *
     * @see #setNestedScrollingEnabled(boolean)
     */
    public boolean isNestedScrollingEnabled();
  • startNestedScroll
    1.开始一个嵌套滑动(横向嵌套滑动、纵向、横纵结合)。
    2.startNestedScroll 将会初始化滑动操作。当触发ACTION_DOWN事件时,就会触发 startNestedScroll。当执行ViewParent#requestDisallowInterceptTouchEvent(boolean) 时,会自动打断嵌套滑动操作。当结束嵌套滑动时,必须调用 stopNestedScroll 方法。
    3.如果 此方法返回true,表示找到了一个配合一起滑动的父View。如果返回 false,表明父View不关心此次的滑动事件。
    当正在滑动时,调用此方法,将会默认返回 true。
    4.每次滑动的步骤中一旦 startNestedScroll 计算出了滑动的距离,都会调用 dispatchNestedPreScroll 方法。如果此时有一起嵌套滑动的父View,父View就至少会消耗一部分滑动距离,这时子View的滑动距离就要相应的调整。
    5.在子View对剩余的滚动距离做了响应后,就要调用 dispatchNestedScroll 方法,将 已消耗的距离和剩余的距离告诉父View,父View可能会有不同的操作
/**
     * Begin a nestable scroll operation along the given axes.
     *
     * <p>A view starting a nested scroll promises to abide by the following contract:</p>
     *
     * <p>The view will call startNestedScroll upon initiating a scroll operation. In the case
     * of a touch scroll this corresponds to the initial {@link MotionEvent#ACTION_DOWN}.
     * In the case of touch scrolling the nested scroll will be terminated automatically in
     * the same manner as {@link ViewParent#requestDisallowInterceptTouchEvent(boolean)}.
     * In the event of programmatic scrolling the caller must explicitly call
     * {@link #stopNestedScroll()} to indicate the end of the nested scroll.</p>
     *
     * <p>If <code>startNestedScroll</code> 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.</p>
     *
     * <p>At each incremental step of the scroll the caller should invoke
     * {@link #dispatchNestedPreScroll(int, int, int[], int[]) 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.</p>
     *
     * <p>After applying the remainder of the scroll delta the caller should invoke
     * {@link #dispatchNestedScroll(int, int, int, int, int[]) dispatchNestedScroll}, passing
     * both the delta consumed and the delta unconsumed. A nested scrolling parent may treat
     * these values differently. See
     * {@link NestedScrollingParent#onNestedScroll(View, int, int, int, int)}.
     * </p>
     *
     * @param axes Flags consisting of a combination of {@link ViewCompat#SCROLL_AXIS_HORIZONTAL}
     *             and/or {@link ViewCompat#SCROLL_AXIS_VERTICAL}.
     * @return true if a cooperative parent was found and nested scrolling has been enabled for
     *         the current gesture.
     *
     * @see #stopNestedScroll()
     * @see #dispatchNestedPreScroll(int, int, int[], int[])
     * @see #dispatchNestedScroll(int, int, int, int, int[])
     */
    public boolean startNestedScroll(int axes);
  • stopNestedScroll
    结束嵌套滑动
/**
     * Stop a nested scroll in progress.
     *
     * <p>Calling this method when a nested scroll is not currently in progress is harmless.</p>
     *
     * @see #startNestedScroll(int)
     */
    public void stopNestedScroll();
  • hasNestedScrollingParent
    是否有配合滑动的父View
/**
     * Returns true if this view has a nested scrolling parent.
     *
     * <p>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.</p>
     *
     * @return whether this view has a nested scrolling parent
     */
    public boolean hasNestedScrollingParent();
  • dispatchNestedScroll
    子View化冻后,将嵌套滑动的相关信息传递给父View
/**
     * Dispatch one step of a nested scroll in progress.
     *
     * @param dxConsumed Horizontal distance in pixels consumed by this view during this scroll step
     * @param dyConsumed Vertical distance in pixels consumed by this view during this scroll step
     * @param dxUnconsumed Horizontal scroll distance in pixels not consumed by this view
     * @param dyUnconsumed Horizontal scroll distance in pixels not consumed by this view
     * @param 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.
     * @return true if the event was dispatched, false if it could not be dispatched.
     * @see #dispatchNestedPreScroll(int, int, int[], int[])
     */
    public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed,
            int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow);
  • dispatchNestedPreScroll
    在子View消费滑动距离之前,先告诉父View。在子View的onInterceptTouchEvent或者onTouch中(一般在MontionEvent.ACTION_MOVE事件里),调用该方法通知父View滑动的距离。该方法的第三第四个参数返回父view消费掉的scroll长度和子View的窗体偏移量。如果这个scroll没有被消费完,则子view进行处理剩下的一些距离,由于窗体进行了移动,如果你记录了手指最后的位置,需要根据第四个参数offsetInWindow计算偏移量,才能保证下一次的touch事件的计算是正确的。
    如果父view接受了它的滚动参数,进行了部分消费,则这个函数返回true,否则为false。
    这个函数一般在子view处理scroll前调用。
/**
     * Dispatch one step of a nested scroll in progress before this view consumes any portion of it.
     *
     * @param dx Horizontal scroll distance in pixels
     * @param dy Vertical scroll distance in pixels
     * @param consumed Output. If not null, consumed[0] will contain the consumed component of dx
     *                 and consumed[1] the consumed dy.
     * @param 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.
     * @return true if the parent consumed some or all of the scroll delta
     * @see #dispatchNestedScroll(int, int, int, int, int[])
     */
    public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow);
  • 还有些 Fling 方法,就不一一介绍了,大家可以自己查看源码。

交互对应流程

NestedScrollView-流程图.png

查看源码:在 NestedScrollView 的一些touch 监听中就可以看到对以上方法的调用。同时,可以看到在ChildHelper中主要调用了ViewParentCompat类的方法。ViewParentCompat是一个和父view交互的兼容类,它会判断api version,如果在Lollipop以上,就是用view自带的方法,否则判断是否实现了NestedScrollingParent接口,去调用接口的方法。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容

  • 简介: 提供一个让有限的窗口变成一个大数据集的灵活视图。 术语表: Adapter:RecyclerView的子类...
    酷泡泡阅读 5,053评论 0 16
  • 最新项目中用到了些Material效果,在此对自己的学习做个小结。 首先养成良好的学习习惯-----看源码: Co...
    风少侠阅读 4,816评论 5 37
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa阅读 8,816评论 0 6
  • 文 | 拾度 蒋来喜的祖上是地主。 来喜的爷爷,爷爷的爷爷,三辈人,唯一的目标,就是置地。村东,村西,村南,一大片...
    杜木土阅读 1,392评论 46 51
  • 今天自己一个人去看了《猩球崛起3》,知道它要上映的时候心里就很激动,喜欢前两部,所以特别期待第三部。 看完走出电影...
    甪玺阅读 560评论 0 0