0

我有一个回收站视图,我想在单元格之间添加一个空格。但是,使用下面的代码可以工作,中间的空间增加了一倍。

我怎样才能隔开它们并避免中间的双倍空间?

public class ItemDecorationAlbumColumns extends RecyclerView.ItemDecoration {
    private int space;

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

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.left = space;
        outRect.right = space;
        outRect.bottom = space;

        // Add top margin only for the first item to avoid double space between items
        if(parent.getChildAdapterPosition(view) == 0) {
            outRect.top = space;
        }
    }
}

来电:

recyclerView.addItemDecoration(new ItemDecorationAlbumColumns(50));
4

1 回答 1

0

使用以下代码解决:

public class ItemDecorationAlbumColumns extends RecyclerView.ItemDecoration {
    private int space;

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

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

        if(parent.getChildAdapterPosition(view) % 2 == 0) {
            outRect.right = space;
            outRect.bottom = space;
            outRect.left = space/2;
        } else {
            outRect.left = space;
            outRect.right = space/2;
            outRect.bottom = space;
        }

        if(parent.getChildAdapterPosition(view) == 0) {
            outRect.top = space;
            outRect.left = space;
        }
    }
}
于 2017-10-26T16:55:59.490 回答