LayoutTransition的坑

来源:互联网 发布:twemproxy 源码解析 编辑:程序博客网 时间:2024/05/21 14:03

最近在做ViewGroup的子view出现和消失动画的时候,使用到了LayoutTransition。

发现了两个坑:
1.影响兄弟View
2.CHANGE_APPEARING和CHANGE_DISAPPEARING两种动画不会执行


先说第一个问题,当View A setLayoutTransition后,A的兄弟View也会做动画。

经过一番调研,在执行这个函数中,有个一个mAnimateParentHierarchy这个属性,它会导致动画被set到本view的父view中,因此会影响本view的兄弟view。而它的默认值是true。

private void runChangeTransition(final ViewGroup parent, View newView, final int changeReason) {        //.....        if (mAnimateParentHierarchy) {            ViewGroup tempParent = parent;            while (tempParent != null) {                ViewParent parentParent = tempParent.getParent();                if (parentParent instanceof ViewGroup) {                    setupChangeAnimation((ViewGroup)parentParent, changeReason, parentAnimator,                            duration, tempParent);                    tempParent = (ViewGroup) parentParent;                } else {                    tempParent = null;                }            }        }        // This is the cleanup step. When we get this rendering event, we know that all of        // the appropriate animations have been set up and run. Now we can clear out the        // layout listeners.        CleanupCallback callback = new CleanupCallback(layoutChangeListenerMap, parent);        observer.addOnPreDrawListener(callback);        parent.addOnAttachStateChangeListener(callback);    }

第二个坑,CHANGE_APPEARING和CHANGE_DISAPPEARING两种动画不会执行。

这两个动画依赖于子view发生layout的change,使用OnLayoutChangeListener来触发动画。

因此需要保证发生子view的layout的change,举个简单的例子,如果父view是一个wrap_content的,在最后新增或者删除子view,不会导致其他子view发生layout的change,只是整个父view的大小发生了变化而已,因此动画也不会被执行。