使用StaggeredGridLayoutManager的RecyclerView内的项目之间的相等间距?

时间:2016-01-11 03:32:51

标签: android android-recyclerview

目前正尝试使用此类:

public class EqualItemSpacingDecoration extends RecyclerView.ItemDecoration {

    private int space;

    public EqualItemSpacingDecoration(int space) {
        this.space = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.bottom = space;
        if (parent.getChildLayoutPosition(view) < 2)
            outRect.top = space;
        if (parent.getChildLayoutPosition(view) % 2 == 0) {
            outRect.left = space;
            outRect.right = space / 2;
        } else {
            outRect.left = space / 2;
            outRect.right = space;
        }
    }

}

并使用此代码:

recycler.addItemDecoration(new EqualItemSpacingDecoration(12));

但间距不相等。下面的屏幕截图中的第三项具有不正确的间距: Spacing is not correct with the third item.

我如何解决此问题?

0 个答案:

没有答案