我有一个ListView的项目需要用动画折叠,然后删除。我使用Animation来折叠项目,折叠完成后,我从ListView中删除项目(从数据列表中删除并调用notifyDataSetChanged)。为了检测动画是否完成,我在interpolatedTime方法中检查applyTransformation==1.0。问题是applyTransformation调用了两次,interpolatedtime==1,所以我不能真正依赖它(否则我可以删除两个项而不是一个)。为什么会这样?以下是我的一些代码:

public static void collapseAndDelete(final View v, final ArrayList<AlarmClock> alarmClockArrayList,
                                     final AlarmsArrayAdapter adapter, final int position) {
    final int initialHeight = v.getMeasuredHeight();
    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {

            if (interpolatedTime == 1) {
                alarmClockArrayList.remove(position);
                adapter.notifyDataSetChanged();
            } else {
                v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
                v.requestLayout();
                v.setAlpha(1.0f - interpolatedTime);
            }
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };
    a.setDuration(400);
    v.startAnimation(a);
}

最佳答案

实现动画侦听器以捕获最终回调

r.setAnimationListener(new OAnimationListener(this));

课程示例:
public class OAnimationListener implements AnimationListener{

    private MyView vv;


    public OAnimationListener(MyView vv) {
        // TODO Auto-generated constructor stub
        this.vv = vv;
    }

    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        if (vv != null)
            vv.stopAnim(2); //or any wanted callback

    }
}

不要忘记设置:
r.setRepeatCount(0);

                r.setFillAfter(true);

07-28 01:47