Skip to content

Instantly share code, notes, and snippets.

@jaganjan
Created November 25, 2015 10:32
Show Gist options
  • Save jaganjan/da2098cd7bdd5c2ca1c0 to your computer and use it in GitHub Desktop.
Save jaganjan/da2098cd7bdd5c2ca1c0 to your computer and use it in GitHub Desktop.
Recyerview item spacing for staggeredgrid and gridlayout manager even spacing for all edges
public class GridSpacingDecoration : RecyclerView.ItemDecoration
{
/// <summary>
/// The m space
/// </summary>
private readonly int _mSpace;
/// <summary>
/// Initializes a new instance of the <see cref="GridSpacingDecoration"/> class.
/// </summary>
/// <param name="space">The space.</param>
public GridSpacingDecoration(int space)
{
_mSpace = space;
}
/// <summary>
/// Gets the item offsets.
/// </summary>
/// <param name="outRect">The out rect.</param>
/// <param name="view">The view.</param>
/// <param name="parent">The parent.</param>
/// <param name="state">The state.</param>
public override void GetItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
{
int spacing = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, _mSpace, view.Resources.DisplayMetrics);
int halfSpacing = spacing / 2;
int childCount = parent.ChildCount;
#pragma warning disable 618
int childIndex = parent.GetChildPosition(view);
#pragma warning restore 618
int spanCount = GetTotalSpan(view, parent);
int spanIndex = childIndex % spanCount;
/* INVALID SPAN */
if (spanCount < 1) return;
outRect.Top = halfSpacing;
outRect.Bottom = halfSpacing;
outRect.Left = halfSpacing;
outRect.Right = halfSpacing;
if (IsTopEdge(childIndex, spanCount))
{
outRect.Top = spacing;
}
if (IsLeftEdge(spanIndex, spanCount))
{
outRect.Left = spacing;
}
if (IsRightEdge(spanIndex, spanCount))
{
outRect.Right = spacing;
}
if (IsBottomEdge(childIndex, childCount, spanCount))
{
outRect.Bottom = spacing;
}
}
/// <summary>
/// Gets the total span.
/// </summary>
/// <param name="view">The view.</param>
/// <param name="parent">The parent.</param>
/// <returns>System.Int32.</returns>
protected int GetTotalSpan(View view, RecyclerView parent)
{
var mgr = parent.GetLayoutManager();
if (mgr.GetType() == typeof(GridLayoutManager))
{
return ((GridLayoutManager)mgr).SpanCount;
}
else if (mgr.GetType() == typeof(StaggeredGridLayoutManager))
{
return ((StaggeredGridLayoutManager)mgr).SpanCount;
}
return -1;
}
/// <summary>
/// Determines whether [is bottom edge] [the specified child index].
/// </summary>
/// <param name="childIndex">Index of the child.</param>
/// <param name="childCount">The child count.</param>
/// <param name="spanCount">The span count.</param>
/// <returns>
/// <c>true</c> if [is bottom edge] [the specified child index]; otherwise, <c>false</c>.
/// </returns>
protected bool IsBottomEdge(int childIndex, int childCount, int spanCount)
{
return childIndex >= childCount - spanCount;
}
/// <summary>
/// Determines whether [is left edge] [the specified span index].
/// </summary>
/// <param name="spanIndex">Index of the span.</param>
/// <param name="spanCount">The span count.</param>
/// <returns><c>true</c> if [is left edge] [the specified span index]; otherwise, <c>false</c>.</returns>
protected bool IsLeftEdge(int spanIndex, int spanCount)
{
return spanIndex == 0;
}
/// <summary>
/// Determines whether [is right edge] [the specified span index].
/// </summary>
/// <param name="spanIndex">Index of the span.</param>
/// <param name="spanCount">The span count.</param>
/// <returns><c>true</c> if [is right edge] [the specified span index]; otherwise, <c>false</c>.</returns>
protected bool IsRightEdge(int spanIndex, int spanCount)
{
return spanIndex == spanCount - 1;
}
/// <summary>
/// Determines whether [is top edge] [the specified child index].
/// </summary>
/// <param name="childIndex">Index of the child.</param>
/// <param name="spanCount">The span count.</param>
/// <returns><c>true</c> if [is top edge] [the specified child index]; otherwise, <c>false</c>.</returns>
protected bool IsTopEdge(int childIndex, int spanCount)
{
return childIndex < spanCount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment