|
| 1 | +package com.jayfeng.lesscode.core.other; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.res.TypedArray; |
| 5 | +import android.graphics.Canvas; |
| 6 | +import android.graphics.Rect; |
| 7 | +import android.graphics.drawable.Drawable; |
| 8 | +import android.support.v4.view.ViewCompat; |
| 9 | +import android.support.v7.widget.LinearLayoutManager; |
| 10 | +import android.support.v7.widget.RecyclerView; |
| 11 | +import android.view.View; |
| 12 | + |
| 13 | +/** |
| 14 | + * RecyclerView的ItemDecoration的默认实现 |
| 15 | + * 1. 默认使用系统的分割线 |
| 16 | + * 2. 支持自定义Drawable类型 |
| 17 | + * 3. 支持水平和垂直方向 |
| 18 | + * 4. 修复了官方垂直Divider显示的bug |
| 19 | + * 扩展自官方android sdk下的Support7Demos下的DividerItemDecoration |
| 20 | + */ |
| 21 | +public class DividerItemDecoration extends RecyclerView.ItemDecoration { |
| 22 | + |
| 23 | + private static final int[] ATTRS = new int[]{ |
| 24 | + android.R.attr.listDivider |
| 25 | + }; |
| 26 | + |
| 27 | + public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; |
| 28 | + |
| 29 | + public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; |
| 30 | + |
| 31 | + private Drawable mDivider; |
| 32 | + private int mWidth; |
| 33 | + private int mHeight; |
| 34 | + |
| 35 | + private int mOrientation; |
| 36 | + |
| 37 | + public DividerItemDecoration(Context context, int orientation) { |
| 38 | + final TypedArray a = context.obtainStyledAttributes(ATTRS); |
| 39 | + mDivider = a.getDrawable(0); |
| 40 | + a.recycle(); |
| 41 | + setOrientation(orientation); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * 新增:支持自定义dividerDrawable |
| 46 | + * |
| 47 | + * @param context |
| 48 | + * @param orientation |
| 49 | + * @param dividerDrawable |
| 50 | + */ |
| 51 | + public DividerItemDecoration(Context context, int orientation, Drawable dividerDrawable) { |
| 52 | + mDivider = dividerDrawable; |
| 53 | + setOrientation(orientation); |
| 54 | + } |
| 55 | + |
| 56 | + public void setOrientation(int orientation) { |
| 57 | + if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { |
| 58 | + throw new IllegalArgumentException("invalid orientation"); |
| 59 | + } |
| 60 | + mOrientation = orientation; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * 新增:支持手动为无高宽的drawable制定宽度 |
| 65 | + * @param width |
| 66 | + */ |
| 67 | + public void setWidth(int width) { |
| 68 | + this.mWidth = width; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * 新增:支持手动为无高宽的drawable制定高度 |
| 73 | + * @param height |
| 74 | + */ |
| 75 | + public void setHeight(int height) { |
| 76 | + this.mHeight = height; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void onDraw(Canvas c, RecyclerView parent) { |
| 81 | + if (mOrientation == VERTICAL_LIST) { |
| 82 | + drawVertical(c, parent); |
| 83 | + } else { |
| 84 | + drawHorizontal(c, parent); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public void drawVertical(Canvas c, RecyclerView parent) { |
| 89 | + final int left = parent.getPaddingLeft(); |
| 90 | + final int right = parent.getWidth() - parent.getPaddingRight(); |
| 91 | + |
| 92 | + final int childCount = parent.getChildCount(); |
| 93 | + for (int i = 0; i < childCount; i++) { |
| 94 | + final View child = parent.getChildAt(i); |
| 95 | + final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child |
| 96 | + .getLayoutParams(); |
| 97 | + final int top = child.getBottom() + params.bottomMargin + |
| 98 | + Math.round(ViewCompat.getTranslationY(child)); |
| 99 | + final int bottom = top + getDividerHeight(); |
| 100 | + mDivider.setBounds(left, top, right, bottom); |
| 101 | + mDivider.draw(c); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public void drawHorizontal(Canvas c, RecyclerView parent) { |
| 106 | + final int top = parent.getPaddingTop(); |
| 107 | + final int bottom = parent.getHeight() - parent.getPaddingBottom(); |
| 108 | + |
| 109 | + final int childCount = parent.getChildCount(); |
| 110 | + for (int i = 0; i < childCount; i++) { |
| 111 | + final View child = parent.getChildAt(i); |
| 112 | + final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child |
| 113 | + .getLayoutParams(); |
| 114 | + final int left = child.getRight() + params.rightMargin + |
| 115 | + Math.round(ViewCompat.getTranslationX(child)); |
| 116 | + final int right = left + getDividerWidth(); |
| 117 | + mDivider.setBounds(left, top, right, bottom); |
| 118 | + mDivider.draw(c); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { |
| 124 | + if (mOrientation == VERTICAL_LIST) { |
| 125 | + outRect.set(0, 0, 0, getDividerHeight()); |
| 126 | + } else { |
| 127 | + outRect.set(0, 0, getDividerWidth(), 0); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private int getDividerWidth() { |
| 132 | + return mWidth > 0 ? mWidth : mDivider.getIntrinsicWidth(); |
| 133 | + } |
| 134 | + |
| 135 | + private int getDividerHeight() { |
| 136 | + return mHeight > 0 ? mHeight : mDivider.getIntrinsicHeight(); |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments