gpt4 book ai didi

android - RecyclerView 项目动画 : Only animate initially visible items

转载 作者:搜寻专家 更新时间:2023-11-01 09:36:34 27 4
gpt4 key购买 nike

我有以下代码来为 RecyclerView 项目设置动画。这非常有效,但如果我向下滚动,它会为所有出现的项目设置动画。

我想要的是一种类似于 Google Play 音乐应用的行为,其中仅针对最初可见 的项目播放动画。通过滚动变得可见的项目应该出现,因为没有动画适配器。知道如何存档此行为吗?

  public class RecyclerViewAnimationAdapter : RecyclerView.Adapter
{
private int m_LastPosition = -1;

public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
base.OnBindViewHolder(holder, position);

animateItem(position, holder.ItemView);
}

public override void OnViewDetachedFromWindow(Object holder)
{
base.OnViewDetachedFromWindow(holder);

var viewToAnimate = ((RecyclerView.ViewHolder) holder).ItemView;

viewToAnimate.ClearAnimation();
}

private void animateItem(int position, View viewToAnimate)
{
if (position > m_LastPosition)
{
var animation = AnimationUtils.LoadAnimation(Application.Context, Resource.Animation.slide_in_bottom);

viewToAnimate.StartAnimation(animation);

m_LastPosition = position;
}
}
}

最佳答案

我在 Android Studio 中测试了这个方法并且它有效,通过一些小的调整它应该也适用于你:

class YourAdapter extends RecyclerView.Adapter{
private int lastPosition = -1;
private boolean isAnimation;

YourAdapter(... )
{
...
this.isAnimation = true;
}
....


private void switchAnimation(boolean isAnimation)
{
this.isAnimation = isAnimation;
}


private void animateItem(int position, View viewToAnimate)
{
if (position > m_LastPosition && isAnimation)
{
var animation = AnimationUtils.LoadAnimation(Application.Context, Resource.Animation.slide_in_bottom);

viewToAnimate.StartAnimation(animation);

lastPosition = position;
}
}

然后在您的 LayoutManager 中覆盖方法 onLayoutChildren(您必须将代码适应 C#):

layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false){
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
super.onLayoutChildren(recycler, state);
myAdapter.switchAnimation(false);
}
};

如果有帮助请告诉我 ;)

关于android - RecyclerView 项目动画 : Only animate initially visible items,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42679757/

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