温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

OpenHarmony ListView怎么搜素

发布时间:2026-11-01 02:20:49 来源:亿速云 阅读:87 作者:小樊 栏目:软件技术

在 OpenHarmony(ArkUI) 里,并没有叫 ListView 的组件(那是 Android 的概念)。在 OpenHarmony 中常用的是:

  • List(容器)
  • ForEach / LazyForEach(渲染数据)
  • 配合 TextInput 做搜索

下面给你一个常见“List + 搜索”的完整示例。


一、基本思路

  1. 用一个数组保存全部数据
  2. 用 TextInput 输入关键字
  3. 根据关键字过滤数组
  4. 用 List + ForEach 显示过滤后的结果

二、示例代码(ArkTS)

@Entry
@Component
struct SearchListExample {
  @State allData: string[] = [
    'Apple',
    'Banana',
    'Cherry',
    'Date',
    'Grape',
    'Lemon',
    'Mango'
  ]

  @State keyword: string = ''
  @State filteredData: string[] = []

  aboutToAppear() {
    this.filteredData = this.allData
  }

  // 搜索方法
  search(value: string) {
    this.keyword = value
    if (value === '') {
      this.filteredData = this.allData
    } else {
      this.filteredData = this.allData.filter(item =>
        item.toLowerCase().includes(value.toLowerCase())
      )
    }
  }

  build() {
    Column() {
      // 搜索框
      TextInput({ placeholder: '请输入关键字' })
        .onChange((value: string) => {
          this.search(value)
        })
        .margin(10)

      // 列表
      List() {
        ForEach(this.filteredData, (item: string) => {
          ListItem() {
            Text(item)
              .fontSize(18)
              .padding(10)
          }
        }, (item: string) => item)
      }
      .width('100%')
      .layoutWeight(1)
    }
  }
}

三、关键点说明

1️⃣ List 是 OpenHarmony 的“ListView”

List() {
  ListItem() { ... }
}

2️⃣ 搜索本质是「数据过滤」

this.filteredData = this.allData.filter(...)

3️⃣ 大数据量用 LazyForEach

如果数据很多(上百条),建议用:

LazyForEach(dataSource, itemGenerator, keyGenerator)

四、常见扩展

✅ 搜索 + 分组
✅ 搜索 + 空状态提示
✅ 搜索 + 拼音 / 首字母
✅ 搜索 + 网络请求(防抖)

如果你:

  • 用的是 OpenHarmony 3.x / 4.x
  • 想做 下拉刷新 + 搜索
  • 或想用 Stage 模型 / FA 模型

可以直接告诉我,我给你更贴合的示例。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI
助
手