HarmonyOS DataSource数据发生变化,Tabs循环的tabContent未发生变化

代码如下:

@Preview
@Component
export struct TextPreLayout {
  testArry: string[] = ["页面1", "页面2", "页面3", "页面4"]
  @State dataSource: LazyDataSource<string> = new LazyDataSource();
  @State listArr: string[] = [];
  private tabController: TabsController = new TabsController();
  @State currentPage: number = 2;

  aboutToAppear() {
    for (let i = 0; i < this.testArry.length; i++) {
      this.dataSource.pushData(this.testArry[i]);
    }
  }

  changeData() {
    this.testArry = ["页面2", "页面1", "页面3", "页面4"]
    this.dataSource.clear();
    for (let i = 0; i < this.testArry.length; i++) {
      this.dataSource.pushData(this.testArry[i]);
    }
  }

  build() {
    Column() {

      Text("改变dataSource")
        .width(160)
        .height(30)
        .backgroundColor('#fcc')
        .fontColor('#f00')
        .onClick(() => {
          this.changeData()
        })
        .margin({ top: 40, left: 40, bottom: 10 })


      List() {
        LazyForEach(this.dataSource, (column: string, index: number) => {
          this.TabBuilder(column, index)
        }, (item: string) => JSON.stringify(item))
      }
      .edgeEffect(EdgeEffect.None)
      // 设置左对齐
      .align(Alignment.Start)
      .listDirection(Axis.Horizontal)
      .scrollBar(BarState.Off)
      .padding({ right: 5, left: 5 })
      .backgroundColor($r('app.color.uicp_FFFFFF_1A2026'))
      .width('100%')
      .height(32)


      Tabs({ index: this.currentPage }) {
        LazyForEach(this.dataSource, (column: string) => {
          TabContent() {
            ChannelTextIndexView({
              channelName: column
            })
          }
        }, (item: string) => JSON.stringify(item))
      }
      .barHeight(0)
      .barBackgroundColor($r('app.color.color_FFFFFF_1A2026'))
      .scrollable(true)
      .onChange((index) => {
        this.currentPage = index;
      })

    }

  }

  @Builder
  TabBuilder(name: string, index: number) {
    Text(name)
      .width(60)
      .height(30)
      .fontColor('#f00')
      .onClick(() => {
        console.debug('AAAA', "homeIndex TabBuilder onClick index=" + index);
        this.currentPage = index;
      })
      .fontSize(this.currentPage === index ? 18 : 16)
      .fontWeight(this.currentPage === index ? FontWeight.Bold :
      FontWeight.Normal)//设置文本的字体粗细,number类型取值[100, 900],取值间隔为100,取值越大,字体越粗,默认值:400 | FontWeight.Normal
      .fontColor(this.currentPage === index ? $r('app.color.uicp_E10000') : $r('app.color.uicp_333333_6B758E'))
  }
}
@Component
export struct ChannelTextIndexView {
  channelName: string = "";

  aboutToAppear(): void {
  }

  build() {
    Column() {
      Text(this.channelName)
      Text(this.channelName)
      Text(this.channelName)
      Text(this.channelName)
      Text(this.channelName)
      Text(this.channelName)
      Text(this.channelName)
      Text(this.channelName)
    }.width("100%").height("100%")
  }
}
HarmonyOS
2024-12-25 14:03:09
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

数据庞大复杂时可能会出现性能问题,Tabs组件不支持LazyForEach(实际上也是一次性加载),所以实际渲染应该和ForEach一样,如果遇到加载缓慢可以尝试优化数据源或数据结构,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-rendering-control-lazyforeach-V5#使用限制

分享
微博
QQ
微信
回复
2024-12-25 16:21:41
相关问题
HarmonyOS 页面高度发生变化
418浏览 • 1回复 待解决