![](https://img.haomeiwen.com/i3793005/0ba295f69b0b3bce.png)
【自定义RecyclerView】
/ **
* @Author Lee
* @Time 2018/3/12
* @Theme
*/
public class MyRecyclerView extends RecyclerView {
public MyRecyclerView(Context context) {
super(context);
addItemDecoration(new DividerLine(context,attrs));
}
public MyRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
addItemDecoration(new DividerLine(context,attrs));
}
public MyRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
addItemDecoration(new DividerLine(context,attrs));
}
}
【自定义分割线】
/**
* @Author Lee
* @Time 2018/3/12
* @Theme
*/
public class DividerLine extends RecyclerView.ItemDecoration {
private int mColor = Color.GRAY;
private int thickness = DEFAULT_THICKNESS;
private int padding;
private static final int DEFAULT_THICKNESS = 1;
private int orientation = HORIZONTAL;
public static final int HORIZONTAL = 0;
public static final int VERTICAL = 1;
public static final int GRID = 2;
private boolean drawBound = false;
private int gridNum = 1;
public DividerLine(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.divder);
mColor = a.getColor(R.styleable.divder__dividerColor, mColor);
padding = (int) a.getDimension(R.styleable.divder__dividerPadding, padding);
thickness = (int) a.getDimension(R.styleable.divder__thickness, thickness);
orientation = a.getInt(R.styleable.divder__orientation, 0);
drawBound = a.getBoolean(R.styleable.divder__drawBound, false);
gridNum = a.getInt(R.styleable.divder__grid_col_num, 1);
a.recycle();
}
@Override
public void onDraw(Canvas c, RecyclerView parent) {
Paint p = new Paint();
p.setColor(mColor);
p.setStrokeWidth(thickness);
//画最前面的一个头部分割线
if (drawBound && parent.getChildCount() > 0) {
View v = parent.getChildAt(0);
if (orientation == HORIZONTAL) {
//画水平位置的分隔线
c.drawLine(v.getLeft() + padding, v.getTop(), v.getRight() - padding, v.getTop(), p);
}
if (orientation == VERTICAL) {
//画竖直位置的分隔线(GridView)
c.drawLine(v.getLeft(), v.getTop()+ padding, v.getLeft(), v.getBottom() - padding, p);
}
if (orientation == GRID) {
for (int i = 0; i < parent.getChildCount(); i += gridNum) {
v= parent.getChildAt(i);
//都画
c.drawLine(v.getLeft() + padding, v.getTop(), v.getRight() - padding, v.getTop(), p);
c.drawLine(v.getLeft(), v.getTop()+ padding, v.getLeft(), v.getBottom() - padding, p);
}
}
}
int to = drawBound ? parent.getChildCount() : parent.getChildCount() - 1;
for (int i = 0; i < to; i++) {
View v = parent.getChildAt(i);
if (orientation == 0) {
//画水平位置的分隔线
c.drawLine(v.getLeft() + padding, v.getBottom(), v.getRight() - padding, v.getBottom(), p);
//画竖直位置的分隔线(GridView)
}
if (orientation == 1) {
c.drawLine(v.getRight(), v.getTop()+ padding, v.getRight(), v.getBottom()- padding, p);
}
if (orientation == GRID) {
c.drawLine(v.getLeft() + padding, v.getBottom(), v.getRight() - padding, v.getBottom(), p);
c.drawLine(v.getRight(), v.getTop()+ padding, v.getRight(), v.getBottom()- padding, p);
}
}
}
}
【xml 配置】
<com.lee.arcproject.view.MyRecyclerView
android:id="@+id/recylerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="GridLayoutManager"
app:spanCount="3"
app:_dividerColor="@color/font_deep_red"
app:_drawBound="false"
app:_dividerPadding="@dimen/padding_8dp"
app:_orientation="grid"
app:_thickness="3px"
app:_grid_col_num="3"
/>
【来源】 http://blog.csdn.net/u013147734/article/details/51263581
网友评论