场景一
基于nestedScroll实现WaterFlow与Scroll混合滑动。
方案:
外层scroll滑动,内层waterFlow滑动,外层使用嵌套属性.nestedScroll进行联动。
核心代码
Scroll() {
Column({ space: 2 }) {
Image($r('app.media.dzdp'))
.width('100%')
.height(200)
.objectFit(ImageFit.Cover)
WaterFlow() {
LazyForEach(this.dataSource, (item: number) => {
FlowItem() {
Column() {
Text("旅游景点" + item).fontSize(12).height('16')
Image($r(`app.media.img_${item % 10}`))
.objectFit(ImageFit.Fill)
.width('100%')
.layoutWeight(1)
}
}
.width('100%')
.height(this.itemHeightArray[item % 100])
.backgroundColor(this.colors[item % 5])
}, (item: string) => item)
}
.columnsTemplate("1fr 1fr")
.columnsGap(10)
.rowsGap(5)
.backgroundColor(0xFAEEE0)
.width('100%')
.height('100%')
.nestedScroll(
{
scrollForward: NestedScrollMode.SELF_FIRST, // 往末尾端滚动
scrollBackward: NestedScrollMode.SELF_FIRST // 往起始端滚动
})
}
}
场景二
list中嵌套List滑动。
方案:
外层list组件和内层list组件基于.nestedScroll进行联动。
核心代码
@Component
struct ItemCompponet {
build() {
Column() {
header({ title: this.title })
List() {
LazyForEach(this.generateDataSource(), (data: string, index) => {