NestedScrollView 源碼學(xué)習(xí)(一)

NestedScrollView

1. ParentView

1. NestedScrollingParent

2. NestedScrollingParentHelper

NestedScrollingParent 是一個(gè)嵌套滑動(dòng)父View的相關(guān)方法的接口對(duì)象索抓。NestedScrollingParentHelper 是一個(gè)嵌套滑動(dòng)父View的事件輔助類,主要是暴露出來與子view交互辆飘。

Method
  • onStartNestedScroll
    子View要開始滑動(dòng)時(shí),會(huì)尋找要跟它嵌套滑動(dòng)的父view哄孤。當(dāng)返回 true 時(shí)悴势,表示這個(gè)父View將跟他一起執(zhí)行嵌套滑動(dòng)钱贯。
   /*
    * @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
    當(dāng)父View要跟隨嵌套滑動(dòng)后挫掏,此方法就提供一個(gè)機(jī)會(huì),讓父View和子View初始化嵌套滑動(dòng)的配置秩命,比如 橫向滑動(dòng)尉共,縱向滑動(dòng),還是都有弃锐。
/**
     * @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
    當(dāng)嵌套滑動(dòng)結(jié)束(結(jié)束就是 touch 的ACTION_UP,ACTION_CANCEL)時(shí)就會(huì)調(diào)用袄友。
/**
     * 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
    當(dāng)正在執(zhí)行嵌套滑動(dòng)的的子View調(diào)度了嵌套滑動(dòng)事件時(shí),就會(huì)調(diào)用此方法霹菊。將已消耗的滑動(dòng)距離和剩余的滑動(dòng)距離都告訴父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
    當(dāng)子View執(zhí)行 dispatchNestedPreScroll(子view中的方法接口)時(shí)當(dāng)用此方法。當(dāng)正在執(zhí)行嵌套滑動(dòng)時(shí)浇辜,在子view滑動(dòng)前,父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當(dāng)前的坐標(biāo)軸
/**
    * 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(快速滑動(dòng))的接口方法柳洋,這里就不講了,大家可以自己看下源碼叹坦。

2. ChildView

1. NestedScrollingChild

2. NestedScrollingChildHelper

NestedScrollingChild 是一個(gè)嵌套滑動(dòng)子View的相關(guān)方法的接口對(duì)象熊镣。NestedScrollingChildHelper 是一個(gè)嵌套滑動(dòng)子View的事件輔助類,主要是暴露出來與父view交互。

Method
  • setNestedScrollingEnabled
    設(shè)置子view是否可以滑動(dòng)绪囱,如果 true 就是可以测蹲,否則不可以。
    當(dāng)子view正在嵌套滑動(dòng)時(shí)鬼吵,設(shè)置為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 是否可以嵌套滑動(dòng)
/**
     * 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.開始一個(gè)嵌套滑動(dòng)(橫向嵌套滑動(dòng)齿椅、縱向琉挖、橫縱結(jié)合)。
    2.startNestedScroll 將會(huì)初始化滑動(dòng)操作涣脚。當(dāng)觸發(fā)ACTION_DOWN事件時(shí)示辈,就會(huì)觸發(fā) startNestedScroll。當(dāng)執(zhí)行ViewParent#requestDisallowInterceptTouchEvent(boolean) 時(shí)遣蚀,會(huì)自動(dòng)打斷嵌套滑動(dòng)操作矾麻。當(dāng)結(jié)束嵌套滑動(dòng)時(shí),必須調(diào)用 stopNestedScroll 方法芭梯。
    3.如果 此方法返回true险耀,表示找到了一個(gè)配合一起滑動(dòng)的父View。如果返回 false,表明父View不關(guān)心此次的滑動(dòng)事件粥帚。
    當(dāng)正在滑動(dòng)時(shí)胰耗,調(diào)用此方法,將會(huì)默認(rèn)返回 true芒涡。
    4.每次滑動(dòng)的步驟中一旦 startNestedScroll 計(jì)算出了滑動(dòng)的距離柴灯,都會(huì)調(diào)用 dispatchNestedPreScroll 方法。如果此時(shí)有一起嵌套滑動(dòng)的父View费尽,父View就至少會(huì)消耗一部分滑動(dòng)距離赠群,這時(shí)子View的滑動(dòng)距離就要相應(yīng)的調(diào)整。
    5.在子View對(duì)剩余的滾動(dòng)距離做了響應(yīng)后旱幼,就要調(diào)用 dispatchNestedScroll 方法查描,將 已消耗的距離和剩余的距離告訴父View,父View可能會(huì)有不同的操作
/**
     * 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
    結(jié)束嵌套滑動(dòng)
/**
     * 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
    是否有配合滑動(dòng)的父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化凍后柏卤,將嵌套滑動(dòng)的相關(guān)信息傳遞給父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消費(fèi)滑動(dòng)距離之前冬三,先告訴父View。在子View的onInterceptTouchEvent或者onTouch中(一般在MontionEvent.ACTION_MOVE事件里)缘缚,調(diào)用該方法通知父View滑動(dòng)的距離勾笆。該方法的第三第四個(gè)參數(shù)返回父view消費(fèi)掉的scroll長(zhǎng)度和子View的窗體偏移量。如果這個(gè)scroll沒有被消費(fèi)完桥滨,則子view進(jìn)行處理剩下的一些距離窝爪,由于窗體進(jìn)行了移動(dòng)弛车,如果你記錄了手指最后的位置,需要根據(jù)第四個(gè)參數(shù)offsetInWindow計(jì)算偏移量蒲每,才能保證下一次的touch事件的計(jì)算是正確的纷跛。
    如果父view接受了它的滾動(dòng)參數(shù),進(jìn)行了部分消費(fèi)邀杏,則這個(gè)函數(shù)返回true贫奠,否則為false。
    這個(gè)函數(shù)一般在子view處理scroll前調(diào)用淮阐。
/**
     * 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 方法叮阅,就不一一介紹了,大家可以自己查看源碼泣特。

交互對(duì)應(yīng)流程

NestedScrollView-流程圖.png

查看源碼:在 NestedScrollView 的一些touch 監(jiān)聽中就可以看到對(duì)以上方法的調(diào)用浩姥。同時(shí),可以看到在ChildHelper中主要調(diào)用了ViewParentCompat類的方法状您。ViewParentCompat是一個(gè)和父view交互的兼容類勒叠,它會(huì)判斷api version,如果在Lollipop以上膏孟,就是用view自帶的方法眯分,否則判斷是否實(shí)現(xiàn)了NestedScrollingParent接口,去調(diào)用接口的方法柒桑。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末弊决,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子魁淳,更是在濱河造成了極大的恐慌飘诗,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件界逛,死亡現(xiàn)場(chǎng)離奇詭異昆稿,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)息拜,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門溉潭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人少欺,你說我怎么就攤上這事喳瓣。” “怎么了赞别?”我有些...
    開封第一講書人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵畏陕,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我氯庆,道長(zhǎng)蹭秋,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任堤撵,我火速辦了婚禮仁讨,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘实昨。我一直安慰自己洞豁,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開白布荒给。 她就那樣靜靜地躺著丈挟,像睡著了一般。 火紅的嫁衣襯著肌膚如雪志电。 梳的紋絲不亂的頭發(fā)上曙咽,一...
    開封第一講書人閱讀 51,370評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音挑辆,去河邊找鬼例朱。 笑死,一個(gè)胖子當(dāng)著我的面吹牛鱼蝉,可吹牛的內(nèi)容都是我干的洒嗤。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼魁亦,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼渔隶!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起洁奈,我...
    開封第一講書人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤间唉,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后睬魂,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體终吼,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年氯哮,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了际跪。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡喉钢,死狀恐怖姆打,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情肠虽,我是刑警寧澤幔戏,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站税课,受9級(jí)特大地震影響闲延,放射性物質(zhì)發(fā)生泄漏痊剖。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一垒玲、第九天 我趴在偏房一處隱蔽的房頂上張望陆馁。 院中可真熱鬧,春花似錦合愈、人聲如沸叮贩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽益老。三九已至,卻和暖如春寸莫,著一層夾襖步出監(jiān)牢的瞬間捺萌,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工储狭, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留互婿,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓辽狈,卻偏偏與公主長(zhǎng)得像慈参,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子刮萌,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 簡(jiǎn)介: 提供一個(gè)讓有限的窗口變成一個(gè)大數(shù)據(jù)集的靈活視圖驮配。 術(shù)語表: Adapter:RecyclerView的子類...
    酷泡泡閱讀 5,165評(píng)論 0 16
  • 最新項(xiàng)目中用到了些Material效果,在此對(duì)自己的學(xué)習(xí)做個(gè)小結(jié)着茸。 首先養(yǎng)成良好的學(xué)習(xí)習(xí)慣-----看源碼: Co...
    風(fēng)少俠閱讀 4,862評(píng)論 5 37
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa閱讀 8,864評(píng)論 0 6
  • 文 | 拾度 蔣來喜的祖上是地主壮锻。 來喜的爺爺,爺爺?shù)臓敔斾汤吶瞬滦澹ㄒ坏哪繕?biāo),就是置地敬特。村東掰邢,村西,村南伟阔,一大片...
    杜木土閱讀 1,448評(píng)論 46 51
  • 今天自己一個(gè)人去看了《猩球崛起3》,知道它要上映的時(shí)候心里就很激動(dòng),喜歡前兩部多搀,所以特別期待第三部歧蕉。 看完走出電影...
    甪璽閱讀 621評(píng)論 0 0