|
| 1 | +package com.blankj.androidutilcode.base.rv; |
| 2 | + |
| 3 | +import android.annotation.SuppressLint; |
| 4 | +import android.content.Context; |
| 5 | +import android.graphics.Canvas; |
| 6 | +import android.graphics.Rect; |
| 7 | +import android.graphics.drawable.Drawable; |
| 8 | +import android.support.annotation.DrawableRes; |
| 9 | +import android.support.annotation.NonNull; |
| 10 | +import android.support.v4.content.ContextCompat; |
| 11 | +import android.support.v4.view.ViewCompat; |
| 12 | +import android.support.v7.widget.RecyclerView; |
| 13 | +import android.view.View; |
| 14 | +import android.widget.LinearLayout; |
| 15 | + |
| 16 | +/** |
| 17 | + * <pre> |
| 18 | + * author: Blankj |
| 19 | + * blog : http://blankj.com |
| 20 | + * time : 2017/08/17 |
| 21 | + * desc : |
| 22 | + * </pre> |
| 23 | + */ |
| 24 | +public class RecycleViewDivider extends RecyclerView.ItemDecoration { |
| 25 | + |
| 26 | + public static final int HORIZONTAL = LinearLayout.HORIZONTAL; |
| 27 | + public static final int VERTICAL = LinearLayout.VERTICAL; |
| 28 | + |
| 29 | + protected Drawable mDivider; |
| 30 | + |
| 31 | + protected int mOrientation; |
| 32 | + |
| 33 | + protected final Rect mBounds = new Rect(); |
| 34 | + |
| 35 | + public RecycleViewDivider(Context context, int orientation, @DrawableRes int resId) { |
| 36 | + this(context, orientation, ContextCompat.getDrawable(context, resId)); |
| 37 | + } |
| 38 | + |
| 39 | + public RecycleViewDivider(Context context, int orientation, @NonNull Drawable divider) { |
| 40 | + setOrientation(orientation); |
| 41 | + mDivider = divider; |
| 42 | + } |
| 43 | + |
| 44 | + public void setOrientation(int orientation) { |
| 45 | + if (orientation != HORIZONTAL && orientation != VERTICAL) { |
| 46 | + throw new IllegalArgumentException( |
| 47 | + "Invalid orientation. It should be either HORIZONTAL or VERTICAL"); |
| 48 | + } |
| 49 | + mOrientation = orientation; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { |
| 54 | + if (parent.getLayoutManager() == null) { |
| 55 | + return; |
| 56 | + } |
| 57 | + if (mOrientation == VERTICAL) { |
| 58 | + drawVertical(c, parent); |
| 59 | + } else { |
| 60 | + drawHorizontal(c, parent); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @SuppressLint("NewApi") |
| 65 | + protected void drawVertical(Canvas canvas, RecyclerView parent) { |
| 66 | + canvas.save(); |
| 67 | + final int left; |
| 68 | + final int right; |
| 69 | + if (parent.getClipToPadding()) { |
| 70 | + left = parent.getPaddingLeft(); |
| 71 | + right = parent.getWidth() - parent.getPaddingRight(); |
| 72 | + canvas.clipRect(left, parent.getPaddingTop(), right, |
| 73 | + parent.getHeight() - parent.getPaddingBottom()); |
| 74 | + } else { |
| 75 | + left = 0; |
| 76 | + right = parent.getWidth(); |
| 77 | + } |
| 78 | + |
| 79 | + final int childCount = parent.getChildCount(); |
| 80 | + for (int i = 0; i < childCount; i++) { |
| 81 | + final View child = parent.getChildAt(i); |
| 82 | + parent.getDecoratedBoundsWithMargins(child, mBounds); |
| 83 | + final int bottom = mBounds.bottom + Math.round(ViewCompat.getTranslationY(child)); |
| 84 | + final int top = bottom - mDivider.getIntrinsicHeight(); |
| 85 | + mDivider.setBounds(left, top, right, bottom); |
| 86 | + mDivider.draw(canvas); |
| 87 | + } |
| 88 | + canvas.restore(); |
| 89 | + } |
| 90 | + |
| 91 | + @SuppressLint("NewApi") |
| 92 | + protected void drawHorizontal(Canvas canvas, RecyclerView parent) { |
| 93 | + canvas.save(); |
| 94 | + final int top; |
| 95 | + final int bottom; |
| 96 | + if (parent.getClipToPadding()) { |
| 97 | + top = parent.getPaddingTop(); |
| 98 | + bottom = parent.getHeight() - parent.getPaddingBottom(); |
| 99 | + canvas.clipRect(parent.getPaddingLeft(), top, |
| 100 | + parent.getWidth() - parent.getPaddingRight(), bottom); |
| 101 | + } else { |
| 102 | + top = 0; |
| 103 | + bottom = parent.getHeight(); |
| 104 | + } |
| 105 | + |
| 106 | + final int childCount = parent.getChildCount(); |
| 107 | + for (int i = 0; i < childCount; i++) { |
| 108 | + final View child = parent.getChildAt(i); |
| 109 | + parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds); |
| 110 | + final int right = mBounds.right + Math.round(ViewCompat.getTranslationX(child)); |
| 111 | + final int left = right - mDivider.getIntrinsicWidth(); |
| 112 | + mDivider.setBounds(left, top, right, bottom); |
| 113 | + mDivider.draw(canvas); |
| 114 | + } |
| 115 | + canvas.restore(); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { |
| 120 | + if (mOrientation == VERTICAL) { |
| 121 | + outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); |
| 122 | + } else { |
| 123 | + outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments