RecyclerView怎么修改分割线?

styles.xml做如下修改:

<style name="AppBaseTheme" parent="android:Theme.Light"></style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- 修改分割线样式--> <item name="android:listDivider">@drawable/s</item> </style>

@draweble/s代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:centerColor="#ff00ff00" android:endColor="#ff0000ff" android:startColor="#ffff0000" android:type="linear" /> <size android:height="4dp"/> </shape>

MyItemDecoration代码如下:

package com.wuzhiqiang.learnswiperefreshlayout; import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View; /** * Created by mfpc on 2015/11/2. */
public class MyItemDecoration extends RecyclerView.ItemDecoration
{ private static final int[] ATTRS = {android.R.attr.listDivider}; private Drawable mDivider; public MyItemDecorationContext context { TypedArray array = context.obtainStyledAttributesATTRS; // 获取分隔条 mDivider = array.getDrawable0; array.recycle; } @Override public void onDrawOverCanvas c, RecyclerView parent, RecyclerView.State state { super.onDrawOverc, parent, state; int count = parent.getChildCount; int left = parent.getPaddingLeft; int right = parent.getWidth-parent.getPaddingRight; forint i = 0; i < count; i++ { View v = parent.getChildAti; RecyclerView.LayoutParams params = RecyclerView.LayoutParams v.getLayoutParams; int top = v.getBottom + params.bottomMargin; int bottom = top + mDivider.getIntrinsicHeight; mDivider.setBoundsleft,top,right,bottom; mDivider.drawc; } }
}

但是中间出错了:

@drawabel/s文件应该怎么写?或者使其他地方有问题?

应为 RecyclerView 和 LayoutManager 是分离的,所以没有分割线属性,可以用 RecyclerView.ItemDecoration 进行分割线的绘制。

提供一个简单的装饰

/** * 分隔线装饰 * * @author youmingdot */
public class DividerLine extends RecyclerView.ItemDecoration { /** * 水平方向 */ public static final int HORIZONTAL = LinearLayoutManager.HORIZONTAL; /** * 垂直方向 */ public static final int VERTICAL = LinearLayoutManager.VERTICAL; // 画笔 private Paint paint; // 布局方向 private int orientation; // 分割线颜色 private int color; // 分割线尺寸 private int size; public DividerLine { thisVERTICAL; } public DividerLineint orientation { this.orientation = orientation; paint = new Paint; } @Override public void onDrawOverCanvas c, RecyclerView parent, RecyclerView.State state { super.onDrawOverc, parent, state; if orientation == VERTICAL { drawHorizontalc, parent; } else { drawVerticalc, parent; } } /** * 设置分割线颜色 * * @param color 颜色 */ public void setColorint color { this.color = color; paint.setColorcolor; } /** * 设置分割线尺寸 * * @param size 尺寸 */ public void setSizeint size { this.size = size; } // 绘制垂直分割线 protected void drawVerticalCanvas c, RecyclerView parent { final int top = parent.getPaddingTop; final int bottom = parent.getHeight - parent.getPaddingBottom; final int childCount = parent.getChildCount; for int i = 0; i < childCount; i++ { final View child = parent.getChildAti; final RecyclerView.LayoutParams params = RecyclerView.LayoutParams child.getLayoutParams; final int left = child.getRight + params.rightMargin; final int right = left + size; c.drawRectleft, top, right, bottom, paint; } } // 绘制水平分割线 protected void drawHorizontalCanvas c, RecyclerView parent { final int left = parent.getPaddingLeft; final int right = parent.getWidth - parent.getPaddingRight; final int childCount = parent.getChildCount; for int i = 0; i < childCount; i++ { final View child = parent.getChildAti; final RecyclerView.LayoutParams params = RecyclerView.LayoutParams child.getLayoutParams; final int top = child.getBottom + params.bottomMargin; final int bottom = top + size; c.drawRectleft, top, right, bottom, paint; } }
}

使用方法

DividerLine dividerLine = new DividerLineDividerLine.VERTICAL;
dividerLine.setSize1;
dividerLine.setColor0xFFDDDDDD;
recyclerView.addItemDecorationdividerLine;

发表评论

电子邮件地址不会被公开。 必填项已用*标注