How to animate recyclerview on scroll like Google plus/Google newsstand?

前端 未结 4 1113
囚心锁ツ
囚心锁ツ 2020-12-04 10:12

How do I animate the RecyclerView when the items appear for first time and also when the user scrolls, just like the way it works for the google plus app or the

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-04 10:52

    Without any outside libraries in RecycleView.Adapter on method onBindViewHolder use animation like in example:

           if (position>lastAnimatedPosition) {
    
    
            //set init transitionY to animate from it
            holder.itemView.setTranslationY(holder.itemView.getHeight());
    
            //animate to orginal position
            holder.itemView.animate().translationYBy(-  holder.itemView.getHeight()).start();
    
    
            lastAnimatedPosition=position;
        }
    

    Above code will animate from bottom every row in list. Animation will be done only once, but onBindViewHolder is running on scrolling so first scrolling of list will be with animation effect.

    Very important is to initialise view to start of animation, so in example i set:

      holder.itemView.setTranslationY( + Y change);
    

    then animation go backs to orginal position:

     holder.itemView.animate().translationYBy(- Y change).start();
    

    If you need alpha do this that way:

     holder.itemView.setAlpha(0);
     holder.itemView.animate().apha(1).start();
    

提交回复
热议问题