HarmonyOS listview怎么进入就是从最底显示的,加入新的也是在最下边显示

HarmonyOS
2024-12-25 12:09:01
388浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

参考示例如下:

import { common } from '@kit.AbilityKit';

@Entry
@Component
struct ListExample {
  @State arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  private scrollerForList: Scroller = new Scroller()
  private context = getContext(this) as common.UIAbilityContext;

  showView(): void {
    this.context.eventHub.on('myEvent', (): void => this.eventFunc());
  }

  eventFunc() {
    let len = this.arr.length
    this.arr.push(len)
    this.scrollerForList.scrollToIndex(len, true)
  }

  build() {
    Column() {
      Stack() {
        List({ space: 20, initialIndex: this.arr.length - 1, scroller: this.scrollerForList }) {
          ForEach(this.arr, (item: number) => {
            ListItem() {
              Text('' + item)
                .width('100%')
                .height(100)
                .fontSize(16)
                .textAlign(TextAlign.Center)
                .borderRadius(10)
                .backgroundColor(0xFFFFFF)
            }
          }, (item: string) => item)
        }
        .listDirection(Axis.Vertical) // 排列方向
        .scrollBar(BarState.Off)
        .friction(0.6)
        .divider({
          strokeWidth: 2,
          color: 0xFFFFFF,
          startMargin: 20,
          endMargin: 20
        }) // 每行之间的分界线
        .edgeEffect(EdgeEffect.Spring) // 边缘效果设置为Spring
        .width('90%')
        Button('新加一个').onClick(() => {
          this.context.eventHub.emit('myEvent');
        })
      }
    }.onAppear(() => {
      this.showView()
    })
    .width('100%')
    .height('100%')
    .backgroundColor(0xDCDCDC)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
分享
微博
QQ
微信
回复
2024-12-25 15:36:38


相关问题
如何把导航默认底下显示
402040浏览 • 1回复 待解决
k8s 加入 node 后 pod 调度问题
2556浏览 • 1回复 待解决
HarmonyOS C++日志输出怎么Log中显示
1373浏览 • 1回复 待解决
HarmonyOS 怎么高亮显示搜索文字
791浏览 • 1回复 待解决
怎么判断webview滚动到最下方?
870浏览 • 2回复 待解决
HarmonyOS Tab控件bar怎么居左显示
489浏览 • 1回复 待解决
Text怎么显示带html标签文本
5322浏览 • 1回复 待解决
怎么给组件设置下边框?
7441浏览 • 1回复 待解决
Fluttertoast无法HarmonyOS显示
1020浏览 • 2回复 待解决
恭喜您,今日已阅读两篇内容,特奖励+2声望, 快来领取吧。