HarmonyOS 鸿蒙Next:Tabs+TabContent切换页面时可通过onVisibleAreaChange感知显示与隐藏
HarmonyOS 鸿蒙Next:Tabs+TabContent切换页面时可通过onVisibleAreaChange感知显示与隐藏
@Entry
@Component
struct Page32 {
@State fontColor: string = '#182431'
@State selectedFontColor: string = '#007DFF'
@State currentIndex: number = 0
private controller: TabsController = new TabsController()
@Builder tabBuilder(index: number, name: string) {
Column() {
Text(name)
.fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
.fontSize(16)
.fontWeight(this.currentIndex === index ? 500 : 400)
.lineHeight(22)
.margin({ top: 17, bottom: 7 })
Divider()
.strokeWidth(2)
.color('#007DFF')
.opacity(this.currentIndex === index ? 1 : 0)
}.width('100%')
}
build() {
Column() {
Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {
TabContent() {
Column().width('100%').height('100%').backgroundColor('#00CB87')
.onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => {
console.info('Test Text isVisible: ' + isVisible + ', currentRatio:' + currentRatio)
if (isVisible && currentRatio >= 1.0) {
console.info('首页显示了')
}
if (!isVisible && currentRatio <= 0.0) {
console.info('首页隐藏了')
}
})
}.tabBar(this.tabBuilder(0, '首页'))
TabContent() {
Column().width('100%').height('100%').backgroundColor('#007DFF')
.onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => {
console.info('Test Text isVisible: ' + isVisible + ', currentRatio:' + currentRatio)
if (isVisible && currentRatio >= 1.0) {
console.info('新闻页显示了')
}
if (!isVisible && currentRatio <= 0.0) {
console.info('新闻页隐藏了')
}
})
}.tabBar(this.tabBuilder(1, '新闻'))
TabContent() {
Column().width('100%').height('100%').backgroundColor('#FFBF00')
.onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => {
console.info('Test Text isVisible: ' + isVisible + ', currentRatio:' + currentRatio)
if (isVisible && currentRatio >= 1.0) {
console.info('我的页显示了')
}
if (!isVisible && currentRatio <= 0.0) {
console.info('我的页隐藏了')
}
})
}.tabBar(this.tabBuilder(2, '我的'))
}
.vertical(false)
.barMode(BarMode.Fixed)
.barWidth(360)
.barHeight(56)
.animationDuration(400)
.onChange((index: number) => {
this.currentIndex = index
})
.width(360)
.height(296)
.margin({ top: 52 })
.backgroundColor('#F1F3F5')
}.width('100%')
}
}
更多关于HarmonyOS 鸿蒙Next:Tabs+TabContent切换页面时可通过onVisibleAreaChange感知显示与隐藏的实战教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于HarmonyOS 鸿蒙Next:Tabs+TabContent切换页面时可通过onVisibleAreaChange感知显示与隐藏的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next系统中,Tabs与TabContent组件在进行页面切换时,确实可以通过onVisibleAreaChange
回调来感知内容的显示与隐藏状态。这是一个非常实用的功能,特别是在构建具有复杂页面切换逻辑的应用时。
onVisibleAreaChange
回调提供了一种机制,让开发者能够获取到组件当前可见的区域信息,包括可见区域的起始位置、尺寸以及相对于父容器的偏移量等。通过监听这些变化,开发者可以准确地判断某个TabContent是否正在被显示或隐藏,从而执行相应的逻辑操作,如加载数据、停止动画等。
具体实现上,你需要在TabContent组件中重写onVisibleAreaChange
方法,并在其中编写处理逻辑。当Tab被切换到前台时,该方法会被调用,并传入当前可见区域的信息。你可以根据这些信息来判断组件的显示状态,并据此执行相应的操作。
需要注意的是,onVisibleAreaChange
回调的触发频率可能会受到系统性能、页面复杂度等多种因素的影响,因此在实际开发中,应合理设计回调中的处理逻辑,避免对系统性能造成不必要的负担。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html