Last active
August 29, 2015 14:18
-
-
Save unosk/b9e20b071399f1269dbf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Rect; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
public class GridSpaceItemDecoration extends RecyclerView.ItemDecoration { | |
private final int mSpanCount; | |
private final int mSpace; | |
private final int[] mHorizontalSpaces; | |
public GridSpaceItemDecoration(int spanCount, int space) { | |
mSpanCount = spanCount; | |
mSpace = space; | |
mHorizontalSpaces = new int[mSpanCount]; | |
for (int i = 0; i < mSpanCount; i++) { | |
mHorizontalSpaces[i] = space * (i + 1) / mSpanCount; | |
} | |
} | |
@Override | |
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { | |
final int position = parent.getChildPosition(view); | |
final int spanIndex = position % mSpanCount; | |
outRect.left = mHorizontalSpaces[mSpanCount - spanIndex - 1]; | |
outRect.right = mHorizontalSpaces[spanIndex]; | |
outRect.bottom = mSpace; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment