Created
March 6, 2017 07:36
-
-
Save 410063005/3f4cc88f5dd7e9bb1e9f86951176a2e8 to your computer and use it in GitHub Desktop.
给RecyclerView的item添加边框
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
public class BorderItemDecoration extends RecyclerView.ItemDecoration { | |
public static final int HORIZONTAL = LinearLayout.HORIZONTAL; | |
public static final int VERTICAL = LinearLayout.VERTICAL; | |
private static final int[] ATTRS = new int[]{ android.R.attr.listDivider }; | |
private Drawable mDivider; | |
private final Rect mBounds = new Rect(); | |
/** | |
* Creates a divider {@link RecyclerView.ItemDecoration} that can be used with a | |
* {@link LinearLayoutManager}. | |
* | |
* @param context Current context, it will be used to access resources. | |
* @param orientation Divider orientation. Should be {@link #HORIZONTAL} or {@link #VERTICAL}. | |
*/ | |
public BorderItemDecoration(Context context, int orientation) { | |
final TypedArray a = context.obtainStyledAttributes(ATTRS); | |
mDivider = a.getDrawable(0); | |
a.recycle(); | |
} | |
/** | |
* Sets the {@link Drawable} for this divider. | |
* | |
* @param drawable Drawable that should be used as a divider. | |
*/ | |
public void setDrawable(@NonNull Drawable drawable) { | |
if (drawable == null) { | |
throw new IllegalArgumentException("Drawable cannot be null."); | |
} | |
mDivider = drawable; | |
} | |
@Override | |
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { | |
if (parent.getLayoutManager() == null) { | |
return; | |
} | |
drawVertical(c, parent); | |
drawHorizontal_(c, parent); | |
} | |
@SuppressLint("NewApi") | |
private void drawVertical(Canvas canvas, RecyclerView parent) { | |
canvas.save(); | |
final int left; | |
final int right; | |
if (parent.getClipToPadding()) { | |
left = parent.getPaddingLeft(); | |
right = parent.getWidth() - parent.getPaddingRight(); | |
canvas.clipRect(left, parent.getPaddingTop(), right, | |
parent.getHeight() - parent.getPaddingBottom()); | |
} else { | |
left = 0; | |
right = parent.getWidth(); | |
} | |
final int childCount = parent.getChildCount(); | |
for (int i = 0; i < childCount; i++) { | |
final View child = parent.getChildAt(i); | |
parent.getDecoratedBoundsWithMargins(child, mBounds); | |
final int bottom = mBounds.bottom + Math.round(ViewCompat.getTranslationY(child)); | |
final int top = bottom - mDivider.getIntrinsicHeight(); | |
mDivider.setBounds(left, top, right, bottom); | |
mDivider.draw(canvas); | |
// if (i == 0) { | |
// int tmpBottom = mBounds.top + Math.round(ViewCompat.getTranslationY(child)); | |
// int tmpTop = tmpBottom - mDivider.getIntrinsicHeight(); | |
// mDivider.setBounds(left, tmpTop, right, tmpBottom); | |
// mDivider.draw(canvas); | |
// } | |
} | |
canvas.restore(); | |
} | |
@SuppressLint("NewApi") | |
private void drawHorizontal_(Canvas canvas, RecyclerView parent) { | |
canvas.save(); | |
final int childCount = parent.getChildCount(); | |
for (int i = 0; i < childCount; i++) { | |
final View child = parent.getChildAt(i); | |
parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds); | |
final int right = mBounds.right + Math.round(ViewCompat.getTranslationX(child)); | |
final int left = right - mDivider.getIntrinsicWidth(); | |
int tmpBottom = mBounds.bottom + Math.round(ViewCompat.getTranslationY(child)); | |
int tmpTop = mBounds.top + Math.round(ViewCompat.getTranslationY(child)); | |
mDivider.setBounds(left, tmpTop, right, tmpBottom); | |
mDivider.draw(canvas); | |
// ADD | |
int tmpLeft = mBounds.left + Math.round(ViewCompat.getTranslationX(child)); | |
int tmpRight = tmpLeft + mDivider.getIntrinsicWidth(); | |
mDivider.setBounds(tmpLeft, tmpTop, tmpRight, tmpBottom); | |
mDivider.draw(canvas); | |
} | |
canvas.restore(); | |
} | |
@SuppressLint("NewApi") | |
private void drawHorizontal(Canvas canvas, RecyclerView parent) { | |
canvas.save(); | |
final int top; | |
final int bottom; | |
if (parent.getClipToPadding()) { | |
top = parent.getPaddingTop(); | |
bottom = parent.getHeight() - parent.getPaddingBottom(); | |
canvas.clipRect(parent.getPaddingLeft(), top, | |
parent.getWidth() - parent.getPaddingRight(), bottom); | |
} else { | |
top = 0; | |
bottom = parent.getHeight(); | |
} | |
final int childCount = parent.getChildCount(); | |
for (int i = 0; i < childCount; i++) { | |
final View child = parent.getChildAt(i); | |
parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds); | |
final int right = mBounds.right + Math.round(ViewCompat.getTranslationX(child)); | |
final int left = right - mDivider.getIntrinsicWidth(); | |
mDivider.setBounds(left, top, right, bottom); | |
mDivider.draw(canvas); | |
// ADD | |
int tmpLeft = mBounds.left + Math.round(ViewCompat.getTranslationX(child)); | |
int tmpRight = tmpLeft + mDivider.getIntrinsicWidth(); | |
mDivider.setBounds(tmpLeft, top, tmpRight, bottom); | |
mDivider.draw(canvas); | |
} | |
canvas.restore(); | |
} | |
@Override | |
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, | |
RecyclerView.State state) { | |
outRect.set(0, 0, mDivider.getIntrinsicWidth(), mDivider.getIntrinsicHeight()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment