HarmonyOS 鸿蒙Next tab页切换时如何定位

发布于 1周前 作者 eggper 来自 鸿蒙OS

HarmonyOS 鸿蒙Next tab页切换时如何定位

课程介绍】tab和【课程目录】tab的tabcontent是在一个页面中,

课程介绍】的tabcontent是【开发二部】和【课程介绍】,也就是下图的1和2

【课程目录】的tabcontent是【课程目录】list,也就是下图的3

我现在想要的效果是点击【课程目录】tab时,scroll能滚动到【课程目录】tab对应的内容也就是【课程目录11】位置处。这个如何实现?

image.png


更多关于HarmonyOS 鸿蒙Next tab页切换时如何定位的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复
滚动到指定位置之后,上方内容是否可以滚动回去,如还需滚动回去可以去掉多余content,使用list,点击课程目录时使用scrollToindex,文档位置,可以参考实现
tabConten本质在于切换内容,所以可能与你业务有冲突

更多关于HarmonyOS 鸿蒙Next tab页切换时如何定位的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


页面代码如下:

[@Entry](/user/Entry)

[@Component](/user/Component)

struct Index {

  [@State](/user/State) currentIndex: number = 0

  flag: boolean = true

  [@Builder](/user/Builder)

  tabBuilder(title: string, targetIndex: number) {

    Column() {

      Text(title).fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B')

    }.width('100%')

    .height(10)

    .justifyContent(FlexAlign.Center)

  }

  build() {

    Column() {

      Column() {

        Column() {

          Tabs({ barPosition: BarPosition.Start, index: this.currentIndex }) {

            TabContent() {

              CoursesContentDisplayView({ flagVisible: true });

            }.tabBar(this.tabBuilder('课程介绍', 0))

            TabContent() {

              CoursesContentDisplayView({ flagVisible: true });

            }.tabBar(this.tabBuilder('课程目录', 1))

            TabContent() {

              Column().width('100%').height('100%')

            }.tabBar(this.tabBuilder('我的3', 2))

          }.scrollable(false)

          .barMode(BarMode.Fixed)

          .onChange((index: number) => {

            this.currentIndex = index

            console.info(index.toString())

          })

          .width('100%')

        }.width('100%')

      }

    }

  }

}

[@Component](/user/Component)

struct CoursesContentDisplayView {

  [@State](/user/State) courseTypeList: Array<CoursesDetailsModel> = [

    new CoursesDetailsModel('课程目录11', '课程内容11'),

    new CoursesDetailsModel('课程目录12', '课程内容12'),

    new CoursesDetailsModel('课程目录13', '课程内容13'),

    new CoursesDetailsModel('课程目录14', '课程内容14'),

    new CoursesDetailsModel('课程目录15', '课程内容15'),

    new CoursesDetailsModel('课程目录16', '课程内容16'),

    new CoursesDetailsModel('课程目录17', '课程内容17'),

    new CoursesDetailsModel('课程目录18', '课程内容18'),

    new CoursesDetailsModel('课程目录19', '课程内容19'),

    new CoursesDetailsModel('课程目录20', '课程内容20'),

    new CoursesDetailsModel('课程目录21', '课程内容21'),

  ]

  [@State](/user/State) flagVisible: boolean = true;

  build() {

    Column() {

      Scroll() {

        Column() {

          Column() {

            Column() {

              Row() {

                Text('开发二部')

                  .width('90%')

                  .textAlign(TextAlign.Start)

                  .fontColor('#000000')

                  .fontSize(20)

                  .fontWeight(600)//FontWeight.Bold

                  .padding({ left: 12 })

              }

              .width('100%')

              .justifyContent(FlexAlign.Start)

              Text('13人参加')

                .width('100%')

                .fontSize(12)

                .textAlign(TextAlign.Start)

                .fontColor('rgba(0, 0, 0, 0.6)')

                .margin({ top: 15, bottom: 18 })

                .padding({ left: 12 })

            }

            .justifyContent(FlexAlign.Start)

            .width('100%')

            .border({ width: { bottom: 15 }, color: "#F0F0F0" })

            Column() {

              // 课程介绍

              Text('课程介绍')

                .width('100%')

                .textAlign(TextAlign.Start)

                .fontColor('#000000')

                .fontSize(15)

                .fontWeight(500)//FontWeight.Bold

                .margin({ top: 20 })

                .padding({ left: 12 })

              Text('课程介绍111')

                .width('100%')

                .fontSize(12)

                .textAlign(TextAlign.Start)

                .fontColor('rgba(0, 0, 0, 0.6)')

                .margin({ top: 15, bottom: 1 })

                .padding({ left: 12, right: 12 })

            }

            .justifyContent(FlexAlign.Start)

            .width('100%')

            .border({ width: { bottom: 15 }, color: "#F0F0F0" })

          }

          // 课程目录

          Column() {

            ForEach(this.courseTypeList, (item: CoursesDetailsModel) => {

              // 课程目录

              Text(item.title)

                .width('100%')

                .textAlign(TextAlign.Start)

                .fontColor('#000000')

                .fontSize(15)

                .fontWeight(500)//FontWeight.Bold

                .margin({ top: 20 })

                .padding({ left: 12 })

              Text(item.name)

                .width('100%')

                .fontSize(12)

                .textAlign(TextAlign.Start)

                .fontColor('rgba(0, 0, 0, 0.6)')

                .margin({ top: 15, bottom: 1 })

                .padding({ left: 12, right: 12 })

            })

          }

          .justifyContent(FlexAlign.Start)

          .width('100%')

        }

      }

      .scrollBar(BarState.On)

    }

    .justifyContent(FlexAlign.Start)

    .width('100%')

    .height('100%')

    .margin({ top: 10 })

  }

}

class CoursesDetailsModel {

  // 课程目录标题

  title: string = '';

  // 课程目录名称

  name: string = '';

  constructor(title: string, name: string) {

    this.title = title;

    this.name = name;

  }

}

针对HarmonyOS 鸿蒙Next tab页切换时如何定位的问题,以下是一些专业的解决方案:

在HarmonyOS 鸿蒙Next中,tab页切换时的定位通常涉及到页面导航和组件状态管理。首先,确保你的应用使用了合适的导航组件,如Navigation,它支持模块内和跨模块的路由切换,并能实现流畅的转场体验。

其次,对于Tabs组件,如果切换时遇到延迟或定位不准确的问题,可以尝试优化性能,确保应用及系统资源充足,并减少不必要的后台进程和服务。同时,调整动画效果,通过设置Tabs组件的animationDuration属性为0来关闭切换动画,以减少延迟。

此外,如果需要在tab页切换时保持某些组件的固定定位,可以使用Stack容器,通过zIndex属性控制组件的显示层级关系,实现类似CSS中fixed定位的效果。

最后,确保鸿蒙系统为最新版本,以便利用最新的性能优化和bug修复。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部