自定义view实现Recyclerview的 网格布局 和 流式布局 分割线的绘制

自定义DividerGridItemDecoration 类,继承RecyclerView.ItemDecoration,实现Recyclerview的 网格布局 和 流式布局 分割线绘制

package com.example.recycleview.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.recycleview.R;


/**
 * 自定义分割线
 */
public class DividerGridItemDecoration extends RecyclerView.ItemDecoration {
    private Drawable mDivider;

    public DividerGridItemDecoration(Context context) {
        //在构造方法中,讲分割线的样子拿到
        mDivider = ContextCompat.getDrawable(context, R.drawable.recycler_divider);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        //画垂直和水平的两种分割线
        drawHorizontal(c, parent);
        drawVertical(c, parent);
    }

    public void drawHorizontal(Canvas c, RecyclerView parent) {
        int childCount = parent.getChildCount();
        //拿到所有孩子
        //根据孩子的坐标位置,计算我们的要画的分割线的位置,然后画
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getLeft() - params.leftMargin;
            final int right = child.getRight() + params.rightMargin
                    + mDivider.getIntrinsicWidth();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    public void drawVertical(Canvas c, RecyclerView parent) {
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int top = child.getTop() - params.topMargin;
            final int bottom = child.getBottom() + params.bottomMargin;
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicWidth();

            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }


    @Override
    public void getItemOffsets(Rect outRect, int itemPosition,
                               RecyclerView parent) {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(),
                    mDivider.getIntrinsicHeight());
    }
}

对应的shape布局文件





    
    
        //设置
    



你可能感兴趣的:(自定义view实现Recyclerview的 网格布局 和 流式布局 分割线的绘制)