Android-给RecyclerView添加分隔线

RecyclerView和ListView不同 ,是不自带分隔线的

如此:

Android-给RecyclerView添加分隔线_第1张图片

在讲为Item加入分割线本质的前,先来介绍,认识一下ChildView,也就是平时我们用到的ListView
RecyclerView中的getChildAt(int position)这个返回的ChildView是哪一部分?

修改list_item的代码,添加一条 

android:layout_margin = "10dp"

Android-给RecyclerView添加分隔线_第2张图片

Android-给RecyclerView添加分隔线_第3张图片

ChildView就是图中蓝色这一部分,边距这一部分并不属于ChildView

那么在RecyclerView中,Google为我们提供了RecyclerView.ItemDecoration添加分隔线

它是一个类,里面封装了三个方法:
(1)void getItemOffsets()
(2)void onDraw()
(3)void onDrawOver()

我们在创建自己的装饰类继承RecyclerView.ItemDecoration的时候,我们只要重写getItemOffsets(),还有onDraw()和onDrawOver两个其中之一就可以了。
那getItemOffsets()方法有什么用呢?从字面意思就是Item要偏移,由于我们在Item和Item之间加入了分隔线,线其实本质就是一个长方形,也是用户自定义的,既然线也有长宽高,就画横线来说,上面的项目加入了分隔线,那下面的项目就要往下平移,平移的量就是分隔线的高度。

我们自定义的MyDecoration.java :(继承RecyclerView.ItemDecoration)

public class MyDecoration extends RecyclerView.ItemDecoration{

    private Context mContext;
    private Drawable mDivider;
    private int mOrientation;
    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

    //我们通过获取系统属性中的listDivider来添加,在系统中的AppTheme中设置
    public static final int[] ATRRS  = new int[]{
            android.R.attr.listDivider
    };

    public MyDecoration(Context context, int orientation) {
        this.mContext = context;
        final TypedArray ta = context.obtainStyledAttributes(ATRRS);
        this.mDivider = ta.getDrawable(0);
        ta.recycle();
        setOrientation(orientation);
    }

    //设置屏幕的方向
    public void setOrientation(int orientation){
        if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST){
            throw new IllegalArgumentException("invalid orientation");        }        mOrientation = orientation;
    } 

   @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (mOrientation == HORIZONTAL_LIST){
            drawVerticalLine(c, parent, state);
        }else {
            drawHorizontalLine(c, parent, state);
        }
    }

    //画横线, 这里的parent其实是显示在屏幕显示的这部分
    public void drawHorizontalLine(Canvas c, RecyclerView parent, RecyclerView.State state){
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++){
            final View child = parent.getChildAt(i);

            //获得child的布局信息
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)child.getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
            //Log.d("wnw", left + " " + top + " "+right+"   "+bottom+" "+i);
        }
    }

    //画竖线
    public void drawVerticalLine(Canvas c, RecyclerView parent, RecyclerView.State state){
        int top = parent.getPaddingTop();
        int bottom = parent.getHeight() - parent.getPaddingBottom();
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++){
            final View child = parent.getChildAt(i); 

           //获得child的布局信息
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)child.getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicWidth();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    //由于Divider也有长宽高,每一个Item需要向下或者向右偏移
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if(mOrientation == HORIZONTAL_LIST){
            //画横线,就是往下偏移一个分割线的高度
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        }else {
            //画竖线,就是往右偏移一个分割线的宽度
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        }
    }
}

现在用shape来编写一个分割线样式:
在drawable右键添加xml文件即可


    
    
这是一条渐变的分割线

在styles.xml的AppTheme中,设置listDivider为我们的divider.xml文件:

@drawable/divider
这样,我们将系统的listDivider设置成我们自定义的分隔线。

在主活动里这样添加我们自定义的分割线

recyclerView.addItemDecoration(new MyDecoration(this,MyDecoration.VERTICAL_LIST));

运行后如下

Android-给RecyclerView添加分隔线_第4张图片


你可能感兴趣的:(微型技术博客)