Android 中 Behavior, NestedScrollingParent, NestedScrollingChild 关系
NestedScroll 相關接口:NestedScrollingParent, NestScrollingChild
頂層布局需要用CoordinatorLayout, Behavior 是CoordinatorLayout 內部類
public class CoordinatorLayout extends ViewGroup implements NestedScrollingParent2 {public static abstract class Behavior<V extends View> {}}NestedScroll 解決了子View 滑動時,父View需要滑動的情況, 子View 把需要滑動的距離傳遞給父View, 父View 可以消耗一部分距離,然后通知子View
子View滑動回調父View的接口:
| ? ?子View | 父View |
| startNestedScroll? ?--開始滑動 | onStartNestedScroll,onNestedScrollAccepted |
| dispatchNestedPreScroll? --預滑動 | onNestedPreScroll |
| dispatchNestedScroll? ?--滑動 | onNestedScroll |
| stopNestedScroll? ?--停止滑動 | onStopNestedScroll |
Behavior 是子View 與父View 之間協調者的角色, 父View的 事件它都有實現,在CoordinatorLayout 的方法實現中,都委托Behavior 進行處理。
@Overridepublic void onNestedScroll(View target, int dxConsumed, int dyConsumed,int dxUnconsumed, int dyUnconsumed) {// 遍歷所有子Viewfor (int i = 0; i < childCount; i++) {final View view = getChildAt(i);if (view.getVisibility() == GONE) {// If the child is GONE, skip...continue;}final LayoutParams lp = (LayoutParams) view.getLayoutParams();if (!lp.isNestedScrollAccepted(type)) {continue;}final Behavior viewBehavior = lp.getBehavior();if (viewBehavior != null) {
//委托給behavior進行處理viewBehavior.onNestedScroll(this, view, target, dxConsumed, dyConsumed,dxUnconsumed, dyUnconsumed, type);accepted = true;}}if (accepted) {onChildViewsChanged(EVENT_NESTED_SCROLL);}}
另外還有兩個類:
NestedScrollingChildHelper輔助類
NestedScrollingParentHelper 輔助類
它們是為了適配Android 5.0 之前的版本
前面介紹了NestedScrollingParent, NestedScrollingChild, Behavior 三者之間的關系,下面看下嵌套滑動的常見實現:
1。 Toolbar+ 圖片,圖片滑動上去時,Toolbar顯示
? ? ?CoordinatorLayout + ToolbarLayout + CollapsingToolbarLayout + Toolbar?
2.? ?Toolbar + TabLayout + ViewPager?
?
轉載于:https://www.cnblogs.com/songsh/p/6530024.html
總結
以上是生活随笔為你收集整理的Android 中 Behavior, NestedScrollingParent, NestedScrollingChild 关系的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 19图的搜索算法总结与比较
- 下一篇: [CodeForces1110C]Mea