1

我正在尝试使用动画扩展/折叠我的列表视图,并且我有以下代码。我想要的是一个列表视图,在屏幕的顶部,它可以展开和折叠,但是当它折叠起来时,我只想从列表视图中查看当前选定的项目(一项一项,一行)。

public static Animation expand(final View v, final boolean expand) {
        try {
            Method m = v.getClass().getDeclaredMethod("onMeasure", int.class,
                    int.class);
            m.setAccessible(true);
            m.invoke(v,
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(
                            ((View) v.getParent()).getMeasuredWidth(),
                            MeasureSpec.AT_MOST));
        } catch (Exception e) {
            e.printStackTrace();
        }
        final int initialHeight = v.getMeasuredHeight();
        if (expand) {
            v.getLayoutParams().height = 50;
        } else {
            v.getLayoutParams().height = initialHeight;
        }
        v.setVisibility(View.VISIBLE);
        Animation a = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime,
                    Transformation t) {
                int newHeight = 50;
                if (expand) {
                    newHeight = (int) (initialHeight * interpolatedTime);
                } else {
                    newHeight = (int) (initialHeight * (1 - interpolatedTime));
                }
                v.getLayoutParams().height = newHeight;
                v.requestLayout();
                if (interpolatedTime == 1 && !expand)
                    v.setVisibility(View.GONE);
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        a.setDuration(500);
        return a;
    }

问题是,当折叠时,我想让 listview 的第一行可见,而不是 listview 完全隐藏,但我找不到可以做到这一点的代码部分。请问有什么帮助吗?

提前非常感谢。

4

0 回答 0