HarmonyOS ArkUi Tabs+TabContent+List实现tab吸顶功能

Demo效果

javascript 复制代码
@Entry
@Component
struct StickyNestedScroll {
  @State message: string = 'Hello World'
  @State arr: number[] = []
  scroller = new Scroller()

  @Styles
  listCard() {
    .backgroundColor(Color.White)
    .height(72)
    .width("100%")
    .borderRadius(12)
  }

  build() {
    Scroll(this.scroller) {
      Column() {
        Stack({ alignContent: Alignment.Top }) {
          Column() {

          }.height("200vp").width('100%').backgroundImage($r('app.media.icon_home_top'))
          // Text('吸顶').width('100%').height(40).backgroundColor(Color.Black).zIndex(999)
        }


        Tabs({ barPosition: BarPosition.Start }) {
          TabContent() {
            List({ space: 10 }) {
              ForEach(this.arr, (item: number) => {
                ListItem() {
                  Text("item" + item)
                    .fontSize(16)
                }.listCard()
              }, (item: string) => item)
            }.width("100%")
            .edgeEffect(EdgeEffect.Spring)
            .nestedScroll({
              scrollForward: NestedScrollMode.PARENT_FIRST,
              scrollBackward: NestedScrollMode.SELF_FIRST
            })
          }.tabBar("Tab1")

          TabContent() {
          }.tabBar("Tab2")
        }
        .vertical(false)
        .backgroundColor(Color.Brown)
        .height("100%")
      }.width("100%")
    }
    .edgeEffect(EdgeEffect.Spring)
    .friction(0.6)
    .backgroundColor('#DCDCDC')
    .scrollBar(BarState.Off)
    .width('100%')
    .height('100%')
  }

  aboutToAppear() {
    for (let i = 0; i < 30; i++) {
      this.arr.push(i)
    }
  }

  @Builder
  testHead() {
    Text('吸顶').width('100%').height(40).backgroundColor(Color.Black).zIndex(999)
  }
}

项目实战效果图

  • Tab:(企业统计)
javascript 复制代码
  @Builder
  TabWidget() {
    Row() {
      Text('企业统计')
        .fontColor($r('app.color.color303242')).fontSize(16).margin({ left: 12 })
    }
    .height('100%')
    .width('calc(100% - 30vp)')
    .borderRadius({ topLeft: 8, topRight: 8 })
    .backgroundColor(Color.White)
    .margin({ left: 15, right: 15 })
    .onClick(() => {
      this.scroller.scrollTo({ xOffset: 0, yOffset: 2000 })
    })
  }
  • 完整项目代码

仔细看代码里的注释

javascript 复制代码
@Entry
@Component
export struct HomePage {
  @State curIndex: number = 0
  scroller: Scroller = new Scroller()
  @State currentOffset: number = 0;
  @StorageLink('systemBarHeight') systemBarHeight: number = 0
  @State topOpacity: number = 1;
  @State searchHint: string = '请输入企业名称';
  @State banner?: BannerBean = new BannerBean()
  @State protMarketBean?: ProspectMarketBean[] = new Array<ProspectMarketBean>()

  aboutToAppear(): void {
 
  }

