逐个动画RecyclerView项目

我正在尝试在我的应用中为我的RecyclerView项目制作动画。 我尝试在我的适配器中使用此代码:

  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); } 

但是,因为我的所有项目在创建视图时一起显示,而在用户单击按钮时不会添加,例如,所有项目一起动画。

我希望这些项目按照其位置顺序依次制作动画。
这怎么可能呢?

这个怎么样:

 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设置适配器之后

  List 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); } } } } 

这可能不是答案,但它可能有所帮助。 我认为你应该在你的动画之间设置一个延迟。

 new Handler().postDelayed(new Runnable() { @Override public void run() { // your code that you want to delay here } }, 1000/* 1000ms = 1sec delay */); 

您需要为动画添加延迟时间并增加每个项目绑定的延迟。

 int delayAnimate = 300; //global variable @Override public void onBindViewHolder(final MyViewHolder holder, int position) { //set view to INVISIBLE before animate holder.viewAnimate.setVisibility(View.INVISIBLE); setAnimation(holder.ll_root); } private void setAnimation(final View view) { Handler handler = new Handler(); handler.postDelayed(new Runnable() { public void run() { Animation animation = AnimationUtils.loadAnimation(mContext, android.R.anim.slide_in_left); if(view!=null){ view.startAnimation(animation); view.setVisibility(View.VISIBLE); } } }, delayAnimate); delayAnimate+=300; } 
 override fun onBindViewHolder(holder: ViewHolder, position: Int) { Handler().postDelayed({ // animate here }, DURATION * position) } 

DURATION *位置有助于逐个动画回收物品。
设置在50-75之间时,DURATION最佳。