2017-01-24 8 views
0

В некоторых приложениях есть общая анимация recyclerview. Когда recyclerview заселяется в первый раз, его элементы скользят снизу, одновременно затухая одновременно. Как достичь этого?Начальная анимация Recyclerview

+0

Попробуйте: https://github.com/wasabeef/recyclerview-animators –

+0

проверить это: https://antonioleiva.com/layout-animations-on-recyclerview/ – cuoka

ответ

1

setItemAnimator() Используется для изменения деталей/новый ввод/удаление. Он не будет работать в первый раз, если вы используете setAdapter() с элементами.

Попробуйте внутри вида адаптера Recycler:

int lastPosition = -1; 
@Override 
public void onViewAttachedToWindow(final ViewHolder holder) { 
      super.onViewAttachedToWindow(holder); 
      final long delayTime = 200; 
      holder.card.setVisibility(View.INVISIBLE); 

      if (holder.getPosition() > lastPosition) { 
       holder.card.getHandler().postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         holder.card.setVisibility(View.VISIBLE); 
         ObjectAnimator alpha = ObjectAnimator.ofFloat(holder.card, "alpha", 0f, 1f); 
         ObjectAnimator scaleY = ObjectAnimator.ofFloat(holder.card, "scaleY", 0f, 1f); 
         ObjectAnimator scaleX = ObjectAnimator.ofFloat(holder.card, "scaleX", 0f, 1f); 
         AnimatorSet animSet = new AnimatorSet(); 
         animSet.play(alpha).with(scaleY).with(scaleX); 
         animSet.setInterpolator(new OvershootInterpolator()); 
         animSet.setDuration(400); 
         animSet.start(); 

        } 
       }, delayTime); 

       lastPosition = holder.getPosition(); 
      } else { 
       holder.card.setVisibility(View.VISIBLE); 
      } 
     } 

Вы можете изменить AnimatorSet и интерполятора как ваши потребности.

Как:

PropertyValuesHolder translateX = PropertyValuesHolder.ofFloat(View.TRANSLATION_X, 0, 0); 
        PropertyValuesHolder translateY = PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, 100, 0); 
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(holder.itemView, translateX, translateY); 
        AnimatorSet animSet = new AnimatorSet(); 
        animSet.play(animator); 
        animSet.setInterpolator(new AccelerateDecelerateInterpolator()); 
        animSet.setDuration(400); 
        animSet.start(); 
0

Попробуйте этот путь, чтобы оживить recycerview

RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator(); 
itemAnimator.setAddDuration(1000); 
itemAnimator.setRemoveDuration(1000); 
recyclerView.setItemAnimator(itemAnimator); 

https://www.sitepoint.com/mastering-complex-lists-with-the-android-recyclerview/

http://www.birbit.com/recyclerview-animations-part-1-how-animations-work/

https://www.studytutorial.in/android-recyclerview-with-animation-tutorial

https://github.com/gabrielemariotti/RecyclerViewItemAnimators

https://github.com/couchbaselabs/mini-hacks/tree/master/android-recycler-view-animations

Смежные вопросы