0

我有一个父 ListView,其中包含基于自定义布局的项目。当用户单击任何项​​目时,我需要向该项目添加一个子 ListView,并且应该显示父 ListView 的整个项目并带有扩展动画。[所有数据都需要动态添加]任何建议......

4

2 回答 2

1

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

于 2012-10-03T15:25:50.507 回答
1

很简单,您可以在布局中添加您的项目(通过 xml 或代码)并使用动画显示(隐藏)。这是乌迪尼奇的例子。它的列表视图项通过动画扩展,并且只需要 4+ 的 API 级别。这个例子很简单。您只在linearlayout被调用中定义您的项目toolbar

展开动画示例

在 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;
        }
    }
}

详细用法在项目中。

于 2012-10-03T15:29:38.513 回答