RecyclerView - ItemDecorator - 不会切断项目的间距

时间:2015-08-29 08:36:36

标签: android android-recyclerview gridlayoutmanager

我使用以下分隔符进行垂直线性布局,它很好地工作:

public class VerticalSpaceItemDecoration extends RecyclerView.ItemDecoration {

    private final int mVerticalSpaceHeight;

    public VerticalSpaceItemDecoration(int mVerticalSpaceHeight) {
        this.mVerticalSpaceHeight = mVerticalSpaceHeight;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if (parent.getChildAdapterPosition(view) != parent.getAdapter().getItemCount() - 1)
            outRect.bottom = mVerticalSpaceHeight;
    }
}

基于此,我尝试为GridLayout编写相同的装饰器,但问题是,它正在切断我的项目。它看起来如下:

public class GridSpaceItemDecoration extends RecyclerView.ItemDecoration {

    private final int mSpaceSize;
    private final int mCols;

    public GridSpaceItemDecoration(int spaceSize, int cols) {
        this.mSpaceSize = spaceSize;
        mCols = cols;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

        int rows = (int)Math.floor(parent.getAdapter().getItemCount() / mCols) + 1;
        int pos = parent.getChildAdapterPosition(view);
        int col = pos % mCols;
        int row = (int)Math.floor(pos / mCols);

        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();

        if (col < mCols - 1)
            outRect.right = /*params.rightMargin + view.getPaddingRight() +*/ mSpaceSize;
        if (row < rows - 1)
            outRect.bottom = /*params.leftMargin + view.getPaddingLeft() +*/ mSpaceSize;
    }
}

两个Decorators的结果如下(添加调试的颜色):

enter image description here

图像显示,GridLayout项目被切断(在项目下方的文字处很容易看到)。我应该提一下,两者的适配器是相同的,我只使用另一个LayoutManager。这些物品是固定尺寸,这是我需要的东西!

1 个答案:

答案 0 :(得分:0)

setClipChildren(false)上调用RecyclerView,它应该修复您看到的由装饰器创建的间距引起的剪辑。