感谢: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;
}
}