RecyclerView设置Item的边距

一,通过继承RecyclerView.ItemDecoration

RecyclerView有三种布局即:LinearLayoutManager 线性布局、StaggeredGridLayoutManager瀑布流布局、GridLayoutManager网格布局。LinearLayoutManager布局和其他两种布局实现设置RecyclerView的Item间距不太一样;以下分别给出实现方法;

1.1,LinearLayoutManager 布局

实现方式1:上下左右间距可以分开设置,也可以设置一样的参数

a,继承RecyclerView.ItemDecoration重写getItemOffsets方法

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private HashMap<String, Integer> spaceValue;
      
        public static final String TOP_SPACE = "top_space";
        public static final String BOTTOM_SPACE = "bottom_space";
        public static final String LEFT_SPACE = "left_space";
        public static final String RIGHT_SPACE = "right_space";

        public SpacesItemDecoration(HashMap spaceValue) {
            this.spaceValue = spaceValue;          
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            if (spaceValue.get(LEFT_SPACE) != null)
                outRect.left = spaceValue.get(LEFT_SPACE);
            if (spaceValue.get(RIGHT_SPACE) != null)
                outRect.right = spaceValue.get(RIGHT_SPACE);
            if (spaceValue.get(BOTTOM_SPACE) != null)
                outRect.bottom = spaceValue.get(BOTTOM_SPACE);
            if (spaceValue.get(TOP_SPACE) != null)
                outRect.top = spaceValue.get(TOP_SPACE);
        }
    }

b,设置item间距

HashMap<String, Integer> spacesVelue = new HashMap<>();
spacesVelue.put(SpacesItemDecoration.TOP_SPACE, 10);
spacesVelue.put(SpacesItemDecoration.BOTTOM_SPACE, 20);
spacesVelue.put(SpacesItemDecoration.LEFT_SPACE, 0);
spacesVelue.put(SpacesItemDecoration.RIGHT_SPACE, 0);
recyclerView.addItemDecoration(new SpacesItemDecoration(spacesVelue));

实现方式2:设置是否包含四周边距;上下左右间距可以分开设置,也可以设置一样的参数;

a,继承RecyclerView.ItemDecoration重写getItemOffsets方法

public class SpacesItemDecoration2 extends RecyclerView.ItemDecoration {
        private HashMap<String, Integer> spaceValue; //间隔
        private boolean includeEdge; //是否包含四周边距

        public static final String TOP_SPACE = "top_space";
        public static final String BOTTOM_SPACE = "bottom_space";
        public static final String LEFT_SPACE = "left_space";
        public static final String RIGHT_SPACE = "right_space";
        public SpacesItemDecoration2(HashMap spaceValue, boolean includeEdge) {
            this.spaceValue = spaceValue;
            this.includeEdge = includeEdge;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
            if (includeEdge) {
                if (spaceValue.get(LEFT_SPACE) != null)
                    outRect.left = spaceValue.get(LEFT_SPACE);
                if (spaceValue.get(RIGHT_SPACE) != null)
                    outRect.right = spaceValue.get(RIGHT_SPACE);
                if (spaceValue.get(BOTTOM_SPACE) != null)
                    outRect.bottom = spaceValue.get(BOTTOM_SPACE);
                if (spaceValue.get(TOP_SPACE) != null)
                    outRect.top = spaceValue.get(TOP_SPACE);
            } else {
                if (layoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
                    if (parent.getChildAdapterPosition(view) == 0)
                        outRect.left = 0;
                    if (parent.getChildAdapterPosition(view) == layoutManager.getItemCount() - 1)
                        outRect.right = 0;
                }
                if (layoutManager.getOrientation() == LinearLayoutManager.VERTICAL) {
                    if (parent.getChildAdapterPosition(view) == 0)
                        outRect.top = 0;
                    if (parent.getChildAdapterPosition(view) == layoutManager.getItemCount() - 1)
                        outRect.bottom = 0;
                }
            }
        }
    }

b,设置间距:

HashMap<String, Integer> spacesVelue = new HashMap<>();
spacesVelue.put(SpacesItemDecoration.TOP_SPACE, 10); //item上边距
spacesVelue.put(SpacesItemDecoration.BOTTOM_SPACE, 20); //item下边距
spacesVelue.put(SpacesItemDecoration.LEFT_SPACE, 0); //item左边距
spacesVelue.put(SpacesItemDecoration.RIGHT_SPACE, 0); //item 右边距
recyclerView.addItemDecoration(new SpacesItemDecoration(spacesVelue,true)); //true代表有边距,false表示没有边距

