RecyclerView设置分隔线的三种方法
一.在item.xml文件中在最下方指定一条分割线,例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image_view"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_view"
android:layout_gravity="center"
android:gravity="left"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="@color/colorPrimary"/>
</LinearLayout>
二.可以指定宽度颜色的模板(成品模板)
1.添加默认分割线:高度为2px,颜色为灰色
mRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL));
2.添加自定义分割线:可自定义分割线drawable
mRecyclerView.addItemDecoration(new RecycleViewDivider(
mContext, LinearLayoutManager.VERTICAL, R.drawable.divider_mileage));
3.添加自定义分割线:可自定义分割线高度和颜色
mRecyclerView.addItemDecoration(new RecycleViewDivider(
mContext, LinearLayoutManager.VERTICAL, 10, getResources().getColor(R.color.divide_gray_color)));
三.万能分割线
package hwp.com.recyclerviewdemo;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
/**
* Created by hwp on 2018/12/29
*/
class MyItemDecoration extends RecyclerView.ItemDecoration{
private final int mOrientation;
private final Context mContext;
private final int[] attrs=new int[]{android.R.attr.listDivider};
private int mDividerHeight;
private Paint mPaint;
private Drawable mDivider;
/**
* 默认分割线:高度为2px,颜色为灰色
*
* @param context
* @param orientation 列表方向
*/
public MyItemDecoration(Context context, int orientation){
if(orientation!= LinearLayoutManager.VERTICAL&&orientation!=LinearLayoutManager.HORIZONTAL)throw new IllegalArgumentException("请输入正确参数");
this.mContext=context;
this.mOrientation=orientation;
TypedArray a = context.obtainStyledAttributes(attrs);
mDivider=a.getDrawable(0);
a.recycle();
}
/**
* 自定义分割线
*
* @param context
* @param orientation 列表方向
* @param drawableId 分割线图片
*/
public MyItemDecoration(Context context,int orientation,int drawableId){
this(context,orientation);
mDivider= ContextCompat.getDrawable(context,drawableId);
mDividerHeight =mDivider.getIntrinsicHeight();
}
/**
*
* @param context
* @param orientation 列表方向
* @param mDividerHeight 分割线高度
* @param mDividerColor 分割线颜色
*/
public MyItemDecoration(Context context,int orientation,int mDividerHeight,int mDividerColor){
this(context,orientation);
this.mDividerHeight=mDividerHeight;
mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(mDividerColor);
mPaint.setStyle(Paint.Style.FILL);
}
//绘制分割线
@Override
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.onDraw(c, parent, state);
if(mOrientation==LinearLayoutManager.VERTICAL){
drawVertical(c,parent);
}else{
drawHorizontal(c,parent);
}
}
//绘制横向 item 分割线
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getMeasuredWidth() - parent.getPaddingRight();
final int childSize = parent.getChildCount();
for (int i = 0; i < childSize; i++) {
final View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
final int top = child.getBottom() + layoutParams.bottomMargin;
final int bottom = top + mDividerHeight;
if (mDivider != null) {
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
if (mPaint != null) {
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
}
//绘制纵向 item 分割线
private void drawVertical(Canvas canvas, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();
final int childSize = parent.getChildCount();
for (int i = 0; i < childSize; i++) {
final View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
final int left = child.getRight() + layoutParams.rightMargin;
final int right = left + mDividerHeight;
if (mDivider != null) {
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
if (mPaint != null) {
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.set(0,0,0,mDividerHeight);
}
}
网友评论