[原创]recyclerview实现多行分组 给分组块加圆角.

2024-05-04 10:48

本文主要是介绍[原创]recyclerview实现多行分组 给分组块加圆角.,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

如果用多个recyclerview 或者里面多套个adapter当我没说,这里的实现方式是通过.借助griviewmanager和item

效果图是要达到这样的.当然这也是我最后搞完的图.

29a5f9a7d0ecebe84ed456ae9ff84eb.jpg
需求:

标题作为1行 内容为3列任意行.
标题内容分组 给标题内容包裹区域加圆角.
只有内容无标题的分组 就给内容区域加圆角

遇到的问题

1.当内容未满3列,颜色为recyclerview颜色或背景色.
2.边距写的不对,导致内容被扭曲,某个item出现背景色而且未和其他行对其.
3.滑动的时候发现我绘制的颜色掉了(是我粗心用的i而不是适配器位置导致)
4.判断末尾行
如果是末尾行直接填充白色圆角
5.如果没有标题,直接给内容加上左 和上右圆角.
6.最底部的边距没得.

其它

直接什么都不做的时候,没有圆角,没有分割线.
设置内容item为3列,但是不满3列,会导致出现背景颜色,有些朋友可能直接设置recyclerview背景了,但是你怕是不要处理圆角了??

如何实现?

  1. 使用GridLayoutManager
    2.给GridLayoutManager 设置setSpanSizeLookup 如果是标题就合并 getSpanSize 表示合并的尺寸,如果是内容是3列,那么这里返回3.否则返回1 ,
  2. 设置ItemDecoration 用getItemOffsets 控制间隔 ,
  3. 设置ItemDecoration 用public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
int spanSize = layoutManager.getSpanSizeLookup().getSpanSize(childAdapterPosition);

主要用于判断当前组是不是0,最后一组我用的另外的方法,组0 index=0, 代表是头部,如果是标题就绘制整个,如果内容 index=0也绘制 ,额好像不需要用到spanGroupIndex我记得用到过得

int spanGroupIndex = layoutManager.getSpanSizeLookup().getSpanGroupIndex(childAdapterPosition, spanCount);
int spanIndex = layoutManager.getSpanSizeLookup().getSpanIndex(childAdapterPosition, spanCount); //比如第一个是标题 ,第二列是 3格子,标题的永远是0 ,分3列的则是0 12 这样的循环。groupindex就是 每一列的完毕  就算一组。

用于判断是否是最后一行

int itemCount = parent.getLayoutManager().getItemCount();

用于预测下一个是不是标题,是标题那么现在就应该开始绘制下半身的圆角,以及如果内容item列不满足总列数,就绘制弥补空白处.

int lastspanIndex = layoutManager.getSpanSizeLookup().getSpanIndex(itemCount-1, spanCount); //比如第一个是标题 ,第二列是 3格子,标题的永远是0 ,分3列的则是0 12 这样的循环。groupindex就是 每一列的完毕  就算一组。

下面是具体写完之后的代码.

绘制空缺块和圆角块.
下图红色代表填充的空缺快,最后一组的填充则是绿色 , 头部是蓝色圆角 , 非最后一组底部圆角填充了黄色,我这样做只是为了调试,弄完之后我就把颜色改成白色了..
经常用到的判断
判断当前是跨了几行,用于判断是否是标题

