删除回收器视图网格布局中的默认间距

时间:2016-03-17 20:07:32

标签: android gridview android-recyclerview

我使用StaggeredGridLayout经理进行回收商视图

mStaggerGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager
                .VERTICAL);

现在,我想删除列和行之间的默认间距。像这张图片中的东西,但只有2列。

enter image description here

1 个答案:

答案 0 :(得分:1)

你必须利用保证金。不是填充物。

StaggeredGridLayoutManager为每个网格项设置默认边距“30dp”。

可以改变如下,

class StaggeredListDecoration extends RecyclerView.ItemDecoration {

    public StaggeredListDecoration() {

    }
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        BaseCard.CARD_TYPE viewType = (BaseCard.CARD_TYPE)view.getTag();
            ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).leftMargin = 0;
            ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).rightMargin = 0;
            ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).topMargin = 0;
            ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).bottomMargin= 0;
    }
}