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