Last active
May 23, 2016 15:31
-
-
Save raviteja83/df7dbee3a68c7775e79e384787ebd9b6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.Canvas; | |
import android.graphics.PorterDuff; | |
import android.graphics.Rect; | |
import android.graphics.drawable.Drawable; | |
import android.support.annotation.ColorRes; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
public class GridDividerDecoration extends RecyclerView.ItemDecoration{ | |
private static final int[] ATTRS = { android.R.attr.listDivider }; | |
private Drawable mDivider; | |
private int mInsets; | |
public GridDividerDecoration(Context context,@ColorRes int color,int insets) { | |
TypedArray a = context.obtainStyledAttributes(ATTRS); | |
mDivider = a.getDrawable(0); | |
a.recycle(); | |
mDivider.setColorFilter(ContextCompat.getColor(context, color), PorterDuff.Mode.SRC_OVER); | |
mInsets = insets; | |
} | |
@Override | |
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { | |
drawVertical(c, parent); | |
drawHorizontal(c, parent); | |
} | |
/** Draw dividers at each expected grid interval */ | |
public void drawVertical(Canvas c, RecyclerView parent) { | |
if (parent.getChildCount() == 0) return; | |
final int childCount = parent.getChildCount(); | |
for (int i = 0; i < childCount; i++) { | |
final View child = parent.getChildAt(i); | |
final RecyclerView.LayoutParams params = | |
(RecyclerView.LayoutParams) child.getLayoutParams(); | |
final int left = child.getLeft() - params.leftMargin - mInsets; | |
final int right = child.getRight() + params.rightMargin + mInsets; | |
final int top = child.getBottom() + params.bottomMargin + mInsets; | |
final int bottom = top + mDivider.getIntrinsicHeight(); | |
mDivider.setBounds(left, top, right, bottom); | |
mDivider.draw(c); | |
} | |
} | |
/** Draw dividers to the right of each child view */ | |
public void drawHorizontal(Canvas c, RecyclerView parent) { | |
final int childCount = parent.getChildCount(); | |
for (int i = 0; i < childCount; i++) { | |
final View child = parent.getChildAt(i); | |
final RecyclerView.LayoutParams params = | |
(RecyclerView.LayoutParams) child.getLayoutParams(); | |
final int left = child.getRight() + params.rightMargin + mInsets; | |
final int right = left + mDivider.getIntrinsicWidth(); | |
final int top = child.getTop() - params.topMargin - mInsets; | |
final int bottom = child.getBottom() + params.bottomMargin + mInsets; | |
mDivider.setBounds(left, top, right, bottom); | |
mDivider.draw(c); | |
} | |
} | |
@Override | |
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { | |
//We can supply forced insets for each item view here in the Rect | |
outRect.set(mInsets, mInsets, mInsets, mInsets); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment