0

Here's the situation:

I have a LinearLayout containing vertically n CardView.

I want to customise the way it's rendered by adding animation on cards.

When the layout is rendered (maybe in onResume()) i want each card to slide from the bottom to the position the card should have taken if not animated, like in the illustration above: enter image description here The yellow arrow represent the LinearLayout's paddingTop and the purple arrow represents the CardView's marginBottom.

I don't know how to achieve this because i can't get the final position, that's why i came here to ask you if someone has already done this or similar.

I tried to use LayoutTransition with ObjectAnimator added to the LinearLayout that animate children when added/removed, but i have to pass a view (the child, that i'm not suppose to know at this time) in ObjectAnimator's constructor..

I also tried this:

[...]
int deviceHeight = getResources().getDisplayMetrics().heightPixels;
[...]
@override
public onResume() {
  super.onResume();
  card.animate().y(position)
      .withStartAction(new Runnable(){
        @Override
          public void run(){
            card.setY(deviceHeight);
          }
        });

but i can't get the value of position (i always get 0) or if i change it to .y(-deviceHeight) the card is rendered at his position then moved to bottom before it slides to the top of LinearLayout but the paddingTop is not even considered..

EDIT: To sum up, i want to make something similar to what happens when android:animateLayoutChanges="true" is set and you remove the first child: the other children just slide up to fill the place remained empty (except that i want to add some delay between each child animation)

Mino
  • 264
  • 3
  • 18

1 Answers1

1

Finally i made a little trick to achieve this:

Assuming the LinearLayout is called cardsContainer, i did this:

LayoutTransition lt = cardsContainer.getLayoutTransition();
lt.setStagger(LayoutTransition.CHANGE_DISAPPEARING, 200);

This piece of code adds 200ms to every animation of each child that moves because of of them has been removed, so i had an empty View (ghostcard) on top that match_parent then i do this:

@Override
public void onWindowFocusChanged (boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if(hasFocus) {
        cardsContainer.removeView(findViewById(R.id.ghostcard));
    } else {
        cardsContainer.addView(ghostCard, 0);
    }
}
Mino
  • 264
  • 3
  • 18