List组件可以下拉上滑,期望实现以下效果:
可以使用onTouch来监听下拉/上滑手势操作,并通过监听下拉距离用来计算是否展开,具体实现如下:
.onTouch((event: TouchEvent) => { if (!this.isAnimating) { // 非动画中 const currentY = event.touches[0].y; switch (event.type) { case TouchType.Down: // 记录当前触摸位置和图片初始高度 this.touchStartY = currentY; this.touchStartHeight = this.imageHeight; break; case TouchType.Move: // 根据图片展开状态和触摸移动的距离调整图片高度 const deltaY = currentY - this.touchStartY; if (this.isExpanded) { if (deltaY >= 0) { return; } else { const newHeight = Math.max(this.touchStartHeight + deltaY, 0); if (Math.abs(newHeight - this.imageHeight) > 1) { this.imageHeight = newHeight; } } } else { if (this.startIndex === 0 && deltaY > 0) { const newHeight = Math.min(deltaY, this.maxImageHeight); if (Math.abs(newHeight - this.imageHeight) > 1) { this.imageHeight = newHeight; } } } break; case TouchType.Up: // 手指抬起,判断当前组件展开的高度进行完全展开或收起动作 const movedDistance = Math.abs(currentY - this.touchStartHeight); // 移动距离小于5不做操作 const isMove = movedDistance > 5; if (isMove) { this.isAnimating = true; this.getUIContext()?.animateTo({ duration: 400, curve: curves.cubicBezierCurve(0.2, 0.8, 0.1, 1.0), onFinish: () => { this.isAnimating = false; } }, () => { if (this.imageHeight > this.threshold) { this.imageHeight = this.maxImageHeight; this.isExpanded = true; } else { this.imageHeight = 0; this.isExpanded = false; } }); } break; } } })
完整示例参考如下:
import curves from '@ohos.curves'; @Entry @Component struct ListDownDemo { @State imageHeight: number = 0; // 图片容器高度 @State startIndex: number = 0; // 列表起始索引 @State isExpanded: boolean = false; // 图片展开状态 private maxImageHeight: number = 240; // 图片最大高度 private touchStartY: number = 0; // 触摸起始位置 private threshold: number = 120; // 触发阈值 private isAnimating: boolean = false; // 是否正在动画中 private touchStartHeight: number = 0; // 触摸图片起始高度 build() { Column() { Column() { Text('下拉组件内容区') .fontSize(20) } .width('100%') .backgroundColor('#f1f3f5') .height(this.imageHeight) .clip(true) .margin({ bottom: 10 }) .align(Alignment.Bottom) .justifyContent(FlexAlign.End) .transition(TransitionEffect.opacity(1).animation({ duration: 200 })) List() { ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], (item: number) => { ListItem() { Row() { Column() { Text(`组件二内容: ${item} `) .fontSize(14) .opacity(0.7) } .layoutWeight(1) .margin({ right: 10 }) } .padding(8) .backgroundColor('#FFFFFF') .height(100) .margin({ top: 8, bottom: 8, left: 16, right: 16 }) .shadow({ radius: 4, color: '#00000020', offsetX: 1, offsetY: 1 }) } }) } .width('100%') .height('100%') .enableScrollInteraction(this.isExpanded ? false : true) .onScrollIndex((start: number) => { this.startIndex = start; }) .layoutWeight(1) .edgeEffect(EdgeEffect.None) } .onTouch((event: TouchEvent) => { if (!this.isAnimating) { // 非动画中 const currentY = event.touches[0].y; switch (event.type) { case TouchType.Down: // 记录当前触摸位置和图片初始高度 this.touchStartY = currentY; this.touchStartHeight = this.imageHeight; break; case TouchType.Move: // 根据图片展开状态和触摸移动的距离调整图片高度 const deltaY = currentY - this.touchStartY; if (this.isExpanded) { if (deltaY >= 0) { return; } else { const newHeight = Math.max(this.touchStartHeight + deltaY, 0); if (Math.abs(newHeight - this.imageHeight) > 1) { this.imageHeight = newHeight; } } } else { if (this.startIndex === 0 && deltaY > 0) { const newHeight = Math.min(deltaY, this.maxImageHeight); if (Math.abs(newHeight - this.imageHeight) > 1) { this.imageHeight = newHeight; } } } break; case TouchType.Up: // 手指抬起,判断当前组件展开的高度进行完全展开或收起动作 const movedDistance = Math.abs(currentY - this.touchStartHeight); // 移动距离小于5不做操作 const isMove = movedDistance > 5; if (isMove) { this.isAnimating = true; this.getUIContext()?.animateTo({ duration: 400, curve: curves.cubicBezierCurve(0.2, 0.8, 0.1, 1.0), onFinish: () => { this.isAnimating = false; } }, () => { if (this.imageHeight > this.threshold) { this.imageHeight = this.maxImageHeight; this.isExpanded = true; } else { this.imageHeight = 0; this.isExpanded = false; } }); } break; } } }) } }