Animating RecyclerView Elements One at a Time

I am trying to animate my RecyclerView elements in my application. 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, since all my elements appear together when creating the view and are not added when the user clicks the button, for example, all elements are waiting together.

I want the elements to be animated one after another, in the order of their position.
How can I do that?

+3
source share
4 answers

How about this:

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

Update : if you want to animate all the elements in order, you must put the logic code on the adapter side.
For example, in action after the adapter for RecyleView is installed

  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); } } } } 
+2
source

This may not be the answer, but it may help. I think you should set the delay between animations.

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

You need to add a delay time to the animation and increase the snap delay of each element.

 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; } 
0
source
 override fun onBindViewHolder(holder: ViewHolder, position: Int) { Handler().postDelayed({ // animate here }, DURATION * position) } 
Position

DURATION * helps animate recyclerview elements one by one.
DURATION is better if set between 50-75.

0
source

All Articles