StaggeredGridLayoutManager似乎不允许自定义单元格宽度或跨越多个列(垂直方向除外)以进行垂直方向。
如何创建如上图所示的布局?在这种情况下,什么是完美的布局管理器... GridLayoutManager或StaggeredGridLayoutManager?
答案 0 :(得分:2)
您可以使用$ cat foo
1
1
1
2
2
3
而不是StaggeredGridLayoutManager
。要在行中使用不同的列数,您必须覆盖setSpanSizeLookup。
示例:
GridLayoutManager
请注意GridLayoutManager gridLayoutManager = new GridLayoutManager(getAppContext(), spanCount);
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
//define span size for this position
//for example, if you have 2 column per row, you can implement something like that:
if(position == youRule) {
return 2; //item will take 2 column (full row size)
} else {
return 1; //you will have 2 rolumn per row
}
}
});
< = spanSize
答案 1 :(得分:0)
好吧,我找到了解决方案!解决方案是: -
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
// if (position % 3 == 0) {
// Log.e("TAG", "Position: " + position +" position % 3= " + position % 3 + " return 2");
// return 2;
// } else {
// Log.e("TAG", "Position: " + position +" position % 3= " + position % 3 + " return 1");
// return 1;
// }
if (position % 3 == 0 || position % 3 == 1) {
Log.e("TAG", "Position: " + position +" position % 3= " + position % 3 + " return 1");
return 1;
} else {
Log.e("TAG", "Position: " + position +" position % 3= " + position % 3 + " return 2");
return 2;
}
// return (3 - position % 3);
}
});