Android animateLayoutChanges 不适用于嵌套布局?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20103888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:18:58  来源:igfitidea点击:

animateLayoutChanges does not work well with nested layout?

androidandroid-layoutandroid-animation

提问by Chen

I have a nested layout like the following:

我有一个嵌套布局,如下所示:

 <LinearLayout>     <!----Parent layout--->
    <LinearLayout>    <!-----child 1--->
       ...
    </LinearLayout>   <!----child 1 ended--->
    <LinearLayout>    <!-----child 2--->
       ...
    </LinearLayout>   <!----child 2 ended--->
 </LinearLayout>    <!----Parent endded--->

The problem I am having now is that since all my data items are within child 1 or child 2 Linearlayout, if I add or delete a item the child linearlayout will animated with the effect of animateLayoutChanges but the parent layout will not do any animation. (I have android:animateLayoutChangesset to truefor all linear layouts). Especially when I delete an item within child 1 the animation effect becomes weird (basically child 2 will jump up while child 1 is still doing its animation).

我现在遇到的问题是,由于我的所有数据项都在 child 1 或 child 2 内Linearlayout,如果我添加或删除一个项目,则 child linearlayout 将使用 animateLayoutChanges 的效果进行动画处理,但父布局不会执行任何动画。(我已为所有线性布局android:animateLayoutChanges设置true为)。特别是当我删除孩子 1 中的一个项目时,动画效果变得很奇怪(基本上孩子 2 会跳起来,而孩子 1 仍在做动画)。

Does anybody have any idea how to solve this?

有谁知道如何解决这个问题?

Thanks

谢谢

UPDATE

更新

Shortly after I posted this question, I found this on android developer's site in the LayoutTransition API.

在我发布这个问题后不久,我在 LayoutTransition API 的 android 开发者网站上找到了这个。

Using LayoutTransition at multiple levels of a nested view hierarchy may not work due to the interrelationship of the various levels of layout.

由于布局的各个级别的相互关系,在嵌套视图层次结构的多个级别使用 LayoutTransition 可能不起作用。

So does anyone have any work around suggestions for this issue?

那么有没有人对这个问题有任何解决建议?

回答by Timo Hildén

The animateLayoutChangesproperty makes use of LayoutTransitions, which animate both the layout's children and, from Android 4.0 onward, ancestors in the layout hierarchy all the way to the top of the tree. In Honeycomb, only the layout's children will be animated. See this Android Developers Blog postfor details.

animateLayoutChanges属性使用了LayoutTransitions,它为布局的子项以及从 Android 4.0 开始的布局层次结构中的祖先一直到树的顶部进行动画处理。在 Honeycomb 中,只有布局的子项会被动画化。有关详细信息,请参阅此 Android 开发人员博客文章

Unfortunately, it seems that there's currently no simple way to have the siblings of a layout react to its LayoutTransitions. You could try using a TransitionListenerto get notified when the layout's bounds are being changed, and move the sibling views accordingly using Animators. See Chet Haase's second answer in this Google+ post.

不幸的是,目前似乎没有简单的方法可以让布局的兄弟姐妹对其LayoutTransitions. 您可以尝试使用 aTransitionListener在更改布局的边界时获得通知,并使用 Animators 相应地移动同级视图。请参阅此 Google+ 帖子中Chet Haase 的第二个答案。

EDIT- Turns out there isa way. In Android 4.1+ (API level 16+) you can use a layout transition type CHANGING, which is disabled by default. To enable it in code:

编辑-原来有一种方式。在 Android 4.1+(API 级别 16+)中,您可以使用CHANGING默认禁用的布局转换类型。要在代码中启用它:

ViewGroup layout = (ViewGroup) findViewById(R.id.yourLayout);
LayoutTransition layoutTransition = layout.getLayoutTransition();
layoutTransition.enableTransitionType(LayoutTransition.CHANGING);

So, in your example, to have child 2 layout animated, you'd need to enable the CHANGINGlayout transformation for it. The transformation would then be applied when there is a change in the bounds of its parent.

因此,在您的示例中,要为 child 2 布局设置动画,您需要为其启用CHANGING布局转换。当其父级的边界发生变化时,将应用转换。

See this DevBytes videofor more details.

有关更多详细信息,请参阅此 DevBytes 视频

回答by beokh

We had added the android:animateLayoutChanges attribute to our LinearLayout but the change didn't trigger an animation. To fix that, use this code:

我们已经将 android:animateLayoutChanges 属性添加到我们的 LinearLayout 但更改没有触发动画。要解决这个问题,请使用以下代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
((ViewGroup) findViewById(R.id.llRoot)).getLayoutTransition()
      .enableTransitionType(LayoutTransition.CHANGING);
}

More details.

更多详情

回答by Sam Chen

Ok, after digesting the first answer, I make it simple here, for those who don't get proper animation result when using android:animateLayoutChanges="true"in NESTEDlayout:

好的,消化完第一个答案后,我在这里简单说一下,对于那些android:animateLayoutChanges="true"NESTED布局中使用时没有得到正确动画结果的人:

  1. Make sure you add android:animateLayoutChanges="true"to the will-be-resizedViewGroup(LinearLayout/RelativeLayout/FrameLayout/CoordinatorLayout).

  2. Use setVisibility()to control the visibility of your targetView.

  3. Listen carefully from here,add android:animateLayoutChanges="true"to the outerViewGroupof your will-be-resizedViewGroup, this outerViewGroupmust be the one who wraps all the position-changing Viewaffecting by the animation.

  4. Add following code in your Activitybefore the setVisibility(), here the rootLinearLayoutis the outer ViewGroupI mentioned above:

    LayoutTransition layoutTransition = rootLinearLayout.getLayoutTransition();
    layoutTransition.enableTransitionType(LayoutTransition.CHANGING);
    
  1. 请确保您添加android:animateLayoutChanges="true"将待调整ViewGroupLinearLayout/ RelativeLayout/ FrameLayout/ CoordinatorLayout)。

  2. 使用setVisibility()来控制你的知名度目标View

  3. 从这里仔细听,添加android:animateLayoutChanges="true"ViewGroup你的意志来调整大小ViewGroup,此ViewGroup必须是谁包装了一个所有的位置变化View由动画的影响。

  4. Activity之前添加以下代码setVisibility(),这里rootLinearLayoutViewGroup我上面提到的外部:

    LayoutTransition layoutTransition = rootLinearLayout.getLayoutTransition();
    layoutTransition.enableTransitionType(LayoutTransition.CHANGING);
    


Before:

前:

enter image description here

在此处输入图片说明

After:

后:

enter image description here

在此处输入图片说明



Reminder: If you miss the 3rd step, you will get null pointer exception.

提醒:如果你错过了第三步,你会得到空指针异常。

Good luck!

祝你好运!

回答by sihaya

It seems that a delayed transition on the parent also works for animating. At least for me the following code gives a proper expand/collapse animation.

似乎父母的延迟过渡也适用于动画。至少对我而言,以下代码提供了适当的展开/折叠动画。

expandTrigger.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        TransitionManager.beginDelayedTransition(parentLayout);
        expanded = !expanded;

        child1.setVisibility(expanded ? View.VISIBLE : View.GONE);
    }
});

For deeply nested layouts you sometimes might need to use a parent higher up in the hierarchy in the call to the TransitionManager.

对于深度嵌套的布局,您有时可能需要在调用TransitionManager.