2

我想得到如下图所示的分隔符:

我想要的是

我希望所有列的宽度相同但间距不同。现在我的第一列和最后一列被剪成这样:

我现在拥有的

这是我的ItemDecoration

public class MoreIndexItemDividerDecoration extends RecyclerView.ItemDecoration {
    private GridLayoutManager mGridLayoutManager;
    private GridLayoutManager.SpanSizeLookup mSpanSizeLookup;
    private int mSpanCount;

    public MoreIndexItemDividerDecoration(GridLayoutManager manager, int spanCount) {
        mGridLayoutManager = manager;
        mSpanSizeLookup = mGridLayoutManager.getSpanSizeLookup();
        mSpanCount = spanCount;
    }

    @Override
    public void getItemOffsets(Rect outRect, View child, RecyclerView parent, RecyclerView.State state) {
        int pos = parent.getChildLayoutPosition(child);
        int left = 0;
        int top = 0;
        int right = 0;
        int bottom = 0;

        if (mGridLayoutManager.getItemViewType(child) == 1) {
            if (mSpanSizeLookup.getSpanIndex(pos, mSpanCount) == 0) {
                left = 50;
            } else {
                left = 5;
            }
            if (mSpanSizeLookup.getSpanIndex(pos, mSpanCount) == mSpanCount - 1) {
                right = 50;
            } else {
                right = 5;
            }
            top = 5;
            bottom = 5;
        } else {
            if (mSpanSizeLookup.getSpanGroupIndex(pos, 4) != 0) {
                top = 45;
            }
            bottom = 45;
        }

        outRect.set(left, top, right, bottom);
        HtscLog.e("child pos: " + pos + " span size: " + mSpanSizeLookup.getSpanSize(pos) + " group index: " + mSpanSizeLookup.getSpanGroupIndex(pos, 4) + " index: " + mSpanSizeLookup.getSpanIndex(pos, 4) + " view type: " + mGridLayoutManager.getItemViewType(child));
    }
}

我怎样才能解决这个问题?谢谢!

4

1 回答 1

0

实际上,在getItemOffsets'soutRect参数中设置的值会占用项目的空间。

我已经实现了一个SCommonItemDecoration实际上解决了这个问题的方法。

请参考:ibosong/CommonItemDecoration

一个迟到的答案,但可能对其他有相同要求的人有所帮助。

于 2017-05-15T09:12:48.657 回答