场景描述
场景一:瀑布流页面多列混排的布局场景,例如:10个item在2列内布局,中间5个item在1列内撑满宽度布局,后10个item在3列内布局。
场景二:瀑布流页面中某一个Item可以固定展示在某一个位置,从而实现吸顶效果
方案描述
场景一:
waterFlow支持自定义Item,从而在WaterFlow内完成多列混排的自定义布局,同时懒加载保证性能。
方案
通过sections配置分组信息,并初始化瀑布流分组,通过splice替换/添加新分组。
核心代码
1、计算FlowItem宽/高,设置FlowItem的宽/高数组,通过对应Item,设置FlowItem的宽/高。
2、通过sections配置分组信息,并初始化瀑布流分组,具体根据 SectionOptions需要自定义。可通过Item和分组分组信息中itemsCount设置指定Item的布局,例如Item=5,第一个分组中itemsCount: 4,Item=5为第二个分组的第二个Item(此处Item从0开始)。
3、即将触底时提前增加数据,添加加新分组到瀑布流末尾。
//计算FlowItem宽/高
getSize() {
let ret = Math.floor(Math.random() * this.maxSize)
return (ret > this.minSize ? ret : this.minSize)
}
// 设置FlowItem的宽/高数组
setItemSizeArray() {
for (let i = 0; i < 100; i++) {
this.itemWidthArray.push(this.getSize())
this.itemHeightArray.push(this.getSize())
}
}
//配置分组信息,可通过Item和分组信息中itemsCount设置指定Item的布局,例如Item=5,第一个分组中为itemsCount: 4,Item=5为第二个分组的第二个Item(此处Item从0开始)
@State sections: WaterFlowSections = new WaterFlowSections()
oneColumnSection: SectionOptions = {
itemsCount: 4,//分组中FlowItem数量,必须是正整数。
crossCount: 1,//纵向布局时为列数,横向布局时为行数
columnsGap: 5,//该分组的列间距,不设置时使用瀑布流的columnsGap
rowsGap: 10,//该分组的行间距,不设置时使用瀑布流的rowsGap
margin: { top: 10, left: 5, bottom: 10, right: 5 },
onGetItemMainSizeByIndex: (index: number) => {//瀑布流组件布局过程中获取指定index的FlowItem的主轴大小
return 150
}
}
twoColumnSection: SectionOptions = {
itemsCount: 20,
crossCount: 2,
onGetItemMainSizeByIndex: (index: number) => {
return this.itemHeightArray[index 100]
}
}
//初始化分组信息
aboutToAppear() {
this.setItemSizeArray()
let sectionOptions: SectionOptions[] = []
let count = 0
let oneOrTwo = 0
while (count