使用动画的 Android ExpandableListView [英] Android ExpandableListView using animation

查看:28
本文介绍了使用动画的 Android ExpandableListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

<ExpandableListView
     android:id="@+id/listView"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >
</ExpandableListView>

我想在 onclick parent 时为孩子添加动画幻灯片.那我该怎么办?

i want add animation slide for child when onclick parent . So How can i do ?

推荐答案

最终更新

自从我写这个答案以来已经有一段时间了.从那时起,发生了很多变化.最大的变化是引入了 RecyclerView,它使列表或网格的动画变得容易.如果可以,我强烈建议切换到 RecyclerViews.对于那些做不到的人,我会看看我能做些什么来修复我的库的错误.

It's been quite a while since I wrote this answer. Since then a lot has changed. The biggest change is with the introduction of RecyclerView that makes animating a list or grid easy. I highly recommend switching over to RecyclerViews if you can. For those who can't I will see what I can do regarding fixing the bugs for my library.

原答案

我实际上不喜欢简单地使用带有展开动画的 ListView 的动画 ExpandableListView 的流行实现,因为在我的用例中,我的每个组都有很多孩子,因此使用普通 ListView 是不可行的因为子视图不会被回收,内存使用量会很大,性能很差.相反,我采用了一种更困难但更具可扩展性和灵活性的方法.

I actually do not like the popular implementation of an animated ExpandableListView that simply uses a ListView with an expand animation because in my use case, each of my groups had a lot of children, therefore it was not feasible to use a normal ListView as the child views will not be recycled and the memory usage will be huge with poor performance. Instead, I went with a much more difficult but more scalable and flexible approach.

我扩展了 ExpandableListView 类并覆盖了 onCollapse 和 onExpand 函数,我还创建了一个 BaseExpandableListAdapter 的子类,称为 AnimatedExpandableListAdapter.在适配器内部,我覆盖了 getChildView 函数并将该函数设为 final,以便该函数不能再次被覆盖.相反,我为子类提供了另一个名为 getRealChildView 的函数来覆盖以提供真正的子视图.然后我向类添加了一个动画标志,如果设置了动画标志,则让 getChildView 返回一个虚拟视图,如果未设置标志,则返回真实视图.现在有了舞台布景,我对 onExpand 执行以下操作:

I extended the ExpandableListView class and overrode the onCollapse and onExpand functions, I also created a subclass of a BaseExpandableListAdapter called AnimatedExpandableListAdapter. Inside the adapter, I overrode the getChildView function and made the function final so that the function cannot be overrode again. Instead I provided another function called getRealChildView for subclasses to override to provide a real child view. I then added an animation flag to the class and made getChildView return a dummy view if the animation flag was set and the real view if the flag was not set. Now with the stage set I do the following for onExpand:

  1. 在适配器中设置动画标志并告诉适配器哪个组正在扩展.
  2. 调用 notifyDataSetChanged()(强制适配器为屏幕上的所有视图调用 getChildView()).
  3. 适配器(在动画模式下)将为初始高度为 0 的扩展组创建一个虚拟视图.然后适配器将获取真正的子视图并将这些视图传递给虚拟视图.
  4. 然后虚拟视图将开始在它自己的 onDraw() 函数中绘制真正的子视图.
  5. 适配器将启动一个动画循环,该循环将扩展虚拟视图直到其大小合适.它还将设置一个动画侦听器,以便在动画完成后清除动画标志,并调用 notifyDataSetChanged().
  1. Set the animation flag in the adapter and tell the adapter which group is expanding.
  2. Call notifyDataSetChanged() (forces the adapter to call getChildView() for all views on screen).
  3. The adapter (in animation mode) will then create a dummy view for the expanding group that has initial height 0. The adapter will then get the real child views and pass these views to the dummy view.
  4. The dummy view will then start to draw the real child views within it's own onDraw() function.
  5. The adapter will kick off an animation loop that will expand the dummy view until it is of the right size. It will also set an animation listener so that it can clear the animation flag once the animation completes and will call notifyDataSetChanged() as well.

最后,完成所有这些后,我不仅能够获得所需的动画效果,还能够获得所需的性能,因为这种方法适用于拥有 100 多个孩子的组.

Finally with all of this done, I was able to not only get the desired animation effect but also the desired performance as this method will work with group with over 100 children.

对于折叠动画,需要做更多的工作来设置和运行这一切.特别是,当您覆盖 onCollapse 时,您不想调用父函数,因为它会立即折叠组,使您没有机会播放动画.相反,您希望在折叠动画结束时调用 super.onCollapse.

For the collapsing animation, a little more work needs to be done to get this all setup and running. In particular, when you override onCollapse, you do not want to call the parent's function as it will collapse the group immediately leaving you no chance to play an animation. Instead you want to call super.onCollapse at the end of the collapse animation.

更新:

这个周末我花了一些时间来重写我的这个 AnimatedExpandableListView 的实现,我在这里发布了一个示例用法的源代码:https://github.com/idunnololz/AnimatedExpandableListView/

I spent some time this weekend to rewrite my implementation of this AnimatedExpandableListView and I'm releasing the source with an example usage here: https://github.com/idunnololz/AnimatedExpandableListView/

这篇关于使用动画的 Android ExpandableListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