HarmonyOS 聊天列表组件?

聊天内容列表展示,最新内容在底部,下拉看历史消息,建议是用 list吗?初始化界面需要自己通过scroller控制滑动底部,还说有什么其他便捷API?

阅读 552
1 个回答

可以使用list组件进行列表展示,并通过list的initialIndex参数实现优先显示在底部的最新内容,示例demo如下:

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @State list: string[] = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11']

  build() {
    Column() {

      List({ space: 10, initialIndex: this.list.length - 1 }) {
        ForEach(this.list, (item: string) => {
          ListItem() {
            Text(item).fontSize(20).fontWeight(FontWeight.Bold).textAlign(TextAlign.Center)
          }.height(400).width('100%').backgroundColor(Color.Orange)
        })
      }
    }
    .width("100%")
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}