5

ListView在 android 中有一个简单的列表结果。单击每个项目后,我希望它向下滑动展开并显示内容。有没有简单的方法在android中做到这一点?

任何帮助将不胜感激。

4

3 回答 3

5

这是乌迪尼奇的例子。它有 listview 项目扩展动画并且只需要 API 级别 4+ 基本上你需要一个动画类

/**
* 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;
        }
    }
}

并使用这个:

View toolbar = view.findViewById(R.id.toolbar);

                // Creating the expand animation for the item
                ExpandAnimation expandAni = new ExpandAnimation(toolbar, 500);

                // Start the animation on the toolbar
                toolbar.startAnimation(expandAni);

展开动画示例

于 2012-09-21T02:12:32.813 回答
3

看看这个答案。不仅如此,您还必须使用粗花呢动画。检查ApiDemos/Animation2示例。还可以查看 ApiDemos 中的 anim 文件夹。它对我有很大帮助。根据您的问题 slide_top_to_bottom 会有所帮助。

于 2010-04-24T17:02:25.713 回答
1

最简单的方法是使用ObjectAnimator

ObjectAnimator animation = ObjectAnimator.ofInt(yourTextView, "maxLines", 40);
animation.setDuration(200).start();

这会将 maxLines 从您的 TextView 更改为 40,超过 200 毫秒。

小心使用 yourTextView.getLineCount() 来确定要扩展的行数,因为在布局通过之前它不会给出准确的数字。我建议您硬编码一个比您预期的文本更长的 maxLines 值。您还可以使用 yourTextView.length() 除以每行预期的最少字符数来估算它。

于 2015-04-15T00:40:43.127 回答