Android expandableListView 的动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2385567/
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 05:38:01  来源:igfitidea点击:

Animation for expandableListView

androidanimationexpandablelistview

提问by dursun

Is it possible to apply an expand or collapse animation for expandableListView?

是否可以为 expandableListView 应用展开或折叠动画?

采纳答案by ccpizza

It can be done using a simple ListView that contains an initially hidden view and a custom class that extends Animation. The basic idea is to start with View.GONEthen gradually re-size the margin from a negative value to the required size while setting visibility to View.VISIBLE.

它可以使用一个简单的 ListView 来完成,该 ListView 包含一个最初隐藏的视图和一个扩展 Animation 的自定义类。基本思想是开始,View.GONE然后逐渐将边距从负值重新调整到所需的大小,同时将可见性设置为View.VISIBLE

See:

看:

..and finally

..最后

The last example contains all the code you need. It looks a bit hackish to me, especially the fact that you must initially set view.bottomMargin = -50or more, otherwise the animation does not work properly the first time, but so far I did not find any viable alternative (apart from using a ScrollView with your own container items instead of a ListView).

最后一个示例包含您需要的所有代码。对我来说这看起来有点骇人听闻,尤其是您必须最初设置view.bottomMargin = -50或更多的事实,否则动画第一次无法正常工作,但到目前为止我没有找到任何可行的替代方案(除了将 ScrollView 与您自己的容器一起使用)项而不是 ListView)。

And finally, this app includes the above example, among lots of other useful examples with links to sources:

最后,这个应用程序包括上面的例子,以及许多其他有用的例子,其中包含指向源的链接:

Update: Google removed the app from play store allegedly for intellectual property violation (although it only contained demos and links to open source projects), the author now made the apk available for direct download from http://goo.gl/ihcgsFor more details see https://plus.google.com/108176685096570584154/posts. NB: I'm not affiliated with the author.

更新:谷歌从 Play 商店中删除了涉嫌侵犯知识产权的应用程序(虽然它只包含演示和开源项目的链接),作者现在可以从http://goo.gl/ihcgs直接下载该 apk 了解更多详情见https://plus.google.com/108176685096570584154/posts。注意:我不隶属于作者。

回答by android developer

I've found a possible (partial) workaround for this problem.

我找到了解决此问题的可能(部分)解决方法。

first you need to store the scroll state of the ExpnadableListView :

首先,您需要存储 ExpnadableListView 的滚动状态:

@Override
public void onScrollStateChanged(final AbsListView view, final int scrollState) {
    this.mScrollState = scrollState;
}

public int getScrollState() {
    return this.mScrollState;
}

for the listView itself, you need to store which group was clicked, so that only its children will get animated:

对于 listView 本身,您需要存储单击了哪个组,以便只有它的子项才会被动画化:

mListView.setOnGroupClickListener(...
@Override
public boolean onGroupClick(...){
mGroupPosition=groupPosition;

now, in the getChildView() method, you check the state of the scrolling , and if it's idle, you start the animation, for example:

现在,在 getChildView() 方法中,您检查 scrolling 的状态,如果它处于空闲状态,则启动动画,例如:

public View getChildView(...) {
// <=prepare rootView and return it later
if (groupPosition==mGroupPosition&&getScrollState() == OnScrollListener.SCROLL_STATE_IDLE)
    rootView.setAnimation(...)

this will set an animation for the child views each time you expand the group.

每次展开组时,这将为子视图设置动画。

the drawback of this are:

这样做的缺点是:

  1. only for the expanded child views. you will need to think of extra logic to animate them when collapsing the group.
  2. all animations start at once . you will need to add multiple animations one after another if you wish that it would work otherwise.
  1. 仅适用于展开的子视图。在折叠组时,您需要考虑额外的逻辑来为它们设置动画。
  2. 所有动画立即开始。如果您希望它可以正常工作,则需要一个接一个地添加多个动画。

回答by Ajit Pratap Singh

I have done a similar job for a simple list view.For doing that I overrode the getView method and applied translate up( or down) animation on each list item.The degree of translation was decided by the position of the list item.

我为一个简单的列表视图做了类似的工作。为此,我覆盖了 getView 方法并在每个列表项上应用了向上(或向下)平移动画。平移的程度由列表项的位置决定。