NestedScrolling机制
2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
NestedScrolling機制(可以稱為嵌套滾動或嵌套滑動)能夠讓父view和子view在滾動時進(jìn)行配合,其基本流程如下:
要實現(xiàn)這樣的交互,父View需要實現(xiàn)NestedScrollingParent接口,而子View需要實現(xiàn)NestedScrollingChild接口。
在這套交互機制中,child是動作的發(fā)起者,parent只是接受回調(diào)并作出響應(yīng)。
另外: 父view和子view并不需要是直接的父子關(guān)系,即如果“parent1包含parent2,parent2包含child”,則parent1和child仍能通過nestedScrolling機制進(jìn)行交互。
- NestedScrollView 已實現(xiàn) NestedScrollingParent和NestedScrollingChild
- RecyclerView 已實現(xiàn) NestedScrollingChild
- CoordinatorLayout 已實現(xiàn) NestedScrollingParent
關(guān)于動作的方法大概可以分成兩類
第一類:主要實現(xiàn)通用的的關(guān)聯(lián)動作,各種view的改變都可以使用。
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency)第二類:主要實現(xiàn)滾動的動作
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) public void onNestedScrollAccepted(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target) public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) public boolean onNestedFling(CoordinatorLayout coordinatorLayout, View child, View target, float velocityX, float velocityY, boolean consumed) public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, View child, View target, float velocityX, float velocityY)方法之間的具體對應(yīng)關(guān)系如下:
| startNestedScroll | onStartNestedScroll、onNestedScrollAccepted |
| dispatchNestedPreScroll | onNestedPreScroll |
| dispatchNestedScroll | onNestedScroll |
| stopNestedScroll | onStopNestedScroll |
流程
可以大致將嵌套滾動的流程概括如下(以觸摸滾動為例,慣性滾動(fling)的流程與此類似):
其他
1、要關(guān)聯(lián)滾動的View需要實現(xiàn)NestedScrollingChild接口,并在用NestedScrollingChildHelper輔助類處理相應(yīng)的回調(diào) 2、需要關(guān)聯(lián)滾動的View的父View需要實現(xiàn)NestedScrollingParent接口,可用NestedScrollingParentHelper輔助類處理相應(yīng)的回調(diào),并在相應(yīng)的回調(diào)方法中處理動作 3、自定義Behavior要繼承CoordinatorLayout.Behavior<V extends View> 4、CoordinatorLayout的直接子類設(shè)置layout_behavior屬性才有效果(CoordinatorLayout會循環(huán)調(diào)用每個子view的behavior)
參考:
NestedScrolling機制(一)——概述 http://blog.csdn.net/al4fun/article/details/53888990 源碼看CoordinatorLayout.Behavior原理 http://blog.csdn.net/qibin0506/article/details/50377592轉(zhuǎn)載于:https://my.oschina.net/u/2501904/blog/1489022
總結(jié)
以上是生活随笔為你收集整理的NestedScrolling机制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vue版本过渡变化
- 下一篇: 即时通讯应用战争开打,到底谁能最终定义我