RecyclerView的ItemDecoration使用

使用过RecyclerView的人都知道,它的功能很强大,可扩展性很强,今天说说ItemDecoration的使用。
ItemDecoration,是用来设置RecyclerView中每个item的间距的,这里不做过多的解释,本篇以GridLayoutManager为例,说说用这个ItemDecoration遇到的问题。
比如我们有三列网格布局,左边边距leftSpace,中间边距都为midSpace,右边边距rightSpace,

RecyclerView的ItemDecoration使用_第1张图片
QQ截图20170529213656.png

根据很多网上的自定义ItemDecoration,可能有如下代码:

public class SpaceItemDecoration2 extends RecyclerView.ItemDecoration {

    private int leftSpace;
    private int midSpace;
    private int rightSpace;

    public SpaceItemDecoration2(int leftSpace, int midSpace, int rightSpace) {
        this.leftSpace = leftSpace;
        this.midSpace = midSpace;
        this.rightSpace = rightSpace;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if (parent.getChildLayoutPosition(view) % 3 == 0) {
            outRect.left = leftSpace;
            outRect.right=0;
        } else if (parent.getChildLayoutPosition(view) % 3 == 1) {
            outRect.left = midSpace;
            outRect.right=0;
        } else if (parent.getChildLayoutPosition(view) % 3 == 2) {
            outRect.left = midSpace;
            outRect.right=rightSpace;
        }
    }
}

这样咋一看以为没毛病,我一开始也这么以为,这样运行起来看,差别挺大,不能实现item的等宽,特别是我当时没有leftSpace和RightSpace,只有midSpace的时候,运行起来,差别不是那么明显,但就是不等宽。这是因为当设置GridLayoutManager的时候,设置3列,首先会均分成三列,如果有设置ItemDecoration,那么是会在每个item的内部分出间距来。
因此,如果需要实现每个item等宽,那么就需要让每个item的分出来的间距相等,明白了这点,接下去就不难了。
下面的代码,只是实现了水平的等分,至于垂直方向,自行扩展。

public class CommonDecoration extends RecyclerView.ItemDecoration {

    private int leftSpace;
    private int midSpace;
    private int rightSpace;

    public CommonDecoration(int leftSpace, int midSpace, int rightSpace) {
        this.leftSpace = leftSpace;
        this.midSpace = midSpace;
        this.rightSpace = rightSpace;
    }

    public CommonDecoration(int space) {
        this.leftSpace = space;
        this.midSpace = space;
        this.rightSpace = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            int spanCount = gridLayoutManager.getSpanCount();
            int totalSpace = leftSpace + rightSpace + midSpace * (spanCount - 1);
            int itemNeedSpace = totalSpace / spanCount;
            if (parent.getChildAdapterPosition(view) % spanCount == 0) {
                //最左一条
                outRect.left = leftSpace;
                outRect.right = itemNeedSpace - leftSpace;
            } else if (parent.getChildAdapterPosition(view) % spanCount == spanCount - 1) {
                //最右一条
                outRect.left = itemNeedSpace - rightSpace;
                outRect.right = rightSpace;
            } else {
                outRect.left = itemNeedSpace / 2;
                outRect.right = itemNeedSpace / 2;
            }
        }
    }
}

你可能感兴趣的:(RecyclerView的ItemDecoration使用)