Andrew
Andrew

Reputation: 45

How do I animate expansion of a ListView?

I'm looking to implement an animation that will slide open or expand a ListView in android. At the moment, I'm using a scale animation (shown below)

    Animation slideDown = new ScaleAnimation(1.0f, 1.0f, 0.0f, 1.0f);
    slideDown.setDuration(1000);

but this doesn't work for 2 reasons:

1) I want the first item to fade in, and then the list to drop down. If I do a fade in animation, the entire list shows up and then jumps back up to the top and scales down to full size

2) I'm not really looking to scale, I would like to just reveal the ListView by animating down the bottom (if that makes sense).

Is there a way that I can do a TranslateAnimation on just the bottom margin or something similar? I looked into the drawable Clip resource, but that didn't seem to fit my needs here (I'm not sure how to apply it to a ListView). And I'm using a normal ListView, not an ExpandableListView, because I just have one group of items.

Upvotes: 1

Views: 863

Answers (2)

Andrew
Andrew

Reputation: 45

So I started searching for different things and it turns out that animating the height was my solution. Essentially I start with a dummy object in the list (so that it has at least 1 row), then I animate it from the height of that row to the height of the full list. Below is the code for the runnable that I am now calling.

private Runnable slideDownList1 = new Runnable() {
    @Override
    public void run() {

        final ListView detail = (ListView)getView().findViewById(R.id.detail_menu_1);
            ValueAnimator va = ValueAnimator.ofInt(detail.getHeight(), detail.getHeight()*mItem.containedObjects.size());
            va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                public void onAnimationUpdate(ValueAnimator animation) {
                    Integer value = (Integer) animation.getAnimatedValue();
                    detail.getLayoutParams().height = value.intValue();
                    detail.requestLayout();
                }
            });
            va.setDuration(1000);
            va.start();
        }
};

The XML is just a basic ListView with the id of detail_menu_1.

Upvotes: 1

MiguelAngel_LV
MiguelAngel_LV

Reputation: 1258

The new element Recycler View include animations. You can use it with Support Library

https://developer.android.com/training/material/lists-cards.html

Upvotes: 0

Related Questions