GridLayoutManager layoutManager = new GridLayoutManager(recyclerView.getContext(), spanCount, LinearLayoutManager.VERTICAL, false);recyclerView.setLayoutManager(layoutManager);layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {@Overridepublic int getSpanSize(int position) {int type = recyclerView.getAdapter().getItemViewType(position);if (position >= recyclerView.getAdapter().getItemCount()) {return NOT_FOUND_SPAN_SIZE;} else if (type == TYPE_MENU_GROUP_NAME) {return MENU_TITLE_SPAN_SIZE;} else if (type == TYPE_GROUP_DATA) {return 1;} else {return NOT_FOUND_SPAN_SIZE;//这是 decoration调用的。}}@Overridepublic int getSpanIndex(int position, int spanCount) {int spanIndex = super.getSpanIndex(position, spanCount);Log.w(TAG, "spanIndex" + spanIndex + ",pos" + position + ",spanCount:" + spanCount);return spanIndex;}@Overridepublic int getSpanGroupIndex(int adapterPosition, int spanCount) {int spanGroupIndex = super.getSpanGroupIndex(adapterPosition, spanCount);Log.w(TAG, "spanGroupIndex" + spanGroupIndex + ",adapterPosition" + adapterPosition + ",spanCount:" + spanCount);return spanGroupIndex;}});

绘制圆角和空缺快设置代码

for (int ix = 0; ix < childCount; ix++) {final View child = parent.getChildAt(ix);int position = parent.getChildAdapterPosition(child);GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();int spanCount = layoutManager.getSpanCount();//表示整个列表分多少列int spanSize = layoutManager.getSpanSizeLookup().getSpanSize(position);//表示当前格所占有的格子数int spanIndex = layoutManager.getSpanSizeLookup().getSpanIndex(position, spanCount); //这里参数是spanCount,不是spanSize,否则不对,比如第一个是标题 ,第二列是 3格子,标题的永远是0 ,分3列的则是0 12 这样的循环。groupindex就是 每一列的完毕  就算一组。if (BuildConfig.DEBUG) {
//                MyAppListAdapter adapter = (MyAppListAdapter) parent.getAdapter();
//                SubMenuItemI subMenuItemI = adapter.getData().get(position);
//                int spanSizeNext = layoutManager.getSpanSizeLookup().getSpanSize(position + 1);}if (spanSize != MENU_TITLE_SPAN_SIZE) {//普通item只会跨1个。 除非是菜单int spanGroupIndex = layoutManager.getSpanSizeLookup().getSpanGroupIndex(position, spanCount);int spanSizeNext = layoutManager.getSpanSizeLookup().getSpanSize(position + 1);if (spanIndex < MENU_TITLE_SPAN_SIZE - 1) {// MENU_TITLE_SPAN_SIZE 等于new GridManager里面的Count,这里用合并的数来判断也可以做,是否属于最后一个格子,但是最后一个格子又是否铺满整个屏幕是最后一个但是没有刚好没铺满,就把它给铺满。if (spanSizeNext == NOT_FOUND_SPAN_SIZE || spanSizeNext == MENU_TITLE_SPAN_SIZE) {//如果下一个不存在那么是最后一个,或者下一个是标题了。drawable = generateDrawable(-1);//补空缺boundAndDraw(c, child.getRight(), right, drawable, child.getTop(), child.getBottom());//这里没有任何圆角if (BuildConfig.DEBUG) {/*                MyAppListAdapter adapter = (MyAppListAdapter) parent.getAdapter();SubMenuItemI subMenuItemI = adapter.getData().get(position);Log.w(TAG, subMenuItemI + ",为最后一个前后面超出!spanIndex:" + spanIndex + ",spanSize:" + spanSize + ",height:" + child.getHeight());*/}}}if (spanSizeNext == NOT_FOUND_SPAN_SIZE || spanSizeNext == MENU_TITLE_SPAN_SIZE) {// 刚好铺满的那种 铺满还需要判断是否下一行是否是标题.//左下角圆角.drawable = generateDrawable(5);//,spanSizeNext == NOT_FOUND_SPAN_SIZE ?Color.GREEN:Color.YELLOW);int top = child.getTop() + child.getHeight();int bottom = (int) (top + radius);
//                            Log.w(TAG,"LEFT:"+left+",top:"+top+",right:"+right+",bottom:"+bottom);boundAndDraw(c, left, right, drawable, top, bottom);}if( spanGroupIndex==0&&position==0){//没有头部 的顶drawable = generateDrawable(4);int top = (int) (child.getTop() - radius);int bottom = child.getTop();boundAndDraw(c, left, right, drawable, top, bottom);}} else {//标题头drawable = generateDrawable(4);int top = (int) (child.getTop() - radius);int bottom = child.getTop();boundAndDraw(c, left, right, drawable, top, bottom);}}}private void debugDrwa(Canvas c, int left, int right, View child) {//以下计算主要用来确定绘制的位置final int top1 = child.getBottom() + 1;ColorDrawable drawable1 = new ColorDrawable(Color.BLUE);final int bottom = top1 + drawable1.getIntrinsicHeight();boundAndDraw(c, left, right, drawable1, top1, bottom);}/*** /外矩形 0左上、1右上、2 右下、3左下的圆角半 4 上 左和右边 5 ,下 左右。** @param type* @return*/private GradientDrawable generateDrawable(int type, int color) {this.color = color;return generateDrawable(type);}

测距代码 测距就是给view留空间,不留空间就绘制在一坨了.

int childAdapterPosition = parent.getChildAdapterPosition(view);GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();int spanCount = layoutManager.getSpanCount(); //构造时传递的参数.//DefaultSpanSizeLookupint spanSize = layoutManager.getSpanSizeLookup().getSpanSize(childAdapterPosition);int spanGroupIndex = layoutManager.getSpanSizeLookup().getSpanGroupIndex(childAdapterPosition, spanCount);if (spanSize == MENU_TITLE_SPAN_SIZE) { //标题才跨 3个。
//            if (needTop) {if (spanGroupIndex == 0) {outRect.top = (int) (getSpace() + radius);} else {outRect.top = (int) (getSpace() + (radius*2));}
//            }} else  {outRect.top = 0;outRect.left = 0;outRect.right = 0;if( spanGroupIndex==0){//没有头部 的顶outRect.top = (int) (getSpace()  + radius);}else{outRect.top = 0;}int spanIndex = layoutManager.getSpanSizeLookup().getSpanIndex(childAdapterPosition, spanCount); //比如第一个是标题 ,第二列是 3格子,标题的永远是0 ,分3列的则是0 12 这样的循环。groupindex就是 每一列的完毕  就算一组。int itemCount = parent.getLayoutManager().getItemCount();int lastspanIndex = layoutManager.getSpanSizeLookup().getSpanIndex(itemCount-1, spanCount); //比如第一个是标题 ,第二列是 3格子,标题的永远是0 ,分3列的则是0 12 这样的循环。groupindex就是 每一列的完毕  就算一组。if (childAdapterPosition>=itemCount-(lastspanIndex+1)) {//最后一行outRect.bottom= (int) (getSpace()+(radius/2));if(lastspanIndex==spanIndex&&(1+spanIndex!=MENU_TITLE_SPAN_SIZE)){
//                        outRect.right=30;//=parent.getRight()-parent.getPaddingRight();//写不写其实都一样,因为不绘制右边?不,应该说都铺满屏幕了,不怕控件没给够}//不能加right,会影响空间}}if (BuildConfig.DEBUG) {
//            int spanGroupIndex =;//1 23格子 或者 1列 都算一组,int spanIndex = layoutManager.getSpanSizeLookup().getSpanIndex(childAdapterPosition, spanCount); //比如第一个是标题 ,第二列是 3格子,标题的永远是0 ,分3列的则是0 12 这样的循环。groupindex就是 每一列的完毕  就算一组。/*       SubMenuItemI subMenuItemI = ((MyAppListAdapter) parent.getAdapter()).getData().get(childAdapterPosition);String title = subMenuItemI.getTitle();*/Log.w(TAG, "Decoration SpanIndex:" + spanIndex + ",groupindex:" + spanGroupIndex + ",spanSize:" + spanSize + ",model:" + "title" + ",adapterposition:" + childAdapterPosition);}

这篇关于[原创]recyclerview实现多行分组 给分组块加圆角.的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/959027

相关文章

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构