  build() {
    Stack({ alignContent: Alignment.Top }) {
      Scroll(this.scroller) {
        Column() {
          Column() {
           // tab 企业统计上面的组件   这里省略 l里面没内容的话 可以设置固定高度测试
           // ....
          }.width('100%')

          Tabs({ barPosition: BarPosition.Start }) {
            TabContent() {
            // 注意注意: 这里是tab 企业统计下面的界面  这里要用list
              List({ space: 10 }) {
                ListItem() {
                  Column() {
                    HomeWebWidget()
                    if (this.protMarketBean && this.protMarketBean.length > 0) {
                      HomeEnterpriseLib({ protMarketBean: this.protMarketBean }).margin({ top: 10 })
                    }
                  }.width('100%')
                }
              }.width("100%").height('100%')
              // edgeEffect  nestedScroll一定要设置
              .edgeEffect(EdgeEffect.Spring)
              .nestedScroll({
                scrollForward: NestedScrollMode.PARENT_FIRST,
                scrollBackward: NestedScrollMode.SELF_FIRST
              })
            }.tabBar(this.TabWidget())
          }
          .barWidth('100%')
          .barHeight(40)
          .vertical(false)
          .margin({ top: 10 })
           //60=搜索组件的高度 this.systemBarHeight= 状态栏的高度  这样吸顶才是刚好处于搜索组件下方  可自行调整
          .height(`calc(100% - ${60 + px2vp(this.systemBarHeight)}vp)`)
        }.width("100%")
        .backgroundImage($r('app.media.icon_home_top'))
        .backgroundImageSize({ width: '100%', height: '50%' })
      }
      .onScroll(() => {
      	// 滚动监听  根据偏移量 顶部搜索栏的界面效果
        this.currentOffset = this.scroller.currentOffset().yOffset;
        // 根据偏移控制透明度
        this.topOpacity = (140 - this.currentOffset) / 140; 例
      })
      .edgeEffect(EdgeEffect.Spring)
      .friction(0.6)
      .backgroundColor('#F3F3F3')
      .scrollBar(BarState.Off)
      .width('100%')
      .height('100%')
		// 滚动偏移量大于0时  显示顶部搜索
      if (this.currentOffset > 0) {
        this.SearchHeaderWidget()
      }
    }
  }

  @Builder
  TabWidget() {
    Row() {
      Text('企业统计')
        .fontColor($r('app.color.color303242')).fontSize(16).margin({ left: 12 })
    }
    .height('100%')
    .width('calc(100% - 30vp)') 
    .borderRadius({ topLeft: 8, topRight: 8 })
    .backgroundColor(Color.White)
    .margin({ left: 15, right: 15 })
    .onClick(() => {
      this.scroller.scrollTo({ xOffset: 0, yOffset: 2000 })
    })
  }

  @Builder
  SearchHeaderWidget() {

    Column() {
      Row() {
        Image($r('app.media.icon_search')).width(18).height(18).margin({ left: 15 })
        Text(this.searchHint).fontColor('#C6C9CF').fontSize(13).margin({ left: 8 })
      }
      .height(40)
      .width('100%')
      .backgroundColor(Color.White)
      .borderRadius(50)
      .onClick(() => {
        router.pushUrl({ url: RouterPath.SEARCH, params: { tabIndex: 1 } })
      })
      .margin({ top: px2vp(this.systemBarHeight) })
    }.backgroundColor('#0256FF')
    .padding({ left: 15, right: 15, bottom: 10, top: 10 })
    .opacity(1 - this.topOpacity) // 顶部搜索组件的透明度
  }
}
相关推荐
HarmonyOS_SDK19 分钟前
HiAI Foundation开发平台,加速端侧AI应用的智能革命
harmonyos
爱桥代码的程序媛9 小时前
鸿蒙开发设备管理:【@ohos.multimodalInput.touchEvent (触摸输入事件)】
程序员·harmonyos·鸿蒙·openharmony·设备管理·鸿蒙开发·输入事件
m0_6442226112 小时前
HarmonyOS开发实战:UDP通讯示例规范
网络·驱动开发·嵌入式硬件·udp·harmonyos·鸿蒙·harmonyos next
张紫娃12 小时前
【鸿蒙学习笔记】数据类型
笔记·学习·harmonyos
MobTech袤博科技16 小时前
ShareSDK HarmonyOS NEXT集成指南
java·服务器·华为·harmonyos
Android技术栈21 小时前
鸿蒙开发Ability Kit(程序框架服务):【向用户申请单次授权】
程序员·移动开发·harmonyos·鸿蒙系统·openharmony·鸿蒙开发·程序框架
m0_644222611 天前
HarmonyOS开发探索:自定义键盘-webview
前端·华为·计算机外设·移动开发·harmonyos·鸿蒙开发
MobTech袤博科技1 天前
MobPush HarmonyOS NEXT 版本集成指南
华为·harmonyos
软通动力1 天前
SwanLinkOS首批实现与HarmonyOS NEXT互联互通,软通动力子公司鸿湖万联助力鸿蒙生态统一互联
harmonyos·openharmony
tyrolin1 天前
鸿蒙OS开发者高级学习第2课:自由流转(含习题答案)
学习·华为·harmonyos