对Recycler进行动画处理逐一查看项目 [英] Animate RecyclerView items one by one
问题描述
我正在为我的应用程序中的RecyclerView
项目设置动画.我尝试在适配器中使用此代码:
I'm trying to animate my RecyclerView
items in my app. I tried using this code in my adapter:
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
myHolder = holder as MyView;
if (position > mCurrentPosition)
{
int currentAnim = Android.Resource.Animation.SlideInLeft;
SetAnimation(holder.ItemView, currentAnim);
mCurrentPosition = position;
}
}
private void SetAnimation(View view, int currentAnim)
{
Animation anim = AnimationUtils.LoadAnimation(mContext, currentAnim);
anim.SetInterpolator(mContext, Android.Resource.Interpolator.Bounce);
view.StartAnimation(anim);
}
但是,因为创建视图时我的所有项目都一起显示,而在用户单击按钮时没有添加,所以所有项目都一起动画.
However, because all of my items appear together when the view is created, and not added when the user clicks a button, for example, all of the items animate together.
我希望这些项目按照它们的位置顺序一个接一个地动画.
这怎么可能?
I want the items to animate one after the other, in the order of their position.
How is this possible to do?
推荐答案
这是怎么回事:
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
myHolder = holder as MyView;
if (position > mCurrentPosition)
{
Handler h = new Handler();
int currentAnim = Android.Resource.Animation.SlideInLeft;
Action myAction = () =>
{
SetAnimation(holder.ItemView, currentAnim);
};
h.PostDelayed(myAction, 1000);
mCurrentPosition = position;
}
}
更新:如果要对所有项目进行动画处理,则应将逻辑代码放在适配器之外.
例如,在活动中,为RecyleView设置适配器后
Update: if you want to animate all items in order, you should put logic code out side an adapter.
For example, in activity, after set adapter for RecyleView
List<YourObject> data; // your data list for RecycleView
// CALL this code after set adapter for RecycleView
Handler h = new Handler();
int currentAnim = Android.Resource.Animation.SlideInLeft;
Action myAction = () =>
{
// assume you use LinearLayoutManager for RecyecleView
View itemView = linearLayoutManager.findViewByPosition(0);
StartAnimation(itemView, 0);
};
h.PostDelayed(myAction, 1000);
private void StartAnimation(View view, int position)
{
Animation anim = AnimationUtils.LoadAnimation(mContext, currentAnim);
anim.SetInterpolator(mContext, Android.Resource.Interpolator.Bounce);
view.StartAnimation(anim);
anim.AnimationEnd += (sender, e) =>
{
// animate next item
++position;
if (position < data.size()) // data is your array list
{
// assume you use LinearLayoutManager for RecyecleView
View itemView = linearLayoutManager.findViewByPosition(position);
if (itemView != null)
{
StartAnimation(itemView, position);
}
}
}
}
这篇关于对Recycler进行动画处理逐一查看项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!