我有一个ListView
,其项目需要通过动画折叠然后删除。我使用Animation
来折叠项目,在完成折叠后,我会删除ListView
中的项目(通过从数据列表中删除它并调用notifyDataSetChanged
)。要检测动画是否完成,请检查interpolatedTime
方法中的applyTransformation
== 1.0。问题是使用`interpolatedTime'== 1调用applyTransformation
两次,所以我不能真正依赖它(否则我可以删除两个项而不是一个)。为什么会这样?这是我的一些代码:
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);
}
答案 0 :(得分:1)
实现动画侦听器以捕获最终回调
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);