1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| import android.graphics.Canvas; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.support.annotation.Nullable; import android.support.v4.util.SparseArrayCompat; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View;
abstract class ItemHolderDivider extends RecyclerView.ItemDecoration { private final SparseArrayCompat<Drawable> mDividers = new SparseArrayCompat<>(2); private final SparseArrayCompat<Integer> mHeights = new SparseArrayCompat<>(2);
protected abstract int keyFrom(RecyclerView.ViewHolder holder);
@Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { final int childCount = parent.getChildCount(); final int width = parent.getWidth(); final int height = parent.getHeight(); for (int childViewIndex = 0; childViewIndex < childCount; childViewIndex++) { final View view = parent.getChildAt(childViewIndex); RecyclerView.ViewHolder holder = parent.getChildViewHolder(view); int key = keyFrom(holder); if (isVertical(parent)) { drawBottom(c, key, (int) view.getY() + view.getHeight(), width); drawTop(c, -key, (int) view.getY(), width); } else { drawRight(c, key, (int) view.getX() + view.getWidth(), height); drawLeft(c, -key, (int) view.getX(), height); } } }
@Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { RecyclerView.ViewHolder holder = parent.getChildViewHolder(view); int key = keyFrom(holder); if (isVertical(parent)) { outRect.bottom = getHeight(key); outRect.top = getHeight(-key); } else { outRect.right = getHeight(key); outRect.left = getHeight(-key); } }
private void drawBottom(Canvas c, int key, int y, int width) { Drawable d = mDividers.get(key); if (d != null) { d.setBounds(0, y, width, y + getHeight(key, d)); d.draw(c); } }
private void drawTop(Canvas c, int key, int y, int width) { Drawable d = mDividers.get(key); if (d != null) { d.setBounds(0, y - getHeight(key, d), width, y); d.draw(c); } }
private void drawRight(Canvas c, int key, int x, int height) { Drawable d = mDividers.get(key); if (d != null) { d.setBounds(x, 0, x + getHeight(key, d), height); d.draw(c); } }
private void drawLeft(Canvas c, int key, int x, int height) { Drawable d = mDividers.get(key); if (d != null) { d.setBounds(x - getHeight(key, d), 0, x, height); d.draw(c); } }
final void putDrawable(int key, Drawable drawable) { mDividers.put(key, drawable); }
final void putHeight(int key, @Nullable Drawable drawable, int height) { if (drawable != null) { mDividers.put(key, drawable); } mHeights.put(key, height); }
private int getHeight(int key) { Drawable d = mDividers.get(key); return getHeight(key, d); }
private int getHeight(int key, @Nullable Drawable d) { int index = mHeights.indexOfKey(key); return index < 0 ? d == null ? 0 : d.getIntrinsicHeight() : mHeights.valueAt(index); }
private boolean isVertical(RecyclerView parent) { RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); return !(layoutManager instanceof LinearLayoutManager) || ((LinearLayoutManager) layoutManager).getOrientation() == LinearLayoutManager.VERTICAL; } }
|