对 ListView 中新添加的项目进行动画处理
如何为 ListView
中新添加的项目添加动画?
我有一个适配器,当我在列表中添加新项目时,我说adapter.notifyDataSetChanged();项目已添加,一切正常,但我的问题是我想要新添加的元素具有一些动画。
How can I animate newly added items in ListView
?
I have a adapter
and when I add new items in my list I say adapter.notifyDataSetChanged();
the items are added, everything works perfectly, but my problem is I want newly added element to have some animation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对
自定义适配器
的getView()
方法中的每个添加的元素进行动画
。从而实现
动画
。Animate
each added element in thegetView()
method of yourCustom Adapter
.And thereby achieve the
Animation
.关于 Android 动画的官方文档说,您可以使用 android:animateLayoutChanges="true" 设置动画在布局更改时触发。
摘自:http://developer.android.com/training/animation/layout.html
The official docs about animation in Android say that you can set an animation to trigger whenever the layout is changed, using android:animateLayoutChanges="true".
Taken from: http://developer.android.com/training/animation/layout.html
添加这种动画比我最初想象的要困难。根据您想要实现的动画类型,有两种方法。
使用LayoutAnimationController。
API 中有一个示例演示。
为每个
View
设置动画:这确实是一个黑客,但我发现向
ListView添加动画的唯一方法< /code> 的子项如下:
您可以尝试通知适配器您想要删除的项目的 id,并调用
adapter.notifyDataSetChanged();
。这将生成对适配器的 getView() 方法的调用。在其中您可以执行以下操作:动画完成后,您可以调用
adapter.notifyDataSetChanged()
将所有内容放置到位。Adding this kind of animations is harder than I first thought of. There are two ways depending of the kind of animation you are trying to achieve.
Using LayoutAnimationController.
There is an example in the API demos.
Animating each
View
:This is quite a hack but the only way I found to add an animation to
ListView
's children is the following:You can try notifying the adapter the id of the item you are willing to delete and call
adapter.notifyDataSetChanged();
. This will generate calls to the adapter'sgetView()
method. Inside it you can do something like:After the animation finished you can recall
adapter.notifyDataSetChanged()
to put everything in place.