在 OpenHarmony(ArkUI) 里,并没有叫 ListView 的组件(那是 Android 的概念)。在 OpenHarmony 中常用的是:
下面给你一个常见“List + 搜索”的完整示例。
TextInput 输入关键字List + ForEach 显示过滤后的结果@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)
}
}
}
List() {
ListItem() { ... }
}
this.filteredData = this.allData.filter(...)
LazyForEach如果数据很多(上百条),建议用:
LazyForEach(dataSource, itemGenerator, keyGenerator)
✅ 搜索 + 分组
✅ 搜索 + 空状态提示
✅ 搜索 + 拼音 / 首字母
✅ 搜索 + 网络请求(防抖)
如果你:
可以直接告诉我,我给你更贴合的示例。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。