Expand ListView item with animation

后端 未结 5 1424
孤街浪徒
孤街浪徒 2020-12-04 09:35

I have a ListView. Initially, the ListView contains some data. When the user clicks on an item, another layout will be dynamically added to that it

5条回答
  •  感动是毒
    2020-12-04 09:54

    Using value animator the solution looks nice:

    ValueAnimator animator = ValueAnimator.ofInt(100, 300);
    animator.setDuration(1000);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        listViewItem.getLayoutParams().height = (Integer) animation.getAnimatedValue();
        listViewItem.requestLayout();
      }
    });
    animator.start();
    

    Just read the android developer guide, it is worth reading: http://developer.android.com/guide/topics/graphics/prop-animation.html

    But keep in mind, that requestLayout() processing is heavy. Because one call of requestLayout() make every nearby element, which is visually affected, recalculate it's layout. It may be better to use negative bottom margin (to hide some part of your element under another one) and use the following to show it:

    listViewItem.setTranslationY((Integer) animation.getAnimatedValue());
    

    Of course, you may animate only bottom margin, like proposed in another answer to this question.

提交回复
热议问题