// 设置水平分割线

rv.setLayoutManager(new LinearLayoutManager(cxt));

rv.addItemDecoration(new GridItemDecoration(1, 0, cxt.getResources().getColor(R.color.gray_dim), true));

// 设置3列网格分割线

recyclerView.setLayoutManager(new GridLayoutManager(context, 3));

recyclerView.addItemDecoration(new GridItemDecoration(1, 1, mAct.getResources().getColor(R.color.gray_dim), true));

public class GridItemDecoration extends RecyclerView.ItemDecoration

{

private int mHorizonLineWidth;

private int mVerticalLineWidth;

private Drawable mDividerDrawable;

private boolean isDrawLastLine;

/**

* 构造分割线

*

* @param horizonLineWidth 水平分割线宽

* @param verticalLineWidth 垂直分割线宽

* @param dividerColor 分割线颜色

* @param isDrawLastLine 是否绘制最后一行分割线

*/

public GridItemDecoration(int horizonLineWidth, int verticalLineWidth, int dividerColor, boolean isDrawLastLine)

{

this.mHorizonLineWidth = horizonLineWidth;

this.mVerticalLineWidth = verticalLineWidth;

this.isDrawLastLine = isDrawLastLine;

this.mDividerDrawable = new ColorDrawable(dividerColor);

}

@Override

public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)

{

// drawHorizontal

int childCount = parent.getChildCount();

for (int i = 0; i < childCount; i++)

{

View child = parent.getChildAt(i);

if (!isDrawLastLine && isLastRaw(parent, i, getSpanCount(parent), childCount))

continue;

RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

final int left = child.getLeft() - params.leftMargin;

final int right = child.getRight() + params.rightMargin;

final int top = child.getBottom() + params.bottomMargin;

final int bottom = top + mHorizonLineWidth;

mDividerDrawable.setBounds(left, top, right, bottom);

mDividerDrawable.draw(c);

}

// drawVertical

int childCount1 = parent.getChildCount();

for (int i = 0; i < childCount1; i++)

{

final View child = parent.getChildAt(i);

if ((parent.getChildViewHolder(child).getAdapterPosition() + 1) % getSpanCount(parent) == 0)

continue;

final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

final int top = child.getTop() - params.topMargin;

final int bottom = child.getBottom() + params.bottomMargin + mHorizonLineWidth;

final int left = child.getRight() + params.rightMargin;

int right = left + mVerticalLineWidth;

mDividerDrawable.setBounds(left, top, right, bottom);

mDividerDrawable.draw(c);

}

}

@Override

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

{

int spanCount = getSpanCount(parent);

int childCount = parent.getAdapter().getItemCount();

int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();

if (itemPosition < 0)

return;

int column = itemPosition % spanCount;

int bottom;

int left = column * mVerticalLineWidth / spanCount;

int right = mVerticalLineWidth - (column + 1) * mVerticalLineWidth / spanCount;

if (isLastRaw(parent, itemPosition, spanCount, childCount))

bottom = isDrawLastLine ? mHorizonLineWidth : 0;

else

bottom = mHorizonLineWidth;

outRect.set(left, 0, right, bottom);

}

private int getSpanCount(RecyclerView parent)

{

int spanCount = -1;

RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();

if (layoutManager instanceof GridLayoutManager)

spanCount = ((GridLayoutManager) layoutManager).getSpanCount();

else if (layoutManager instanceof StaggeredGridLayoutManager)

spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();

return spanCount;

}

private boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount)

{

RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();

if (layoutManager instanceof GridLayoutManager)

{

int remainCount = childCount % spanCount;

if (remainCount == 0)

return pos >= childCount - spanCount;

else

return pos >= childCount - childCount % spanCount;

}

else if (layoutManager instanceof StaggeredGridLayoutManager)

{

int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();

if (orientation == StaggeredGridLayoutManager.VERTICAL)

{

// 水平滚动

int remainCount = childCount % spanCount;

if (remainCount == 0)

return pos >= childCount - spanCount;

else

return pos >= childCount - childCount % spanCount;

}

else // 垂直滚动

return (pos + 1) % spanCount == 0;

}

else if (layoutManager instanceof LinearLayoutManager)

{

return pos >= parent.getAdapter().getItemCount() - 1;

}

return false;

}

}

