9.ArkUI List的介绍和使用

ArkUI List 组件详解与使用指南

List 是 ArkUI(HarmonyOS 开发框架)中用于展示长列表数据的高性能滚动容器组件。以下是 List 的详细介绍和使用方法。

基本介绍

List 组件特点:

  • 支持垂直/水平滚动
  • 高性能渲染(仅渲染可视区域内的项)
  • 支持多种布局方式
  • 内置多种滑动操作和交互效果

基本使用

1. 简单列表

typescript 复制代码
@Entry
@Component
struct SimpleListExample {
  private data: string[] = ['Apple', 'Banana', 'Orange', 'Pear', 'Grape']

  build() {
    Column() {
      List({ space: 10 }) {
        ForEach(this.data, (item: string) => {
          ListItem() {
            Text(item)
              .fontSize(20)
              .width('100%')
              .textAlign(TextAlign.Center)
              .backgroundColor('#f0f0f0')
              .padding(10)
          }
        }, (item: string) => item)
      }
      .width('100%')
      .height('100%')
    }
  }
}

2. 复杂列表项

typescript 复制代码
@Entry
@Component
struct ComplexListExample {
  private contacts = [
    { name: '张三', phone: '13800138000', avatar: 'user1.png' },
    { name: '李四', phone: '13900139000', avatar: 'user2.png' },
    // 更多数据...
  ]

  build() {
    List({ space: 5 }) {
      ForEach(this.contacts, (contact) => {
        ListItem() {
          Row() {
            Image(contact.avatar)
              .width(50)
              .height(50)
              .borderRadius(25)
            
            Column() {
              Text(contact.name)
                .fontSize(18)
                .fontWeight(FontWeight.Bold)
              Text(contact.phone)
                .fontSize(14)
                .fontColor('#666')
            }.margin({ left: 10 })
          }
          .width('100%')
          .padding(10)
        }
      }, contact => contact.phone)
    }
    .width('100%')
    .height('100%')
  }
}

核心功能

1. 列表方向

typescript 复制代码
List() {
  // 列表项
}
.layoutDirection(Axis.Vertical) // 垂直列表(默认)

List() {
  // 列表项
}
.layoutDirection(Axis.Horizontal) // 水平列表

2. 列表分隔线

typescript 复制代码
List() {
  // 列表项
}
.divider({
  strokeWidth: 1,
  color: '#f0f0f0',
  startMargin: 20,
  endMargin: 20
})

3. 列表滚动控制

typescript 复制代码
@State scroller: Scroller = new Scroller()

build() {
  List({ scroller: this.scroller }) {
    // 列表项
  }
  
  // 滚动到指定位置
  Button('滚动到底部')
    .onClick(() => {
      this.scroller.scrollTo({ x: 0, y: 1000 })
    })
}

4. 下拉刷新和上拉加载

typescript 复制代码
@State isRefreshing: boolean = false
@State isLoadingMore: boolean = false

build() {
  List() {
    // 列表项
  }
  .onScrollIndex((start, end) => {
    // 滚动到接近底部时触发加载更多
    if (end >= this.data.length - 5) {
      this.loadMore()
    }
  })
  .refresh({
    refreshing: this.isRefreshing,
    onRefresh: () => {
      this.refreshData()
    }
  })
}

private refreshData() {
  this.isRefreshing = true
  // 模拟异步请求
  setTimeout(() => {
    this.isRefreshing = false
  }, 1000)
}

private loadMore() {
  if (this.isLoadingMore) return
  this.isLoadingMore = true
  // 模拟异步加载
  setTimeout(() => {
    // 添加新数据
    this.isLoadingMore = false
  }, 1000)
}

性能优化

1. 复用标识

typescript 复制代码
ForEach(this.data, (item) => {
  ListItem() {
    // 内容
  }
}, (item) => item.id) // 提供唯一键提高复用效率

2. 懒加载

typescript 复制代码
ListItem() {
  LazyForEach(this.dataSource, (item) => {
    // 列表项内容
  }, (item) => item.id)
}

3. 固定高度

typescript 复制代码
ListItem() {
  // 内容
}
.height(80) // 指定固定高度提高性能

高级功能

1. 分组列表

typescript 复制代码
@Entry
@Component
struct SectionListExample {
  private sections = [
    {
      title: 'A',
      data: ['Apple', 'Apricot', 'Avocado']
    },
    {
      title: 'B',
      data: ['Banana', 'Blackberry', 'Blueberry']
    }
    // 更多分组...
  ]

  build() {
    List() {
      ForEach(this.sections, (section) => {
        ListItemGroup({ header: this.renderHeader(section.title) }) {
          ForEach(section.data, (item) => {
            ListItem() {
              Text(item)
                .padding(10)
            }
          })
        }
      })
    }
  }

  @Builder renderHeader(title: string) {
    Text(title)
      .fontSize(18)
      .fontWeight(FontWeight.Bold)
      .backgroundColor('#f0f0f0')
      .width('100%')
      .padding(10)
  }
}

2. 滑动操作

typescript 复制代码
ListItem() {
  // 主内容
}
.swipeAction({ 
  end: this.DeleteButton(),
  start: this.MarkButton() 
})

@Builder DeleteButton() {
  Button('删除')
    .width(80)
    .height('100%')
    .backgroundColor(Color.Red)
    .onClick(() => {
      // 删除操作
    })
}

@Builder MarkButton() {
  Button('标记')
    .width(80)
    .height('100%')
    .backgroundColor(Color.Green)
    .onClick(() => {
      // 标记操作
    })
}

最佳实践

  1. 避免复杂嵌套:减少列表项的嵌套层级
  2. 使用固定尺寸:尽可能为列表项指定固定高度/宽度
  3. 分页加载:大数据集采用分页加载
  4. 图片优化:使用缩略图或懒加载大图片
  5. 减少状态更新:避免频繁更新列表数据

通过合理使用 List 组件,可以构建出高性能、流畅滚动的列表界面。

相关推荐
0xCC说逆向10 分钟前
Windows逆向工程提升之二进制分析工具:HEX查看与对比技术
汇编·windows·单片机·嵌入式硬件·安全·pe结构·pe文件
正经教主1 小时前
【基础】Windows开发设置入门3:在 Windows 11 上设置开发驱动器,提升性能速度
windows·开发环境·开发驱动器
Xam_d_LM1 小时前
【Linux】如何清除 Ubuntu 留下的 EFI 分区,Windows 磁盘管理器右键删除卷是灰色
linux·运维·windows·ubuntu·重装系统·磁盘管理器
gyeolhada1 小时前
2025蓝桥杯JAVA编程题练习Day8
java·数据结构·算法·蓝桥杯
m0_738206542 小时前
嵌入式学习的第二十三天-数据结构-树+哈希表+内核链表
数据结构·学习
freyazzr2 小时前
Leetcode刷题 | Day60_图论06
数据结构·c++·算法·leetcode·图论
霜羽68922 小时前
【数据结构篇】排序1(插入排序与选择排序)
数据结构·算法·排序算法
freyazzr2 小时前
Leetcode刷题 | Day64_图论09_dijkstra算法
数据结构·c++·算法·leetcode·图论
shandianchengzi3 小时前
【工具】Windows|外接的显示器怎么用软件调亮度(Brightness Slider)
windows·计算机外设·显示器·工具·软件
L_cl3 小时前
【Python 算法零基础 4.排序 ② 冒泡排序】
数据结构·python·算法