鸿蒙开发:案例集合List:模拟附近人列表插入

🎯 案例集合List:模拟附近人列表插入

🌍 案例集合List

🚪 最近开启了学员班级点我欢迎加入

🏷️ 效果图(录制有点掉帧)

📖 参考

🧩 拆解

  • 从列表前面插入(后续会增加高性能的方式)
javascript 复制代码
@Component
export struct ListItemUnshiftAnimation {
  /**
   * 模拟数据
   */
  @State mockData: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  /**
   * 监听是否滑动到List顶部
   */
  @State @Watch('isListTopChange') isListTop: boolean = false
  /**
   * 定时器ID
   */
  @State timerId: number = -1

  /**
   * 滑动到头开启延时器追加数据
   */
  isListTopChange() {
    if (this.isListTop) {
      this.timerId = setInterval(() => {
        this.getUIContext().animateTo({ duration: 300, curve: Curve.Smooth }, () => {
          this.mockData.unshift(this.mockData.length + 1)
        })
      }, 2000)
    }
  }

  /**
   * 模拟Swipe组件或者banner组件
   */
  @Builder
  mockSwipeBuilder() {
    Stack({ alignContent: Alignment.Center }) {
      Image($r('app.media.startIcon'))
        .width('100%')
        .height(200)
        .draggable(false)
        .borderRadius(10)
      Text('模拟Swipe组件')
        .fontSize(20)
        .fontWeight(FontWeight.Bold)
    }
    .height(200)
    .width('100%')
  }

  aboutToDisappear(): void {
    this.isListTop = false // 关闭追加数据
    clearInterval(this.timerId) // 清空延时器
  }

  build() {
    Column() {
      this.mockSwipeBuilder()

      List({ space: 20 }) {
        ListItem() // 这里处理是否滑动到顶部占位组件
        ForEach(this.mockData, (item: number) => {
          ListItem() {
            Text(item.toString())
              .height(150)
              .width('100%')
              .borderRadius(10)
              .backgroundColor(Color.Orange)
              .textAlign(TextAlign.Center)
          }
          // 170 = 150的组件高度 20的组件边距,这样设置不会有挤压 | 割裂的效果
          .transition(TransitionEffect.OPACITY.combine(TransitionEffect.translate({ y: -170 })))

          // 这里在实际业务中需要取字段中的唯一做标识,不然会有很奇怪的问题
        }, (item: number) => item.toString())
      }
      .cachedCount(1, true) // 预加载数量1,true代表需要显示
      .width('100%')
      .layoutWeight(1)
      .scrollBar(BarState.Off) // 关闭滚动条
      .onReachStart(() => this.isListTop = true) // 是否滑动到头
      .onScrollIndex(() => {
        this.isListTop = false // 关闭追加数据
        clearInterval(this.timerId) // 清空延时器
      })
    }
    .height('100%')
    .width('100%')
  }
}

🌸🌼🌺

相关推荐
程序猿追18 小时前
HarmonyOS 6 上做个极简浏览器:Web 组件 + 前进后退 + 加载进度
前端·华为·harmonyos
程序猿追18 小时前
HarmonyOS 6 音视频实战:用 AVPlayer 做一个本地音乐播放器
华为·音视频·harmonyos
威哥爱编程18 小时前
HarmonyOS 7 多设备 UX 自动检测实战:AppAnalyzer 多设备体检 + 一多工程底线,提前拦截截断重叠大图大字
harmonyos
威哥爱编程18 小时前
HarmonyOS 7 安全相机 2.0 实战:数字内容溯源,为拍摄内容提供来源保护
harmonyos
威哥爱编程18 小时前
HarmonyOS 7 弱网优化实战:Network Boost Kit 场景加速 + QUIC 长连接 + 弱网直播优化
harmonyos
威哥爱编程18 小时前
HarmonyOS 7 应用故障智能诊断实战:APMS 故障总览 + Profiler 跨语言泄漏 + Operation Analyzer 冻屏分析
harmonyos
暖风熏人醉18 小时前
Expo 鸿蒙双 SDK 时代:我把适配鸿蒙做成了工程
harmonyos
程序猿追19 小时前
HarmonyOS 6 毛玻璃卡片:背景模糊 + 半透明
华为·harmonyos
CV工程师丁Sir20 小时前
ArkWeb 手记 05|请求拦截:劫持 H5 接口与图片资源
华为·harmonyos
雨雨雨雨雨别下啦21 小时前
【OpenHarmony北向开发】配置环境
华为·harmonyos