在HarmonyOS NEXT开发中Scroller无法滚动到顶部,底部输入框页面被顶上去?
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
private list: string[] = []
private scroller = new Scroller()
aboutToAppear(): void {
for (let i = 0; i < 200; i++) {
this.list.push(`选项${i}`)
}
}
build() {
Column() {
Text('我是固定标题')
.fontColor(Color.White)
.backgroundColor(Color.Red)
List({ scroller: this.scroller }) {
ForEach(this.list, (item: string) => {
ListItem() {
Text(item)
.padding(10)
}
})
}
.layoutWeight(1)
.onAppear(() => {
this.scroller.scrollEdge(Edge.Bottom)
this.scroller.scrollToIndex(this.list.length - 1)
this.scroller.scrollToIndex(this.list.length)
})
TextInput()
}
.height('100%')
.width('100%')
}
}
代码如上:
列表加载完数据后,需要滚动到底部,使用场景是聊天页面,使用 this.scroller.scrollEdge 和 this.scroller.scrollToIndex ,都没有滚动到底部;底部有个输入框,获取到焦点时,整个页面被顶上去了,怎么做到顶部控件固定,列表正常滚动显示
可以使用 initialIndex: this.list.length - 1 。如下:
你可以通过设置:
来解决,具体内容请看:https://developer.huawei.com/consumer/cn/doc/harmonyos-refere...