RecyclerView在GridLayoutManager情况下实现四周都有分割线的ItemDecoration

本文介绍了如何在RecyclerView使用GridLayoutManager时,实现四周都有分割线的效果。针对传统方法忽略边缘分割线的问题,作者分享了一个改造过的ItemDecoration方案,通过判断当前位置是否为第一行来设置额外的偏移量,确保分割线完整显示。
摘要由CSDN通过智能技术生成

感谢:http://www.jianshu.com/p/d58cbd61c40a _SOLID

之前UI有个比较特别的需求,一个gridview形式的列表,但是四周都有白色的分割线:
这里写图片描述

类似这个效果,一开始以为会比较简单,然后写了之后才发现,目前网上找到的大部分方法话分割线都是省略掉屏幕边缘的部分的,
即第一行 第一列 最后一行 最后一列的没有分割线的

http://blog.csdn.net/lmj623565791/article/details/45059587
如hongyang大神的这个文章中就是这种典型的gridview的ItemDecoration

于是在掘金和github找了很久,终于找到一个符合一般要求的—最后一行也与分割线,虽然第一行还是没有,不过既然最后一行都有了,第一韩有也不难了

http://www.jianshu.com/p/d58cbd61c40a
自定义ItemDecoration这个问题你真的注意到了吗—–_SOLID

这个博客贴出的ItemDecoration正好拿来改造一番

关键代码在于:

 private boolean isfirstRow(RecyclerView parent, int pos, int spanCount,
                               int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            // childCount = childCount - childCount % spanCount;
            int lines = childCount % spanCount == 0 ? childCount / spanCount : childCount / spanCount + 1;
             //如是第一行则返回true
            if ((pos / spanCount + 1) == 1) {
                return true;
            } else {
                return false;
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager)
                    .getOrientation();
            // StaggeredGridLayoutManager 且纵向滚动
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                childCount = childCount - childCount % spanCount;
                // 如果是最后一行,则不需要绘制底部
                if (pos >= childCount)
                    return true;
            } else {
                // 如果是最后一行,则不需要绘制底部
                if ((pos + 1) % spanCount == 0) {
                    return true;
                }
            }
     
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值