请参考示例:Tabs() { ForEach(this.tabBarArray, (tabsItem: NewsTypeModel) => { TabContent() { Column() { NewsList({ currentIndex: $currentIndex }) } } .tabBar(this.TabBuilder(tabsItem.id)) }, (tabsItem: NewsTypeModel, index?: number) => JSON.stringify(tabsItem) + index); } .barHeight(Const.TabBars_BAR_HEIGHT) .barMode(BarMode.Scrollable) .barWidth(Const.TabBars_BAR_WIDTH) .onChange((index: number) => { this.currentIndex = index; this.currentPage = 1; }) .vertical(false) .animationDuration(0) } 子组件-NewsList组件 @Component export default struct NewsList { @State newsModel: NewsModel = new NewsModel(); @Link currentIndex: number; aboutToAppear() { if(this.currentIndex == 0){ console.log("单独新闻接口0") }else if(this.currentIndex == 1){ console.log("单独新闻接口1") }else if(this.currentIndex == 2){ console.log("单独新闻接口3") }else if(this.currentIndex == 3){ console.log("单独新闻接口4") } } build() { Column() { if (this.newsModel.pageState === PageState.Success) { this.ListLayout() } else if (this.newsModel.pageState === PageState.Loading) { this.LoadingLayout() } else { this.FailLayout() } } .width(Const.FULL_WIDTH) .height(Const.FULL_HEIGHT) .justifyContent(FlexAlign.Center) .onTouch((event: TouchEvent | undefined) => { if (event) { if (this.newsModel.pageState === PageState.Success) { listTouchEvent(this.newsModel, event); } } }) } @Builder LoadingLayout() { CustomRefreshLoadLayout({ customRefreshLoadClass: new CustomRefreshLoadLayoutClass(true, $r('app.media.ic_pull_up_load'), $r('app.string.pull_up_load_text'), this.newsModel.pullDownRefreshHeight) }) } @Builder ListLayout() { List() { ListItem() { RefreshLayout({ refreshLayoutClass: new CustomRefreshLoadLayoutClass(this.newsModel.isVisiblePullDown, this.newsModel.pullDownRefreshImage, this.newsModel.pullDownRefreshText, this.newsModel.pullDownRefreshHeight) }) } ForEach(this.newsModel.newsData, (item: NewsData) => { ListItem() { NewsItem({ newsData: item }) } .height(Const.NewsListConstant_ITEM_HEIGHT) .backgroundColor($r('app.color.white')) .margin({ top: Const.NewsListConstant_ITEM_MARGIN_TOP }) .borderRadius(Const.NewsListConstant_ITEM_BORDER_RADIUS) }, (item: NewsData, index?: number) => JSON.stringify(item) + index) ListItem() { if (this.newsModel.hasMore) { LoadMoreLayout({ loadMoreLayoutClass: new CustomRefreshLoadLayoutClass(this.newsModel.isVisiblePullUpLoad, this.newsModel.pullUpLoadImage, this.newsModel.pullUpLoadText, this.newsModel.pullUpLoadHeight) }) } else { NoMoreLayout() } } } .width(Const.NewsListConstant_LIST_WIDTH) .height(Const.FULL_HEIGHT) .margin({ left: Const.NewsListConstant_LIST_MARGIN_LEFT, right: Const.NewsListConstant_LIST_MARGIN_RIGHT }) .backgroundColor($r('app.color.listColor')) .divider({ color: $r('app.color.dividerColor'), strokeWidth: Const.NewsListConstant_LIST_DIVIDER_STROKE_WIDTH, endMargin: Const.NewsListConstant_LIST_MARGIN_RIGHT }) // Remove the rebound effect. .edgeEffect(EdgeEffect.None) .offset({ x: 0, y: `${this.newsModel.offsetY}px` }) .onScrollIndex((start: number, end: number) => { // Listen to the first index of the current list. this.newsModel.startIndex = start; this.newsModel.endIndex = end; }) } @Builder FailLayout() { Image($r('app.media.none')) .height(Const.NewsListConstant_NONE_IMAGE_SIZE) .width(Const.NewsListConstant_NONE_IMAGE_SIZE) Text($r('app.string.page_none_msg')) .opacity(Const.NewsListConstant_NONE_TEXT_opacity) .fontSize(Const.NewsListConstant_NONE_TEXT_size) .fontColor($r('app.color.fontColor_text3')) .margin({ top: Const.NewsListConstant_NONE_TEXT_margin }) } }
请参考示例: