0

I have a parent ListView shown with items based on a custom layout. When a user clicks on any item then I need to add a child ListView to that item and should display the overall item of the parent ListView with expanding animation. [All data need to be added dynamically] Any suggestions....

Manjunath
  • 2,063
  • 2
  • 29
  • 60
  • 1
    have you tried anything? an expandable listview is probably what you are looking for – dymmeh Oct 03 '12 at 14:48
  • 4
    don't put a listview in a listview – njzk2 Oct 03 '12 at 14:49
  • 4
    I'd go for ExpandableListView, here is a tutorial: http://myandroidsolutions.blogspot.ro/2012/08/android-expandable-list-example.html – Cata Oct 03 '12 at 14:49
  • In the ExpandableListView, problem is you can't mix the layout of title and the layout of the list which will be show after click. – Manjunath Oct 03 '12 at 14:53
  • Why not just launch another activity that shows your child listview? – dymmeh Oct 03 '12 at 14:55
  • Nope, The requirement is so...when the user clicks on an item of prent ListView, I should add contents to child ListView and set it visible. Then, the height of the parent ListView's item becomes bigger so the item height should be expanded with animation. – Manjunath Oct 03 '12 at 14:59
  • Similar to this link, but this is a horizontal sliding thing, I need a vertical sliding thing http://nicolahibbert.com/demo/liteAccordion/ – Manjunath Oct 03 '12 at 15:06

2 Answers2

1

Instead of using a second ListView, you might want to consider using just a simple LinearLayout and populating it dynamically (toggling its visibility with View.VISIBLE and View.GONE). From what I know you shouldn't nest ListViews.

  • possibly, I am thinking the same thing. But, at one time it forces me to alter the layout parameters inside ListView's getView() which is spoiling it somehow. – Manjunath Oct 03 '12 at 15:27
1

Simple you can add your item in a layout (via xml or code) and show(hide) with animation. Here is example from Udinic. It had listview item expand with animation and require API level only 4+. This example's so simple. You only define your item in linearlayout called toolbar

ExpandAnimationExample

in onItemClick event use ExpanAnimation

/**
* This animation class is animating the expanding and reducing the size of a view.
* The animation toggles between the Expand and Reduce, depending on the current state of the view
* @author Udinic
*
*/
public class ExpandAnimation extends Animation {
    private View mAnimatedView;
    private LayoutParams mViewLayoutParams;
    private int mMarginStart, mMarginEnd;
    private boolean mIsVisibleAfter = false;
    private boolean mWasEndedAlready = false;

    /**
* Initialize the animation
* @param view The layout we want to animate
* @param duration The duration of the animation, in ms
*/
    public ExpandAnimation(View view, int duration) {

        setDuration(duration);
        mAnimatedView = view;
        mViewLayoutParams = (LayoutParams) view.getLayoutParams();

        // decide to show or hide the view
        mIsVisibleAfter = (view.getVisibility() == View.VISIBLE);

        mMarginStart = mViewLayoutParams.bottomMargin;
        mMarginEnd = (mMarginStart == 0 ? (0- view.getHeight()) : 0);

        view.setVisibility(View.VISIBLE);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);

        if (interpolatedTime < 1.0f) {

            // Calculating the new bottom margin, and setting it
            mViewLayoutParams.bottomMargin = mMarginStart
                    + (int) ((mMarginEnd - mMarginStart) * interpolatedTime);

            // Invalidating the layout, making us seeing the changes we made
            mAnimatedView.requestLayout();

        // Making sure we didn't run the ending before (it happens!)
        } else if (!mWasEndedAlready) {
            mViewLayoutParams.bottomMargin = mMarginEnd;
            mAnimatedView.requestLayout();

            if (mIsVisibleAfter) {
                mAnimatedView.setVisibility(View.GONE);
            }
            mWasEndedAlready = true;
        }
    }
}

Detail usage is in project.

Trung Nguyen
  • 7,442
  • 2
  • 45
  • 87