RecyclerView设置每个item之间间隔

RecyclerView设置每个item之间间隔

private void initDecoration() {
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.TOP_DECORATION, 0);//top间距
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.BOTTOM_DECORATION, 0);//底部间距
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.LEFT_DECORATION, 6);//左间距
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.RIGHT_DECORATION, 6);//右间距
        firstRecycler.addItemDecoration(new RecyclerViewSpacesItemDecoration(stringIntegerHashMap));
        secondRecycler.addItemDecoration(new RecyclerViewSpacesItemDecoration(stringIntegerHashMap));
    }
public class RecyclerViewSpacesItemDecoration extends RecyclerView.ItemDecoration {
    private HashMap<String, Integer> mSpaceValueMap;

    public static final String TOP_DECORATION = "top_decoration";
    public static final String BOTTOM_DECORATION = "bottom_decoration";
    public static final String LEFT_DECORATION = "left_decoration";
    public static final String RIGHT_DECORATION = "right_decoration";

    public RecyclerViewSpacesItemDecoration(HashMap<String, Integer> mSpaceValueMap) {
        this.mSpaceValueMap = mSpaceValueMap;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view,
                               RecyclerView parent, RecyclerView.State state) {
        if (mSpaceValueMap.get(TOP_DECORATION) != null)
            outRect.top = mSpaceValueMap.get(TOP_DECORATION);
        if (mSpaceValueMap.get(LEFT_DECORATION) != null)

            outRect.left = mSpaceValueMap.get(LEFT_DECORATION);
        if (mSpaceValueMap.get(RIGHT_DECORATION) != null)
            outRect.right = mSpaceValueMap.get(RIGHT_DECORATION);
        if (mSpaceValueMap.get(BOTTOM_DECORATION) != null)
            outRect.bottom = mSpaceValueMap.get(BOTTOM_DECORATION);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 RecyclerView.ItemDecoration 来设置每个元素之间间隔。你可以创建一个类,继承 RecyclerView.ItemDecoration,并在其中重写 getItemOffsets() 方法,该方法会在 RecyclerView 绘制每个 item 时调用。在该方法中,你可以设置每个 item间隔。 以下是一个示例: ``` public class SpaceItemDecoration extends RecyclerView.ItemDecoration { private int space; public SpaceItemDecoration(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; } else { outRect.top = 0; } } } ``` 在上面的示例中,我们创建了一个 SpaceItemDecoration 类,并在构造函数中传入每个 item 之间间隔。然后,在 getItemOffsets() 方法中,我们设置每个 item 的左、右、上和下的间隔。注意,我们只添加了第一个 item 的顶部间隔,以避免相邻两个 item 之间有双倍的间隔。 最后,将 SpaceItemDecoration 应用到 RecyclerView 中: ``` int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing); recyclerView.addItemDecoration(new SpaceItemDecoration(spacingInPixels)); ``` 在上面的示例中,我们将间隔大小作为像素传递给 SpaceItemDecoration,并将其应用到 RecyclerView 中。你可以在 dimens.xml 文件中定义 spacing 的值,以便在不同的设备上进行适配。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值