Skip to content

Instantly share code, notes, and snippets.

@patrick-elmquist
Last active September 8, 2020 08:04
Show Gist options
  • Save patrick-elmquist/e28cedb927acfca8a6a4778ef1855964 to your computer and use it in GitHub Desktop.
Save patrick-elmquist/e28cedb927acfca8a6a4778ef1855964 to your computer and use it in GitHub Desktop.
Enter animation demo: GridRecyclerView implementation
/**
* RecyclerView with support for grid animations.
*
* Based on:
* https://gist.github.com/Musenkishi/8df1ab549857756098ba
* Credit to Freddie (Musenkishi) Lust-Hed
*
* ...which in turn is based on the GridView implementation of attachLayoutParameters(...):
* https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/GridView.java
*
*/
public class GridRecyclerView extends RecyclerView {
/** @see View#View(Context) */
public GridRecyclerView(Context context) { super(context); }
/** @see View#View(Context, AttributeSet) */
public GridRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); }
/** @see View#View(Context, AttributeSet, int) */
public GridRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }
@Override
protected void attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params,
int index, int count) {
final LayoutManager layoutManager = getLayoutManager();
if (getAdapter() != null && layoutManager instanceof GridLayoutManager){
GridLayoutAnimationController.AnimationParameters animationParams =
(GridLayoutAnimationController.AnimationParameters) params.layoutAnimationParameters;
if (animationParams == null) {
// If there are no animation parameters, create new once and attach them to
// the LayoutParams.
animationParams = new GridLayoutAnimationController.AnimationParameters();
params.layoutAnimationParameters = animationParams;
}
// Next we are updating the parameters
// Set the number of items in the RecyclerView and the index of this item
animationParams.count = count;
animationParams.index = index;
// Calculate the number of columns and rows in the grid
final int columns = ((GridLayoutManager) layoutManager).getSpanCount();
animationParams.columnsCount = columns;
animationParams.rowsCount = count / columns;
// Calculate the column/row position in the grid
final int invertedIndex = count - 1 - index;
animationParams.column = columns - 1 - (invertedIndex % columns);
animationParams.row = animationParams.rowsCount - 1 - invertedIndex / columns;
} else {
// Proceed as normal if using another type of LayoutManager
super.attachLayoutAnimationParameters(child, params, index, count);
}
}
}
@seyoung-hyun
Copy link

seyoung-hyun commented Sep 8, 2020

@patrick-elmquist
Thanks your code :)
Can I use your code in my commercial app? (The based code of Freddie (Musenkishi) Lust-Hed is under Apache 2.0.)
Please let me know how I can use the code for commercial distribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment