从零开始仿抖音做一个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加载是否可以更流畅等),有待后续优化。

相关推荐
不要卷鸿蒙啊8 小时前
【鸿蒙开发】HMRouter一款和好用的管理路由三方工具
前端·harmonyos
chenyingjian9 小时前
鸿蒙|性能优化-渲染丢帧优化
harmonyos
ujainu11 小时前
在 HarmonyOS PC 上实现自定义窗口样式的 Electron 应用详解
华为·electron·harmonyos
ujainu12 小时前
Electron 极简时钟应用开发全解析:托盘驻留、精准北京时间与 HarmonyOS PC 适配实战
javascript·electron·harmonyos
盐焗西兰花12 小时前
鸿蒙学习实战之路-Share Kit系列(10/17)-目标应用接收分享(应用内处理)
学习·华为·harmonyos
江湖有缘12 小时前
基于开发者空间部署OtterWiki知识管理工具【华为开发者空间】
华为
大雷神13 小时前
HarmonyOS APP<玩转React>开源教程八:主题系统实现
react.js·开源·harmonyos
fei_sun13 小时前
【鸿蒙智能硬件】(六)使用鸿蒙app展示环境监测数据
华为·harmonyos
懒洋洋在睡觉15 小时前
鸿蒙 6.0横屏显示时画面旋转错误
华为·图形渲染·harmonyos
键盘鼓手苏苏15 小时前
Flutter 组件 reaxdb_dart 适配鸿蒙 HarmonyOS 实战:响应式 NoSQL 数据库,构建高性能本地持久化与分布式状态同步架构
flutter·harmonyos·鸿蒙·openharmony·reaxdb_dart