【问题标题】:ListView within a listview列表视图中的列表视图
【发布时间】:2012-09-24 12:40:47
【问题描述】:

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

【问题讨论】:

  • 你尝试过什么吗?一个可扩展的列表视图可能是您正在寻找的
  • 不要将列表视图放在列表视图中
  • 我会选择 ExpandableListView,这里有一个教程:myandroidsolutions.blogspot.ro/2012/08/…
  • 在ExpandableListView中,问题是你不能混合标题的布局和点击后显示的列表的布局。
  • 为什么不启动另一个显示您的子列表视图的活动?

标签: android android-listview layout-animation


【解决方案1】:

很简单,您可以在布局中添加您的项目(通过 xml 或代码)并使用动画显示(隐藏)。这是乌迪尼奇的例子。它的列表视图项通过动画展开,并且只需要 4+ 的 API 级别。 这个例子很简单。您只在 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;
        }
    }
}

具体用法在项目中。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 可能,我也在想同样的事情。但是,有一次它迫使我改变 ListView 的 getView() 中的布局参数,这以某种方式破坏了它。
    猜你喜欢
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    相关资源
    最近更新 更多