animating the childs of an expandablelistview when collapsing/expanding

ivov picture ivov · Dec 27, 2010 · Viewed 11.4k times · Source

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

Answer

Warpzit picture Warpzit · Apr 10, 2012

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.