如何解决如何在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
}
}
您可以看看这个tutorial
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。