gpt4 book ai didi

java - RecyclerView 中的属性动画

转载 作者:行者123 更新时间:2023-11-29 23:17:18 25 4
gpt4 key购买 nike

我想使用 PropertyAnimationRecyclerView 中创建一个动画。我需要隐藏我的每一行,还需要从下到上移动。

在适配器内部,我创建了一个在 OnBindViewHolder

中运行的例程
private void setAnimation(View viewToAnimate, int position) {
if (position > lastPosition) {
viewToAnimate.Animate()
.TranslationYBy(500)
.TranslationY(0)
.AlphaBy(0)
.Alpha(1)
.SetDuration(2000)
.SetStartDelay(5);

lastPosition = position;
}
}

我的行没有移动,也没有从不可见变为可见。它们是完全静态的。我知道它是 xamarin 代码,但我也标记为 java,因为它是我的模拟代码。

最佳答案

你想要这样的效果吗:

在适配器的 OnBindViewHolder 中:

private int lastAnimatedPosition = -1;

public override void OnBindViewHolder(RecyclerView.ViewHolder viewHolder, int position)
{
RunEnterAnimation(viewHolder.ItemView, position);
}

RunEnterAnimation 方法:

private void RunEnterAnimation(View view, int position)
{
if (position > lastAnimatedPosition)
{
lastAnimatedPosition = position;
ObjectAnimator animator1 = ObjectAnimator.OfFloat(view, "TranslationY", -500, 0);
animator1.SetDuration(2000);
ObjectAnimator animator2 = ObjectAnimator.OfFloat(view, "alpha", 0, 1);
animator2.SetDuration(2000);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.PlayTogether(animator1, animator2);
animatorSet.StartDelay=5;
animatorSet.Start();
}
}

ps:如果你不想使用xml你可以使用上面的方法,你也可以使用LayoutAnimationResources/anim/xx.xml ,也可以通过ItemAnimator

实现

关于java - RecyclerView 中的属性动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55109957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com