CoordinatorLayout分析

public class CoordinatorLayout extends ViewGroup implements NestedScrollingParent2

建立依赖

onMeasure-》prepareChildren-》dependsOn-》layoutDependsOn
测量的第一步就是确定依赖关系
prepareChildren中getResolvedLayoutParams找到支持behavior的节点,然后找出互相依赖关系(2个for扫描所有节点,两两对比),结果加入缓存列表
layoutDependsOn被子类覆盖,只需要对dependency判断返回成功,就建立依赖

视图改变

onNestedScroll-》onChildViewsChanged-》onDependentViewChanged

child的ontouch事件里有对parent的调用

behavior实例化parseBehavior内,ThreadLocal>>存放构造函数,用反射获取构造缓存

补充androidx.core.util.Pools

private final Object[] mPool;//默认的存储数组
private int mPoolSize;//当前剩余容量计数器

public T acquire() {
            if (mPoolSize > 0) {
                final int lastPooledIndex = mPoolSize - 1;
                T instance = (T) mPool[lastPooledIndex];
                mPool[lastPooledIndex] = null;
                mPoolSize--;
                return instance;
            }
            return null;
        }
private static Rect acquireTempRect() {
        Rect rect = sRectPool.acquire();
        if (rect == null) {
            rect = new Rect();
        }
        return rect;
    }
 public boolean release(@NonNull T instance) {
            if (isInPool(instance)) {
                throw new IllegalStateException("Already in the pool!");
            }
            if (mPoolSize < mPool.length) {
                mPool[mPoolSize] = instance;
                mPoolSize++;
                return true;
            }
            return false;
        }

        private boolean isInPool(@NonNull T instance) {
            for (int i = 0; i < mPoolSize; i++) {
                if (mPool[i] == instance) {
                    return true;
                }
            }
            return false;
        }
private static void releaseTempRect(@NonNull Rect rect) {
        rect.setEmpty();
        sRectPool.release(rect);
    }

总是从末尾获得引用,引用赋值后使用,释放时清空rect的值,但是存入到数组内,不释放引用,这样就是缓存了新建过程。

你可能感兴趣的:(CoordinatorLayout分析)