1

I am trying to take a CardView (in a RecycleView) from actual width to 0. I am a newbie in android and in android animations. Could you say me what I am doing wrong?

This is my code:

final View v = cardViewHolder.cv;
int actualWidth = v.getMeasuredWidth();
ValueAnimator anim = ValueAnimator.ofInt(actualWidth, 0);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
     public void onAnimationUpdate(ValueAnimator valueAnimator) {
          int val = (Integer) valueAnimator.getAnimatedValue();
          ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
          layoutParams.width = val;
          v.setLayoutParams(layoutParams);
    }
});
anim.setDuration(R.integer.card_flip_time_full);
anim.start();

With a breakpoint I see that val is always 728 (the width) and never changes. What is my fail?

Javier Torron Diaz
  • 347
  • 1
  • 4
  • 25
  • call v.requestLayout() in each update. See this https://stackoverflow.com/questions/13856180/usage-of-forcelayout-requestlayout-and-invalidate to understand all view invalidation calls better – mhenryk Jan 10 '18 at 11:16
  • It is exactly the same. Just checking... did you mean write `v.requestLayout();` inside `public void onAnimationUpdate(ValueAnimator valueAnimator)` ? I did it with the same result. – Javier Torron Diaz Jan 10 '18 at 11:46

1 Answers1

0

I solved it:

final View v = cardViewHolder.cv;
int actualWidth = v.getMeasuredWidth();
ValueAnimator anim = ValueAnimator.ofInt(actualWidth, 0);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
     public void onAnimationUpdate(ValueAnimator valueAnimator) {
          // This needs to be declared as Integer instead of int
          v.requestLayout(); // @mhenryk advice
          Integer val = (Integer) valueAnimator.getAnimatedValue();  
          int value = val.intValue();  // Then get the int value
          ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
          layoutParams.width = value;
          v.setLayoutParams(layoutParams);
    }
});
anim.setDuration(R.integer.card_flip_time_full);
anim.start();
Javier Torron Diaz
  • 347
  • 1
  • 4
  • 25