RecyclerView设置verticalSapcing等

RecyclerView没有像GridView那样有提供verticalSpacing属性,上StackOverflow找到了一种替代方法,代码如下

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
    private int mVerticalSpacing, mHorizontalSpacing;

    public SpacesItemDecoration(int verticalSpacing, int horizontalSpacing){
        mHorizontalSpacing = horizontalSpacing;
        mVerticalSpacing = verticalSpacing;
    }

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

在RecyclerView中使用

int verticalSpacing = getResources().getDimensionPixelSize(R.dimen.my_items_grid_vertical_spacing);
int horizontalSpacing = getResources().getDimensionPixelSize(R.dimen.my_items_grid_horizontal_spacing);
recyclerView.addItemDecoration(new SpacesItemDecoration(verticalSpacing, horizontalSpacing));

 

posted @ 2015-06-11 17:21  BaronZ  阅读(1017)  评论(0编辑  收藏  举报