[英]Recyclerview initial animation
There is a common recyclerview animation I see in some apps. 我在某些应用程序中看到一个常见的recyclerview动画。 When recyclerview is populated for the first time , its items slide in from bottom while also fading in at the same time. 首次填充recyclerview时,其项目从底部滑入,同时也逐渐消失。 How to achieve this ? 如何实现呢?
setItemAnimator()
is used for item changes/ new insertion/ deletion. setItemAnimator()
用于项目更改/新插入/删除。 It will not work at first time, if you are using setAdapter()
with items. 如果您将setAdapter()
与项目一起使用,则第一次将无法使用。
Try this inside the Recycler view adapter: 在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);
}
}
You can change the AnimatorSet and Interpolator as your need. 您可以根据需要更改AnimatorSet和Interpolator。
Like: 喜欢:
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();
Try this way to animate recycerview 尝试这种方式为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/ https://www.sitepoint.com/mastering-complex-lists-with-the-android-recyclerview/
http://www.birbit.com/recyclerview-animations-part-1-how-animations-work/ http://www.birbit.com/recyclerview-animations-part-1-how-animations-work/
https://www.studytutorial.in/android-recyclerview-with-animation-tutorial https://www.studytutorial.in/android-recyclerview-with-animation-tutorial
https://github.com/gabrielemariotti/RecyclerViewItemAnimators https://github.com/gabrielemariotti/RecyclerViewItemAnimators
https://github.com/couchbaselabs/mini-hacks/tree/master/android-recycler-view-animations https://github.com/couchbaselabs/mini-hacks/tree/master/android-recycler-view-animations
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.