17

I'm trying to animate my child views in an expandablelistview. I would like the child view to slide down from top to bottom when expanding a group and sliding from bottom to top when collapsing a group. I've looked at several methods (animating the viewgroup or the child views) but none seem to work very well or I'm not doing it right.

I've extended a class from BaseExpandableListAdapter to create my own custom adapter. I also have custom (xml) views for the groups/childs which I inflate in the getChildView and getGroupView methods.

I would only like the current collapsed/expanded group to animate it's child. Can anyone point me in the right direction? If you need more information or code please let me know!

Regards, Ivo

ivov
  • 171
  • 1
  • 1
  • 4

3 Answers3

5

So what I've done is to use a regular listview and then animate the row views when clicked.

I use this methode for animation: Android animate drop down/up view proper

It can be a bit tricky if the height for the view to be dropped down is wrap_content, for this problem I had to find and set the height before I start the animation:

public static void setHeightForWrapContent(Activity activity, View view) {
    DisplayMetrics metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int screenWidth = metrics.widthPixels;

    int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    int widthMeasureSpec = MeasureSpec.makeMeasureSpec(screenWidth, MeasureSpec.EXACTLY);

    view.measure(widthMeasureSpec, heightMeasureSpec);
    int height = view.getMeasuredHeight();
    view.getLayoutParams().height = height;
}

The view should be gone before the animation start and then made visible when animation starts.

Edit: I made a complete example here.

Community
  • 1
  • 1
Warpzit
  • 27,966
  • 19
  • 103
  • 155
2

Another solution is to use the Android-SlideExpandableListView library I wrote: https://github.com/tjerkw/Android-SlideExpandableListView

It builds upon ideas from Udinic and others.

More about it can be read in this blog post: http://tjerktech.wordpress.com/2012/06/23/an-emerging-android-ui-pattern-for-contextual-actions/

TjerkW
  • 2,086
  • 21
  • 26
  • Would be nice if you could fill in your answer with a explanation of what's at the other end of those links. Link only answers have a tendency to rot. Thanks. – Kev Jun 23 '12 at 15:39
2

You can add animation to each child view in the bindChildView method. In order to animate only the current group's child - just catch the onExpand event, read it's childs, save the ids on some array, and on the bindChildView - animate only the childs saved on that array.

Udinic
  • 3,014
  • 2
  • 25
  • 32
  • 3
    Can you be more spesific? Or do you have any sample code? Thanks. – user430926 Mar 29 '12 at 11:04
  • I'll elaborate: On the bindChildView yo have access to the child's View object. You can use the startAnimation to apply animation on the object. You can set an offset to each of them, to make the animation be gradual. – Udinic May 14 '12 at 10:01