android - 如何在 GridLayoutManager 中准备 setSpanSizeLookup
问题描述
我需要每个 18,36,54,...18*Nth... 网格布局,宽度:匹配父级和高度:网格布局管理器中的 60dp 用于分页加载栏目的。
这是我的代码,
GridLayoutManager mng_layout = new GridLayoutManager(this, 4);//WHAT IS 4 ?//
mng_layout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
int mod = position % 6; //WHAT IS 6 ?//
if (position == 0 || position == 1)
return 2; //WHAT IS 2 ?//
else if (position < 6)
return 1; //WHAT IS 1 ?//
else if (mod == 0 || mod == 1)
return 2; //WHAT IS 2 ?//
else
return 1; //WHAT IS 1 ?//
}
});
如果有人有时间,请解释代码中的注释行
解决方案
跨度大小表示此项目从网格中占用多少个单元格
public int getSpanSize(int position) {
int mod = position % 6; //here 18%6=0 ,19%6=1 ,36%6=0 ,37%6=1 and so one
if (position == 0 || position == 1)
return 2; // if this is the first or sesond items make it take span size of 2 so 4/2 =2 cells will be shows
else if (position < 6)
return 1; // here 2,3,4,5 < 6 so every item should take span size 1 out of 4 , so we have 4 cells
else if (mod == 0 || mod == 1)
return 2; // here we check the mode which is defined above 18,19 & 36,37 ,54,55 and so on every pair will span 2 cells for every number
else
return 1; // any thing other like will take one span out of 4 grid columns
}
}
你可以看看这个教程
推荐阅读
- python - 树莓派上的“ValueError:负数返回字节(bytearray(seq))”
- python - 使用 REGEX PYTHON 的清洁列表
- amazon-web-services - AWS Fargate - 如何使用 python boto3 获取任务的公共 IP 地址
- android - 在 react native 上更改 FLIPPER_VERSION 会产生 BUILD FAILED 输出
- asp.net-core - Asp .Net Core - X-Paged-List 省略号问题
- python - 取消选择 tkinter 画布按钮
- javascript - React Admin:如何在编辑表单中显示 id 列表?
- java - 无法将地图列表插入到 Apache Cassandra
- windows - 在 Windows 命令提示符中使用 echo 清空文件内容
- javascript - 浏览器何时删除超时?