【HarmonyOS NEXT】TabContent滚动时,过渡阶段仍然显示当前界面的数据,如何处理

 

【关键字】

Tabs / TabContent / 滚动

【问题描述】

TabContent滚动时,过渡阶段仍然显示当前界面的数据,问题代码如下。

SplashPage:

@Entry
@Component
struct SplashPage {
  @StorageProp('currentBreakpoint') currentBreakpoint: string = 'sm';
  private breakpointSystem = new BreakpointSystem();
  private timeOutId: number = 0;
  private titles = ['1', '2', '3', '4']
  @State @Watch('onTabSelect') currentIndex: number = 0
  @State datas: Array<string> = ['A1', "B1", 'C1', 'D1']
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) {
      JtTabs({
        titles: this.titles,
        currentIndex: $currentIndex,
        tabContents: () => {
          this.customTabsContext()
        }
      }).width('100%').layoutWeight(1)
    }
    .height(StyleConstants.FULL_HEIGHT)
    .width(StyleConstants.FULL_WIDTH)
    .backgroundColor($r('app.color.page_background'))
  }

  @Builder
  customTabsContext() {
    List() {
      ForEach(this.datas, (item: string) => {
       ListItem(){
         Text(item).fontSize(20).height(45).width('100%')
       }
      })
    }.width('100%').height('100%').border({ color: '#F5F5F5', width: 1 })
  }
  aboutToAppear() {
  }
  onTabSelect() {
    if (this.currentIndex === 0) {
      this.datas = ['A1', "B1", 'C1', 'D1']
    } else if (this.currentIndex === 1) {
      this.datas = ['A2', "B2", 'C2', 'D2']
    } else if (this.currentIndex === 2) {
      this.datas = ['A3', "B3", 'C3', 'D3']
    }
  }
  aboutToDisappear() {
    this.breakpointSystem.unregister();
    clearTimeout(this.timeOutId);
  }
}

JtTabs:

@Component
export struct  JtTabs {
  @Link currentIndex: number
  @Prop titles: Array<string> = []
  @BuilderParam tabContents: (index: number) => void = defaultIndexBuilder
  private controller: TabsController = new TabsController()
  build() {
    Column() {
      Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
        ForEach(this.titles, (item: string, index: number) => {
          TabContent() {
            if (this.tabContents !== defaultIndexBuilder) {
              this.tabContents(index)
            } else {
              Column()
                .width('100%')
                .height('100%')
                .backgroundColor(Color.Red)
            }
          }
        })
      }
      .vertical(false)
      .animationDuration(300)
      .onChange((index: number) => {
        this.currentIndex = index
      })
    }.width('100%').height('100%')
  }
}

【解决方案】

这个现象的原因是Tabs里的TabContent实际为同一个对象,而这个对象的数据又由页签值Index控制,导致出现了页签切换完成后数据才变化的情况。

建议在构建Tabs时使用不同的对象作为TabContent,参考如下代码:

@Entry
@Component
export struct SplashPage {
  @StorageProp('currentBreakpoint') currentBreakpoint: string = 'sm';
  private titles = ['1', '2', '3']
  @State currentIndex: number = 0
  @State datas: Array<Array<string>> = [['A1', "B1", 'C1', 'D1'],['A2', "B2", 'C2', 'D2'],['A3', "B3", 'C3', 'D3']]
  private controller: TabsController = new TabsController()

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) {
      Column() {
        Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
          ForEach(this.titles, (item: string, index: number) => {
            TabContent() {
              List() {
                ForEach(this.datas[Number(item)-1], (item: string) => {
                  ListItem(){
                    Text(item).fontSize(20).height(45).width('100%')
                  }
                })
              }.width('100%').height('100%').border({ color: '#F5F5F5', width: 1 })
            }
          })
        }
        .vertical(false)
        .animationDuration(300)
        .onChange((index: number) => {
          this.currentIndex = index
        })
      }.width('100%').height('100%')
      .layoutWeight(1)
    }
    .height('100%')
    .width('100%')
    .backgroundColor(Color.Green)
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值