android 分割线 整洁,Android-RecyclerView分割线(水平/垂直/网格)相关推荐

  1. android view上下滚动条,Android自定义View六(ViewGroup水平垂直滚动实现类似支付宝年度账单的效果)...

    先看两张效果图 1.垂直滑动 onegif.gif 2.水平滑动 twoGIF.gif 先看使用方法 1.AndroidStudio 引入 Project.gradle repositories { ...

  2. android 水平方向瀑布流,Android RecyclerView(瀑布流)水平/垂直方向分割线

     Android RecyclerView(瀑布流)水平/垂直方向分割线 Android RecyclerView不像过去的ListView那样随意的设置水平方向的分割线,如果要实现Recycle ...

  3. Android 活用RecyclerView分割线

    1.ItemDecoration简介 Recyclerview是我们日常开发中使用频率比较高的的控件,而其中的ItemDecoration作为布局装饰又能很方便的帮助我们定义分割线,列表排行效果以及设 ...

  4. android 分割线布局,Android RecyclerView网格布局(支持多种分割线)详解(2)

    记录了下RecyclerView的使用方法,并且讲述了线性布局列表的使用方法,在此基础上加上了万能分割线,支持颜色分割线和图片分割线,同时支持对分割线设置线宽. 这篇是总结一下网格布局的使用,同样也支 ...

  5. Android零基础入门第65节:RecyclerView分割线开发技巧

    2019独角兽企业重金招聘Python工程师标准>>> 在上一期通过简单学习,已经领略到了RecyclerView的灵活性,当然都是一些最基础的用法,那么本期一起来学习Recycle ...

  6. Android移动开发之【Android实战项目】Recyclerview添加花色分割线

    最近在做项目的过程中发现干巴巴的Recyclerview真的不好看,这里讲一下怎么一句话加默认的分割线,并且改变分割线的样式. 文章目录 一.添加默认分割线 二.修改样式 三.设置方法 通过 setD ...

  7. android gridview行分割线,Android使用GridView实现表格分割线效果

    使用gridview实现表格分割线效果,网格布局表格布局也是可以实现的. 效果如下: 1.主函数代码: package com.example.qd.douyinwu; import android. ...

  8. Android史上最强分割线全攻略

    借鉴自 https://blog.csdn.net/megatronkings/article/details/52156312 说实话,分割线这个东西,真的太难太难了!!! 难在何处?难在用最对的方 ...

  9. Android布局之边框、分割线

    Android布局之边框.分割线 先上图,实现如下图样式 布局文件使用Linerlayout垂直布局即可,这里省略,主要需要添加如下样式: <?xml version="1.0&quo ...

最新文章

  1. VTK:可视化算法之CreateBFont
  2. UILabel简单高效实现圆角的方式
  3. Codeforces Round #727 (Div. 2) E. Game with Cards dp + 思维
  4. ACM模板——差分约束
  5. sklearn 特征选择与特征抽取 —— feature_selection、feature_extraction
  6. android studio for android learning (二十七) UI控件动态加载机制浅析
  7. 海康威视复赛题 --- 算法说明书
  8. iOS开发 字体适配
  9. 带“小弟”其实是一种投资
  10. 基于 Retina-GAN 的视网膜图像血管分割
  11. 前端开发入门 --摘自慕克网大漠穷秋
  12. OpenGL课程设计 光线追踪
  13. 基于Python-Pycharm的猴子摘桃小game
  14. 怎样在微信公众号发文件?
  15. 最新holer使用方法 如何使用外网访问自己主机的web应用
  16. 中国支付结算体系全貌
  17. 深圳打卡(四) 红树林与深圳湾公园
  18. C# JSon解析之三个库的性能对比
  19. 一个屌丝程序猿的人生(三十)
  20. 6论文降重小技巧(建议收藏)

热门文章

  1. “黑天鹅”与“灰犀牛”不能混为一谈,揭开数据保护的“隐秘角落”
  2. 解决绿联扩展坞插网线无法联网的问题
  3. Android支持的视频格式
  4. Zigbee之旅(二):第一个CC2430程序——LED灯闪烁实验
  5. mysql查询10分钟内的数据库_十分钟了结MySQL information_schema
  6. .NET Apache Spark做基于商品推荐系统如此简单
  7. 创维E900V22C、E900V22D_语音更新-ROOT权限_线刷和卡刷固件包
  8. 『常识』盈利模式、商业模式、经营模式的区别
  9. 报错 java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify @Bootstra
  10. python matplotlib画三维折线图