从零开始仿抖音做一个APP(首页顶部T标签添加&页面关联)

从零开始仿抖音做一个APP(首页顶部标签添加&页面关联)

本文主要介绍首页顶部标签相关布局及页面切换。

先看看首页布局:

首先一个顶部标签栏,底部一个Swiper,用于跟随顶部标签的变化而变化,同时支持页面滚动切换。此外对左右两个icon都进行了封装,对于重复使用的标签或者view都可以这样使用,如下图所示:

复制代码
@Builder
  iconTop(src: ResourceStr, clickevent: () => void) {
    Row() {
      Image(src)
        .width(22)
        .height(22)
    }
    .onClick(() => {
      //处理点击事件
    })
    .height(HomeConstant.HOME_TAB_HEIGHT)
    .width(36)
    .justifyContent(FlexAlign.Center)
  }

对于顶部标签的布局,本文使用了ForEach工具,每一个标签再次进行了封装,具体代码如下:

复制代码
 Flex({
          justifyContent: FlexAlign.SpaceBetween,
          space: { main: LengthMetrics.px(30), cross: LengthMetrics.px(30) }
        }) {
          ForEach(HomeConstant.HOME_TAB_ARRAY, (item: string, index: number) => {
            this.textTop(index, item)
          })
        }.layoutWeight(1)

        this.iconTop($r('app.media.home_search'), () => {
        })

text封装:

复制代码
@Builder
  textTop(index: number, name: string) {
    Column() {
      Text(name)
        .fontColor(this.currentIndex === index ? $r('app.color.home_tab_selected_color') :
        $r('app.color.home_tab_normal_color'))
        .fontSize(16)
        .fontWeight(this.currentIndex === index ? 500 : 400)
        .lineHeight(20)
        .margin({ top: 10, bottom: 8 })
      Divider()
        .strokeWidth(2)//后续再对此处宽度、动画做进一步处理
        .width(24)
        .color('#fff')
        .opacity(this.currentIndex === index ? 1 : 0)
    }
    .height('100%')
    .justifyContent(FlexAlign.Center)
    .layoutWeight(1)
    .onClick(() => {
      this.currentIndex = index
      this.swiperController.changeIndex(this.currentIndex)
    })
  }

接下来将顶部标签与swiper进行关联,根据标签变化动态更新UI。此处swiper的数据源swiperData需要转为IDataSource类型。

复制代码
Swiper(this.swiperController) {
        LazyForEach(this.swiperData, (item: string, index: number) => {
          if (index === 0) {
            ExperienceView()
          } else if (index === 1) {
            LongVideoView()
          } else if (index === 2) {
            HotView()
          } else if (index === 3) {
            FocusView()
          } else if (index === 4) {
            RecommendView()
          }
        }, (item: string) => item)
      }
      .loop(false)
      .onAnimationStart((index: number, targetIndex: number, extraInfo: SwiperAnimationEvent) => {
        this.currentIndex = targetIndex
        this.swiperController.changeIndex(targetIndex)
      })

如此便实现了首页各个标签页面的切换的加载,效果如下所示。

接下来就是完成各个子模块的页面绘制。

本项目为工作之余随手编写,有很多可继续完善的地方(比如顶部是否用Tabs组件、部分组件的数据能否进一步封装、UI加载是否可以更流畅等),有待后续优化。

相关推荐
风华圆舞2 小时前
Flutter + 鸿蒙 Intents Kit:页面直达能力的完整接入方案
flutter·ui·华为·harmonyos
三声三视2 小时前
Electron 在鸿蒙 PC 上跑 webview,我是怎么把首屏从 4.2s 干到 1.1s 的
华为·electron·harmonyos·鸿蒙
互联网散修4 小时前
鸿蒙实战:从0到1构建功能完备的搜索页面
华为·harmonyos
花椒技术4 小时前
RN 多包热更新实践:更新校验、运行时加载与 Bridge 缓存治理
react native·react.js·harmonyos
不喝水就会渴4 小时前
【共创季稿事节】HarmonyOS 7.0 时代的新基建 :DevEco CLI + Claude Code,鸿蒙 AI 开发的黄金搭档
人工智能·华为·harmonyos
星释4 小时前
鸿蒙智能体开发实战:2.创建单Agent
harmonyos·智能体
世人万千丶5 小时前
成语接龙小应用 - HarmonyOS ArkUI 开发实战-TextInput与List列表-PC版本
华为·list·harmonyos·鸿蒙·鸿蒙系统
TrisighT5 小时前
AI写鸿蒙UI:10个跑崩8个,剩下2个看运气
ai编程·harmonyos·arkts
伶俜665 小时前
鸿蒙原生应用实战(十八)ArkUI 记账本:SQLite 账单 + 图表统计 + 分类管理
jvm·sqlite·harmonyos
Davina_yu6 小时前
自定义弹窗:使用CustomDialogController实现复杂交互(27)
harmonyos·鸿蒙·鸿蒙系统