LinearLayoutManager
可以实现垂直或水平的线性布局
- 默认的垂直方向的线性布局
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
- 水平方向的现行布局
// 第三个参数用于指定是否从右到左布局,基本都是false
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
GridLayoutManager
网格样式的布局,GridLayoutManager继承自LinearLayoutManager
int spanCount = 3; // 指定每行/列内的item个数
GridLayoutManager layoutManager = new GridLayoutManager(this, spanCount);
recyclerView.setLayoutManager(layoutManager);
setSpanSizeLookup
用来设置每个item需要占用多少个格子,最多不能超过spanCount
int spanCount = 3;
GridLayoutManager layoutManager = new GridLayoutManager(this, spanCount);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return (position % spanCount) + 1;
}
});
StaggeredGridLayoutManager
瀑布流样式布局
int spanCount = 3; // 指定每行/列内的item个数
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(this, spanCount);
recyclerView.setLayoutManager(layoutManager);
setFullSpan
用来设置item是否占用整行/列,可以用来实现Header/Footer的效果
来源:CSDN
作者:20200316
链接:https://blog.csdn.net/weixin_46576403/article/details/104900835