Skip to content Skip to sidebar Skip to footer

How To Add Recyclerview Slide In Animation For New Item

I have a RecyclerView and add items to mCommentArrayList at index 0. I am trying to create a slide-in animation at the top of the view as new items (CardViews) are added to the Rec

Solution 1:

Using the library you were talking about (https://github.com/wasabeef/recyclerview-animators) it's very easy to add a SlideInAnimator to your RecyclerView. Just use the following code to set an Animator to your RecyclerView (pick one):

    recyclerView.setItemAnimator(newSlideInDownAnimator());
    recyclerView.setItemAnimator(newSlideInRightAnimator());
    recyclerView.setItemAnimator(newSlideInLeftAnimator());
    recyclerView.setItemAnimator(newSlideInUpAnimator());

Once you have done this the you can simply trigger the animation by calling notifyItemInserted(position) or notifyItemRangeInserted(positionStart, itemCount). These calls will trigger the Animator, calling notifyDatasetChanged()won't.

Triggering the insertion animation:

    recyclerView.getAdapter().notifyItemInserted(position);
    recyclerView.getAdapter().notifyItemRangeInserted(positionStart, itemCount);

Solution 2:

Hope this code help's you !

create one animation file animation_from_right.xml

<?xml version="1.0" encoding="utf-8"?><setxmlns:android="http://schemas.android.com/apk/res/android"android:duration="700"android:fillAfter="false"
><translateandroid:interpolator="@android:anim/decelerate_interpolator"android:fromXDelta="100%p"android:toXDelta="0"
/><alphaandroid:fromAlpha="0.5"android:toAlpha="1"android:interpolator="@android:anim/accelerate_decelerate_interpolator"
/></set>

in Activity

Animationanimation= AnimationUtils.loadAnimation(mActivity, R.anim.animation_from_right);
        holder.itemView.startAnimation(animation);

use above code in your Adapter on onBindViewHolder

Solution 3:

The best way animate the recyclerview will be to do it in the onbindviewholder method.

Here is how to do it-

create a field variable lastAnimatedPosition in the adapter class.

privateint lastAnimatedPosition = -1;

Then in the onbindviewholder-

@OverridepublicvoidonBindViewHolder(ViewHolder holder, int position) {
        Commentscomment= mDataSet.get(position);
        holder.userComment.setText(comment.getUserComment());
        holder.userID.setText("User " + position);
if (position > lastAnimatedPosition) {
      lastAnimatedPosition = position;
      Animationanimation= AnimationUtils.loadAnimation(context, R.anim.my_anim_set);
      animation.setInterpolator(newAccelerateDecelerateInterpolator());
      ((ViewHolder) holder).container.setAnimation(animation);
      animation.start();
    }
    }

Next a few tweaks in your viewholder class-

publicclassViewHolderextendsRecyclerView.ViewHolder {
        // each data item is just a string in this caseprotected ImageView userAvatar;
        protected TextView userID;
        protected TextView userComment;
        **protected View container;**


        publicViewHolder(View v) {
            super(v);
            **container = v;**
            userAvatar = (ImageView) v.findViewById(R.id.profile_image);
            userID = (TextView) v.findViewById(R.id.user_ID);
            userComment = (TextView) v.findViewById(R.id.user_comment_textview);
        }

        **publicvoidclearAnimation() {
          container.clearAnimation();
        }**
    }

And lastly simply override onViewDetachedFromWindow-

@OverridepublicvoidonViewDetachedFromWindow(final ViewHolder holder) {
    holder.clearAnimation();
  }

UPDATE

Since the element to be animated is in the 0th index replace the if (position > lastAnimatedPosition) snippet with -

if (position == 0) {
      lastAnimatedPosition = position;
      Animationanimation= AnimationUtils.loadAnimation(context, R.anim.my_anim_set);
      animation.setInterpolator(newAccelerateDecelerateInterpolator());
      ((ViewHolder) holder).container.setAnimation(animation);
      animation.start();
    }

Post a Comment for "How To Add Recyclerview Slide In Animation For New Item"