问题描述
我有一个如下所示的嵌套布局:
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--->
我现在遇到的问题是,由于我的所有数据项都在 child 1 或 child 2 Linearlayout
中,如果我添加或删除一个项目,则 child linearlayout 将使用 animateLayoutChanges 的效果进行动画处理,但是父布局不会做任何动画.(我将所有线性布局的 android:animateLayoutChanges
设置为 true
).特别是当我删除孩子 1 中的一个项目时,动画效果变得很奇怪(基本上孩子 2 会跳起来,而孩子 1 仍在做动画).
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:animateLayoutChanges
set to true
for 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).
有人知道如何解决这个问题吗?
Does anybody have any idea how to solve this?
谢谢
更新
在我发布这个问题后不久,我在 android 开发者网站上的 LayoutTransition API 中发现了这个问题.
Shortly after I posted this question, I found this on android developer's site in the LayoutTransition API.
在嵌套视图层次结构的多个级别使用 LayoutTransition 可能无法正常工作,因为各个级别的布局之间存在相互关系.
那么有没有人对此问题有任何解决建议?
So does anyone have any work around suggestions for this issue?
推荐答案
animateLayoutChanges
属性利用了 LayoutTransitions
,它为布局的子项和 Android 4.0 中的子项设置动画向前,布局层次结构中的祖先一直到树的顶部.在 Honeycomb 中,只有布局的子项会被动画化.有关详细信息,请参阅这篇 Android 开发者博客文章.
The animateLayoutChanges
property 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 post for details.
不幸的是,目前似乎没有简单的方法可以让布局的兄弟姐妹对其 LayoutTransitions
做出反应.您可以尝试使用 TransitionListener
在布局的边界发生更改时获得通知,并使用 Animators 相应地移动同级视图.在这篇 Google+ 帖子中查看 Chet Haase 的第二个答案.
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 TransitionListener
to 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.
编辑 - 事实证明这是一种方法.在 Android 4.1+(API 级别 16+)中,您可以使用默认禁用的布局转换类型 CHANGING
.在代码中启用它:
EDIT - Turns out there is a 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:
ViewGroup layout = (ViewGroup) findViewById(R.id.yourLayout);
LayoutTransition layoutTransition = layout.getLayoutTransition();
layoutTransition.enableTransitionType(LayoutTransition.CHANGING);
因此,在您的示例中,要为 child 2 布局设置动画,您需要为其启用 CHANGING
布局转换.当其父级的边界发生变化时,将应用转换.
So, in your example, to have child 2 layout animated, you'd need to enable the CHANGING
layout transformation for it. The transformation would then be applied when there is a change in the bounds of its parent.
有关详细信息,请参阅此 DevBytes 视频.
See this DevBytes video for more details.
这篇关于animateLayoutChanges 不适用于嵌套布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!