CardView within RecyclerView: animation while populating

I have a recycled view which is populated with CardView layouts. I’ve noticed that in many apps, these items have a nice animated entrance rather than abruptly appearing as they currently do in my application. An example of what I’m trying to achieve is shown below:
enter image description here

How would I use my swipeRefreshListener/adapter to trigger this animation? How would I implement this through my adapter?

  • How to Make my Android App FullScreen via Android Manifest?
  • android java implementation flaws .. are they documented?
  • How to check if resource pointed by Uri is available?
  • Record video with a different preview size than the resulting video file
  • Toolbar and Contextual ActionBar with AppCompat-v7
  • programmatically execute Touch event in android
  • Note: I need this animation to also be triggered when the RecyclerView is first populated with the CardView items, and the data they contain.

    My RecyclerView’s adapter is shown below:

    public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    private String[] mDataset;
    private String[] mClassDataset;
    private int lastPosition = -1;
    private Context context;
    View view;
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public TextView mClassDataView;
        CardView container;
        public ViewHolder(View itemView) {
            mTextView = (TextView)itemView.findViewById(;
            mClassDataView = (TextView)itemView.findViewById(;
            container = (CardView)itemView.findViewById(;
    public RecyclerAdapter(String[] myDataset, String[] classData, Context context) {
        mDataset = myDataset;
        mClassDataset = classData;
        this.context = context;
    public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_item, parent, false);
        ViewHolder vh = new ViewHolder(v);
        view = v;
        return vh;
    public int getItemCount() {
        return mClassDataset.length;
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if (mDataset.length < mClassDataset.length) {
            if (mClassDataset[position].equals("N/A")) {
        } else {
        for (int i = 0; i < getItemCount(); i++) {
            animate(view, i);
    private void animate(final View view, final int position){
            Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
            lastPosition = position;

    The animation is currently working, however the thing is the items are being animated all at once. I tried iterating through the items by creating a custom getChild() method, but that didn’t work. Somehow I need to access/animate each child individually and possibly set a delay between each animation. However, I’m not sure how exactly I can accomplish this. I know I can use a Handler to set delay, but accessing each child is a problem.

    Related posts:

    Android - Change left margin using animation
    HTML5 canvas issues on Android 4.1.1
    Fragment replacement triggers onQueryTextChange on searchview
    Can an Android device act as an iBeacon?
    Black flickering on Honeycomb WebView
    Android: custom separator (or even item) in ListView depening on content of item
  • Attempt to invoke virtual method 'int android.text.Layout.getLineCount()' on a null object reference
  • Publishing Android Library (aar) to Bintray with chosen flavors
  • Android: Bitmap resizing using better resampling algorithm than bilinear (like Lanczos3)
  • Drawable as Background for CardView
  • how to make animation for popup window in android
  • How to get EditText value and display it on screen through TextView?
  • 3 Solutions collect form web for “CardView within RecyclerView: animation while populating”

    Use this custom class of RecyclerView, it behaves like the app that you posted its screenshot (Reddit Relay), it prevents scrolling while animating first items.
    You may want to custom the animation as you want.

    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;
     * @author Leo on 2015/09/03
    public class MyRecyclerView extends RecyclerView {
        private boolean mScrollable;
        public MyRecyclerView(Context context) {
            this(context, null);
        public MyRecyclerView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        public MyRecyclerView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            mScrollable = false;
        public boolean dispatchTouchEvent(MotionEvent ev) {
            return !mScrollable || super.dispatchTouchEvent(ev);
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            super.onLayout(changed, l, t, r, b);
            for (int i = 0; i < getChildCount(); i++) {
                animate(getChildAt(i), i);
                if (i == getChildCount() - 1) {
                    getHandler().postDelayed(new Runnable() {
                        public void run() {
                            mScrollable = true;
                    }, i * 100);
        private void animate(View view, final int pos) {
            view.animate().alpha(1.0f).translationY(0).setDuration(300).setStartDelay(pos * 100);



    For the RecyclerView you can use ItemAnimators by setting them with this method:

    RecyclerView.setItemAnimator(RecyclerView.ItemAnimator animator)

    There are a few libraries out there that implement this ItemAnimator, for example this one:

    First define the animation:

    <set xmlns:android="">
    <translate android:fromXDelta="100%p" android:toXDelta="0"
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
        android:duration="300" />

    Then set it in your adapter:

    public void onBindViewHolder(ViewHolder holder, int position)
        // Here you apply the animation when the view is bound
        setAnimation(holder.container, position);
     * Here is the key method to apply the animation
    private void setAnimation(View viewToAnimate, int position)
        // If the bound view wasn't previously displayed on screen, it's animated
        if (position > lastPosition)
     Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.push_left_in);
            lastPosition = position;

    Referenced from: RecyclerView : How to create insert animation effect?

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.