列表视图中的ListView

时间:2012-10-03 14:46:39

标签: android android-listview layout-animation

我有一个显示基于自定义布局的项目的父ListView。当用户点击任何项目时,我需要将子ListView添加到该项目,并应显示具有扩展动画的父ListView的整体项目。 [所有数据都需要动态添加] 任何建议......

2 个答案:

答案 0 :(得分:1)

您可能只想使用一个简单的LinearLayout并动态填充它(使用View.VISIBLE和View.GONE切换其可见性),而不是使用第二个ListView。据我所知,你不应该嵌套ListViews。

答案 1 :(得分:1)

简单,您可以在布局中添加项目(通过xml或代码)并使用动画显示(隐藏)。这是Udinic的例子。它有listview项目扩展动画,要求API级别只有4+。 这个例子很简单。您只能在名为linearlayout

toolbar中定义项目

ExpandAnimationExample

onItemClick事件中的

使用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;
        }
    }
}

详细用法在项目中。