1.2,网格布局和瀑布流布局解决办法:

    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {

        private HashMap<String, Integer> spaceValue; //间隔
        private boolean includeEdge; //是否包含四周边距
        private int spanCount; //列数


        public static final String TOP_SPACE = "top_space";
        public static final String BOTTOM_SPACE = "bottom_space";
        public static final String LEFT_SPACE = "left_space";
        public static final String RIGHT_SPACE = "right_space";

        public SpacesItemDecoration(int spanCount, HashMap<String, Integer> spaceValue, boolean includeEdge) {
            this.spanCount = spanCount;
            this.spaceValue = spaceValue;
            this.includeEdge = includeEdge;
        }

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

            //这里是关键,需要根据你有几列来判断
            int position = parent.getChildAdapterPosition(view); // item position
            int column = position % spanCount; // item column


            if (includeEdge) {
                outRect.left = spaceValue.get(LEFT_SPACE) - column * spaceValue.get(LEFT_SPACE) / spanCount; 
                outRect.right = (column + 1) * spaceValue.get(RIGHT_SPACE) / spanCount; 
                if (position < spanCount) { // top edge 判断是首行
                    outRect.top = spaceValue.get(TOP_SPACE);
                }
                outRect.bottom = spaceValue.get(BOTTOM_SPACE); // item bottom
            } else {
                outRect.left = column * spaceValue.get(LEFT_SPACE) / spanCount; 
                outRect.right = spaceValue.get(RIGHT_SPACE) - (column + 1) * spaceValue.get(RIGHT_SPACE) / spanCount; 
                if (position >= spanCount) {//不是首行
                    outRect.top = spaceValue.get(TOP_SPACE); // item top
                }
                //只有首行设置上边距
                //if (position >= spanCount) {//不是首行
                //   outRect.top = spaceValue.get(TOP_SPACE); // item top
                //}else {
                //   outRect.top = 20; //首行
                //}

                //只有下边距
//                if (position > (parent.getAdapter().getItemCount() - spanCount)-1) {
//                    outRect.bottom = 100;
//                }
            }
        }
    }

设置item边距:

HashMap<String, Integer> spacesVelue = new HashMap<>();
        spacesVelue.put(SpacesItemDecoration.TOP_SPACE, 10);
        spacesVelue.put(SpacesItemDecoration.BOTTOM_SPACE, 20);
        spacesVelue.put(SpacesItemDecoration.LEFT_SPACE, 0);
        spacesVelue.put(SpacesItemDecoration.RIGHT_SPACE, 0);

        recyclerView.addItemDecoration(new SpacesItemDecoration(2,spacesVelue, false));

本文参考:https://www.jianshu.com/p/e372cec819db

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
要动态设置 RecyclerViewitem 的边距,你可以创建一个自定义的 ItemDecoration,并在其中设置边距。以下是一个示例代码,展示了如何动态设置 RecyclerViewitem 的边距: 首先,创建一个自定义的 ItemDecoration 类,例如 CustomItemDecoration: ```java public class CustomItemDecoration extends RecyclerView.ItemDecoration { private int spacing; public CustomItemDecoration(int spacing) { this.spacing = spacing; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); // 设置边距 outRect.left = spacing; outRect.right = spacing; outRect.top = spacing; outRect.bottom = spacing; } } ``` 然后,在你的 Activity 或 Fragment 中,通过调用 RecyclerView 的 addItemDecoration 方法来应用这个自定义的 ItemDecoration: ```java int spacing = getResources().getDimensionPixelSize(R.dimen.item_spacing); // 获取边距的像素值 RecyclerView recyclerView = findViewById(R.id.recyclerView); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); RecyclerView.Adapter adapter = new YourAdapter(); recyclerView.setAdapter(adapter); recyclerView.addItemDecoration(new CustomItemDecoration(spacing)); ``` 在上述示例中,我们通过 getResources().getDimensionPixelSize(R.dimen.item_spacing) 方法获取了边距的像素值。你可以在 res/values/dimens.xml 文件中定义这个边距的尺寸。 最后,我们创建了一个 LinearLayoutManager,并将其设置RecyclerView 的布局管理器。然后,设置了适配器,并通过 addItemDecoration 方法将自定义的 ItemDecoration 应用到 RecyclerView 上。 这样就可以动态设置 RecyclerViewitem 的边距了。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ang_qq_252390816

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值