Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
GridRecyclerView. A Grid-specific RecyclerView that can use gridLayoutAnimation.
* Copyright (C) 2014 Freddie (Musenkishi) Lust-Hed
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* See the License for the specific language governing permissions and
* limitations under the License.
package com.musenkishi.gists.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.GridLayoutAnimationController;
* An extension of RecyclerView, focused more on resembling a GridView.
* Unlike {@link}, this view can handle
* {@code <gridLayoutAnimation>} as long as you provide it a
* {@link} in
* {@code setLayoutManager(LayoutManager layout)}.
* Created by Freddie (Musenkishi) Lust-Hed.
public class GridRecyclerView extends RecyclerView {
public GridRecyclerView(Context context) {
public GridRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
public GridRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
public void setLayoutManager(LayoutManager layout) {
if (layout instanceof GridLayoutManager){
} else {
throw new ClassCastException("You should only use a GridLayoutManager with GridRecyclerView.");
protected void attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params, int index, int count) {
if (getAdapter() != null && 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);

This comment has been minimized.

Copy link

@dominicthomas dominicthomas commented Nov 18, 2014

This looks great, thanks! In my implementation attachLayoutAnimationParameters is never called. Any ideas why?


This comment has been minimized.

Copy link
Owner Author

@Musenkishi Musenkishi commented Dec 1, 2014

No clue. Are you calling recyclerView.scheduleLayoutAnimation(); after setting the adapter? And have you set android:layoutAnimation="@anim/your_layout_animation" to your in the layout?


This comment has been minimized.

Copy link

@MarsVard MarsVard commented Mar 9, 2015

I had the same issue as @dominicthomas setting layout animation xml fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.