I have a ListView
whose items need to be collapsed with an animation and then deleted. I use Animation
for collapsing items, and after it's done collapsing I delete the item from the ListView
(by deleting it from data list and calling notifyDataSetChanged
). To detect if animation is done I check if interpolatedTime
== 1.0 in applyTransformation
method. The problem is that applyTransformation
is called twice with `interpolatedTime' == 1, so I can't really rely on that (otherwise I can delete two items instead of just one). Why is this happening? Here's some of my code:
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);
}
Implement animation listener to catch final callback
r.setAnimationListener(new OAnimationListener(this));
example of the class:
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
}
}
do not forget to setup this:
r.setRepeatCount(0);
r.setFillAfter(true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With