在ExpandableStickyheaderList视图android中设置用于展开/折叠的自定义动画

时间:2016-01-18 19:19:23

标签: android animation

我正在使用ExpandableStickyHeaderListView 它现在工作得很完美,但我想做什么来动画崩溃/扩展部分
因为我已经实现了自定义IAnimationExecutor,如库的文档中所述,但我得到的结果是下一部分在动画结束前不会向上移动,我将上一部分的可见性设置为Gone 所以我在动画时间有空白区域,这太丑了 关于如何以正确的方式做到这一点的任何想法

这就是我的实施

 ExpandableStickyListHeadersListView.IAnimationExecutor fancyAnimExecutor = new ExpandableStickyListHeadersListView.IAnimationExecutor() {
        @Override
        public void executeAnim(final View target, int animType) {

            if (animType == ExpandableStickyListHeadersListView.ANIMATION_EXPAND) {

                Utils.expandOrCollapse(target,"expand");


            } else if (animType == ExpandableStickyListHeadersListView.ANIMATION_COLLAPSE) {
                Utils.expandOrCollapse(target,"collapse");


            }
        }
    };

和动画方法

public static void expandOrCollapse(final View v,String exp_or_colpse) {
    TranslateAnimation anim = null;
    if(exp_or_colpse.equals("expand"))
    {
        anim = new TranslateAnimation(0.0f, 0.0f, -v.getHeight(), 0.0f);
        v.setVisibility(View.VISIBLE);
    }
    else{
        anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, -v.getHeight());
        Animation.AnimationListener collapselistener= new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                v.setVisibility(View.GONE);
            }
        };

        anim.setAnimationListener(collapselistener);
    }

    // To Collapse
    //

    anim.setDuration(500);
    anim.setInterpolator(new LinearInterpolator());
    v.startAnimation(anim);
}

由于

1 个答案:

答案 0 :(得分:0)

试试这个:

public static void Expand(final View v) {
        v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final int targetHeight = v.getMeasuredHeight();

        // Older versions of android (pre API 21) cancel animations for views with a height of 0.
        v.getLayoutParams().height = 1;
        v.setVisibility(View.VISIBLE);
        Animation a = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {

                int height = (int) (targetHeight * interpolatedTime);


                v.getLayoutParams().height = interpolatedTime == 1
                        ? ViewGroup.LayoutParams.WRAP_CONTENT
                        : height;

                v.requestLayout();
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        a.setDuration(EXPAND_COLLAPSE_BAR_DURATION);
        v.startAnimation(a);
    }


    public static void Collapse(final View v) {

        final int initialHeight = v.getMeasuredHeight();

        Animation a = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if (interpolatedTime == 1) {
                    v.setVisibility(View.GONE);
                } else {
                    v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
                    v.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };


        a.setDuration(EXPAND_COLLAPSE_BAR_DURATION);
        a.setFillAfter(false);
        v.startAnimation(a);
    }