animating the childs of an expandablelistview when

2019-02-06 00:39发布

问题:

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

回答1:

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.



回答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.



回答3:

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/