title: How do I use a GridLayoutAnimation in a RecyclerView?
body: <p>I'm trying to replace my GridView with the new RecyclerView (using GridLayoutManager) but it seems like it doesn't cope well with gridLayoutAnimation (<code>ClassCastException: LayoutAnimationController$AnimationParameters cannot be cast to GridLayoutAnimationController$AnimationParameters</code>). It works with a regular layout animation, but because it's a grid, it takes too long to complete on tablets.</p>
  
  <p>What I'm trying to accomplish is similar to <a href="http://www.google.com/design/spec/animation/meaningful-transitions.html#meaningful-transitions-hierarchical-timing">Hierarchical Timing</a>. If you look at the example video, it shows the layout animation go from top-left to down-right diagonally. A regular layout animation would execute the animation row after row, hence taking too much time to complete on bigger grids (e.g. tablets). I've also tried exploring ItemAnimator, but that would only run the animation on all cells simultaneously like it does in the "Don't" example.</p>
  
  <p>Is there a way to accomplish this grid layout animation in a RecyclerView?</p>
  
  <p><strong>This is the gridview_layout_animation.xml:</strong></p>
  
  <pre><code>&lt;!-- replace gridLayoutAnimation with layoutAnimation and --&gt;
  &lt;!-- replace column- and rowDelay with delay for RecyclerView --&gt;
  
  &lt;gridLayoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
      android:columnDelay="15%"
      android:rowDelay="15%"
      android:animation="@anim/grow_in"
      android:animationOrder="normal"
      android:direction="top_to_bottom|left_to_right"
      android:interpolator="@android:interpolator/linear"
  /&gt;
  </code></pre>
  
  <p><strong>And this is the animation grow_in.xml:</strong></p>
  
  <pre><code>&lt;set android:shareInterpolator="false"
   xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;scale
          android:interpolator="@android:interpolator/decelerate_quint"
          android:fromXScale="0.0"
          android:toXScale="1.0"
          android:fromYScale="0.0"
          android:toYScale="1.0"
          android:pivotX="50%"
          android:pivotY="50%"
          android:fillAfter="true"
          android:duration="400"
          android:startOffset="200"
      /&gt;
  &lt;/set&gt;
  </code></pre>
  
  <p><strong>EDIT:</strong> Based on Galaxas0's answer, <a href="https://gist.github.com/Musenkishi/8df1ab549857756098ba">here</a> is a solution which only requires you to use a custom view that extends <code>RecyclerView</code>. Basically only overriding the <code>attachLayoutAnimationParameters()</code> method. With this <code>&lt;gridLayoutAnimation&gt;</code> works as it did with GridView.</p>
  
  <pre><code>public class GridRecyclerView extends RecyclerView {
  
      public GridRecyclerView(Context context) {
          super(context);
      }
  
      public GridRecyclerView(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
  
      public GridRecyclerView(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
      }
  
      @Override
      public void setLayoutManager(LayoutManager layout) {
          if (layout instanceof GridLayoutManager){
              super.setLayoutManager(layout);
          } else {
              throw new ClassCastException("You should only use a GridLayoutManager with GridRecyclerView.");
              }
          }
  
      @Override
      protected void attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params, int index, int count) {
  
          if (getAdapter() != null &amp;&amp; getLayoutManager() instanceof GridLayoutManager){
  
              GridLayoutAnimationController.AnimationParameters animationParams =
                  (GridLayoutAnimationController.AnimationParameters) params.layoutAnimationParameters;
  
              if (animationParams == null) {
                  animationParams = new GridLayoutAnimationController.AnimationParameters();
                  params.layoutAnimationParameters = animationParams;
              }
  
              int columns = ((GridLayoutManager) getLayoutManager()).getSpanCount();
  
              animationParams.count = count;
              animationParams.index = index;
              animationParams.columnsCount = columns;
              animationParams.rowsCount = count / columns;
  
              final int invertedIndex = count - 1 - index;
              animationParams.column = columns - 1 - (invertedIndex % columns);
              animationParams.row = animationParams.rowsCount - 1 - invertedIndex / columns;
  
          } else {
              super.attachLayoutAnimationParameters(child, params, index, count);
          }
      }
  }
  </code></pre>