CoordinatorLayout解析

166 阅读1分钟

介绍

CoordinatorLayout是一个功能更强大的FrameLayout,主要用于这两个方面:1.作为一个顶级Layout使用 2.作为一个容器用于处理多个子View之间的交互

源码解析

setupForInsets();
super.setOnHierarchyChangeListener(new HierarchyChangeListener());

if (ViewCompat.getImportantForAccessibility(this)
        == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
    ViewCompat.setImportantForAccessibility(this,
            ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
}

CoordinatorLayout的构造方法中,主要通过setupForInsets()设置了边界效果,状态栏的沉浸状态,以及设置了层级变化监听,在childViewAdd()和childViewRemoved的时候回调。之后,setImportantForAccessibility()更改了无障碍的重要性。 image.png HierarchyChangeListener对ViewGroup的OnHierarchyChangeListener进行主要一个扩展,在onChildViewRemoved()新增了一个onChildViewsChanged(EVENT_VIEW_REMOVED)的方法。 image.png onChildViewRemoved()传递被依赖的View到对应的Behavior中。

image.png 重点看这个循环,View的删除和嵌套滑动都在这里,通过Behavior的layoutDependsOn(),检查checkChild的依赖,在switch中调用了behavior的onDependentViewRemoved()