我们在HarmonyOS开发中,如何一个带有搜索功能的列表?

阅读 654
1 个回答

可以使用TextField组件与ListView组件结合来实现带有搜索功能的列表。

@Entry
@Component
struct SearchableListAbility {
  @State private searchText: string = '';
  @State private filteredItems: string[] = [];

  build() {
    Column() {
      TextField(this.searchText).placeholder('Search...');
      ListView(this.filteredItems)
        .itemCreator((item) => Text(item).fontSize(18))
        .itemSize(50);

      // Simulate filtering logic
      this.filteredItems = this.searchText ? ['Item 1', 'Item 2', 'Item 3'].filter((item) => item.includes(this.searchText)) : ['Item 1', 'Item 2', 'Item 3'];
    }
  }
}

参见:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides...

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进