Is there a way to add animation when opening an expandable list in Android? I want it so that when the user clicks on the expandable list, it has an animation/effect like I'm opening a sliding drawer.
It moves slow until it is completely opened.
Is there a way to add animation when opening an expandable list in Android? I want it so that when the user clicks on the expandable list, it has an animation/effect like I'm opening a sliding drawer.
It moves slow until it is completely opened.
So what I've done is use a regular
ListView
and then perform the animation in onListItemClick. The animation is similar to what I do at this link: Android animate drop down/up view properBut only for a portion of the row view. The row view is implemented in following way in xml:
The normal is used without expand. When expand is activated the expanded is set to visible instead of gone. You need to keep control of setting it to gone again when closing (remember this is set at your convertviews which are recycled).
I can clarify further if needed, it's just quite a lot of code to put in.
I have tried to make this work as well. I have found one solution that works for the childViews. It does not animate the actual expanding of the group though, but animates the child cells as they fill the space the expansion leaves behind.
Edit: There is a bug in collapsing, which will make some cells that should not be hidden, become hidden. This is probably related to View-recycling in the listView. I will will update when I have a solution to this.
Animating with layoutAnimation in setOnGroupClickListener
We need more tweaks to make the animation only apply to the actual children of the expanded/collapsed group. Because we can't overload the correct part in the LayoutAnimationController, we need to create a special ViewGroup class. This is the same technique as in , "Can LayoutAnimationController animate only specified Views".
In the ExpandableListViewAdapter, we now need some state handling to allow or ignore animation on items in the list.
And in the ViewHolder of the children.
The GroupViews are also contained in a AnimationAverseRelativeLayout. Since I have set "shouldAnimate" to default to false, I don't need to touch them.
That's what i did in this situations.
What this would do is that the height of the listView will get doubled and it would be animated. If you set the current list height ZERO. this would act like a drawer.
I've spent a lot of time searching with no luck. The existing solutions are just not smooth enough - if your layout is something more complex than just 2 buttons, it becomes laggy.
So, I've created my own
ListAdapter
that caches the whole view into aBitmap
and then performs the animation on the cached view instead of the view itself. It works much faster.Here it is: https://github.com/dmitry-zaitsev/ExpandableAdapter
The good news is that you don't need to rewrite a bunch of code - just wrap my
ExpandableAdapter
around your adapter and provide the id of the view that will act like a toggle button and the id of the view that holds the content of the second level:And that is all.
I had this same exact problem. And I fixed it once and for all. I open-sourced it to github. https://github.com/tjerkw/Android-SlideExpandableListView
Basically, you include this project dependency with your Android project. And then wrap your
ListAdapter
into aSlideExpandableListAdapter
. The wrapper will then add the slide functionality with animation to yourListView
.Hope it helps you, I'm already using it in two projects.