最近在做项目中使用RecyclerView的addItemDecoration方法添加分割线,出现了分割线在有些手机(比如魅蓝和小米手机)显示不出来的问题。
后来在网上找了一些资料,还是无法修改分割线颜色,最后找到了正确的方法,实现了修改分割线颜色和高度。
自定义RecyclerView.ItemDecoration类的子类SimpleDividerItemDecoration类,代码如下:
- public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
- private Drawable mDivider;
- public SimpleDividerItemDecoration(Context context) {
- mDivider = context.getResources().getDrawable(R.drawable.line_divider);
- }
- @Override
- public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
- int left = parent.getPaddingLeft();
- int right = parent.getWidth() - parent.getPaddingRight();
- int childCount = parent.getChildCount();
- for (int i = 0; i < childCount; i++) {
- View child = parent.getChildAt(i);
- RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
- int top = child.getBottom() + params.bottomMargin;
- int bottom = top + mDivider.getIntrinsicHeight();
- mDivider.setBounds(left, top, right, bottom);
- mDivider.draw(c);
- }
- }
- }
public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;
public SimpleDividerItemDecoration(Context context) {
mDivider = context.getResources().getDrawable(R.drawable.line_divider);
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}
说明:context.getResources().getDrawable(R.drawable.line_divider);
line_divider.xml:
- <?xml version=“1.0” encoding=“utf-8”?>
- <shape xmlns:android=”http://schemas.android.com/apk/res/android”
- android:shape=”rectangle”>
- <size
- android:width=”1dp”
- android:height=”1dp” />
- <solid android:color=”@color/dark_gray” />
- </shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="1dp"
android:height="1dp" />
<solid android:color="@color/dark_gray" />
</shape>
说明:通过修改android:color和android:height可以修改分割线颜色和宽度。
最后,RecyclerView对象使用addItemDecoration方法添加分割线,代码如下:- recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this));
recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this));
原文地址:http://stackoverflow.com/questions/31242812/how-can-a-divider-line-be-added-in-an-android-recyclerview
上面方式定义的公共类SimpleDividerItemDecoration类可以被许多页面的RecyclerView共用,分割线的宽度和颜色统一,缺点是无法实现不同页面使用不同颜色和高度的分割线。
以下对SimpleDividerItemDecoration进行了修正,实现了自定义颜色和高度的分割线效果。
- public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
- private Drawable mDivider; //分割线Drawable
- private int mDividerHeight; //分割线高度
- /**
- * 使用line_divider中定义好的颜色
- * @param context
- * @param dividerHeight 分割线高度
- */
- public SimpleDividerItemDecoration(Context context, int dividerHeight) {
- mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider);
- mDividerHeight = dividerHeight;
- }
- /**
- *
- * @param context
- * @param divider 分割线Drawable
- * @param dividerHeight 分割线高度
- */
- public SimpleDividerItemDecoration(Context context, Drawable divider, int dividerHeight){
- if(divider == null){
- mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider);
- }else{
- mDivider = divider;
- }
- mDividerHeight = dividerHeight;
- }
- //获取分割线尺寸
- @Override
- public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
- super.getItemOffsets(outRect, view, parent, state);
- outRect.set(0, 0, 0, mDividerHeight);
- }
- @Override
- public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
- int left = parent.getPaddingLeft();
- int right = parent.getWidth() - parent.getPaddingRight();
- int childCount = parent.getChildCount();
- for (int i = 0; i < childCount; i++) {
- View child = parent.getChildAt(i);
- RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
- int top = child.getBottom() + params.bottomMargin;
- int bottom = top + mDividerHeight;
- mDivider.setBounds(left, top, right, bottom);
- mDivider.draw(c);
- }
- }
public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider; //分割线Drawable
private int mDividerHeight; //分割线高度
/**
* 使用line_divider中定义好的颜色
* @param context
* @param dividerHeight 分割线高度
*/
public SimpleDividerItemDecoration(Context context, int dividerHeight) {
mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider);
mDividerHeight = dividerHeight;
}
/**
*
* @param context
* @param divider 分割线Drawable
* @param dividerHeight 分割线高度
*/
public SimpleDividerItemDecoration(Context context, Drawable divider, int dividerHeight){
if(divider == null){
mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider);
}else{
mDivider = divider;
}
mDividerHeight = dividerHeight;
}
//获取分割线尺寸
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.set(0, 0, 0, mDividerHeight);
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDividerHeight;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
使用方法:
- Drawable drawable = ContextCompat.getDrawable(getActivity(), R.drawable.line_divider_red);
- recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this,drawable, 5));
Drawable drawable = ContextCompat.getDrawable(getActivity(), R.drawable.line_divider_red);
recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this,drawable, 5));
转自cherrlasea的博客:http://blog.csdn.net/hxltech/article/details/52117604