等待其他视图动画结束

时间:2013-07-25 20:44:47

标签: android android-layout android-animation

我有以下布局代码段:

    <LinearLayout
        android:id="@+id/tagContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true" >

    </LinearLayout>

    <TextView
        android:id="@+id/commentLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tagContainer" />

正如您所看到的,animateLayoutChanges上的true设置为LinearLayout,因此当项目添加到其中时,它会为添加设置动画。这没关系,但删除有问题。如果我删除一个项目,动画仍然可以正常播放,但下面的TextView会立即弹出并且不会等待动画完成。如何实现这一目标,甚至更好,使TextView动画与LinearLayout动画同步?

2 个答案:

答案 0 :(得分:20)

您可以通过以下方式从视图中检索LayoutTransition

mLinearLayout = findViewById(R.id.myLayout);
LayoutTransition layoutTransition = mLinearLayout.getLayoutTransition();
layoutTransition.addTransitionListener(new TransitionListener(){

            @Override
            public void endTransition(LayoutTransition arg0, ViewGroup arg1,
                    View arg2, int arg3) {
                switch(arg2.getId()){
                                //....
                                }
            }

            @Override
            public void startTransition(LayoutTransition transition,
                    ViewGroup container, View view, int transitionType) {
                switch(view.getId()){
                                //....
                                }

            }});

答案 1 :(得分:2)

这不是一个答案,而是一个现成的代码片段,您可以尝试检查正在发生的事情。

mContainer = (ViewGroup) v.findViewById(R.id.container);
if (Build.VERSION.SDK_INT >= 11) {
    mLayoutTransition = mContainer.getLayoutTransition();

    if (mLayoutTransition != null) {
        mLayoutTransition.addTransitionListener(new LayoutTransition.TransitionListener() {

            @Override
            public void startTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) {
                Log.d("\n\n startTransition: in "+container+" view "+view+" type "+ descr(transitionType));
            }

            @Override
            public void endTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) {
                Log.d("\n\n endTransition: in "+container+" view "+view+" type "+ descr(transitionType));
            }

            String descr(int transitionType) {
                String[] m = new String[]{"CHANGE_APPEARING","CHANGE_DISAPPEARING","APPEARING","DISAPPEARING"};
                return "" + transitionType + ": " + m[transitionType&3] + " changing="+( transitionType&LayoutTransition.CHANGING);
            }
        });
    }
}

对我来说,容器视图是LinearLayout,感兴趣的事件是:

public void endTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) { // UI thread
    if (transitionType == LayoutTransition.DISAPPEARING) {
        // start 2nd animation, it will be done while another view is moved
    }
}