RecyclerView ItemDecoration分隔符可以减少我的孩子观看次数

时间:2016-06-02 02:50:34

标签: android android-layout android-recyclerview rect gridlayoutmanager

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

what i want

我希望所有列的宽度相同但间距不同。 现在我的第一列和最后一列已经像这样切割了:

what i have now

这是我的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));
    }
}

我该如何解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:0)

事实上,getItemOffsets' outRect参数中设置的值占据了项目的空间。

我实施了SCommonItemDecoration,实际上解决了这个问题。

请参阅:ibosong/CommonItemDecoration

答案较晚,但可能对有相同要求的其他人有帮助。