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閱讀 229,565評論 6 539
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,115評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,577評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,514評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,234評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,621評論 1 326
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,641評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,822評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,380評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,128評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,319評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,879評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,548評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,970評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,229評論 1 291
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,048評論 3 397
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,285評論 2 376

推薦閱讀更多精彩內容

  • 簡介: 提供一個讓有限的窗口變成一個大數據集的靈活視圖。 術語表: Adapter:RecyclerView的子類...
    酷泡泡閱讀 5,199評論 0 16
  • 最新項目中用到了些Material效果,在此對自己的學習做個小結。 首先養成良好的學習習慣-----看源碼: Co...
    風少俠閱讀 4,869評論 5 37
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa閱讀 8,893評論 0 6
  • 文 | 拾度 蔣來喜的祖上是地主。 來喜的爺爺,爺爺的爺爺,三輩人,唯一的目標,就是置地。村東,村西,村南,一大片...
    杜木土閱讀 1,467評論 46 51
  • 今天自己一個人去看了《猩球崛起3》,知道它要上映的時候心里就很激動,喜歡前兩部,所以特別期待第三部。 看完走出電影...
    甪璽閱讀 633評論 0 0