【入门到精通】鸿蒙next应用开发:tabContent内容在tabBar上显示并响应滑动事件实现案例

 往期鸿蒙5.0全套实战文章必看:(文中附带鸿蒙5.0全栈学习资料)


tabContent内容可以在tabBar上显示并响应滑动事件案例

介绍

本示例实现了tabContent内容可以在tabBar上显示并且tabBar可以响应滑动事件的功能。

效果图预览

使用说明

  1. 点击播放按钮进行视频播放,按住进度条按钮和进度条下方区域可以拖动进度条,更改视频播放进度。

实现思路

原生的Tabs组件,tabContent内容无法在tabBar上显示。本案例实现tabContent内容可以在tabBar上显示并且tabBar可以响应滑动事件的功能 主要是通过将Tabs组件的barHeight设置为0,重新自定义tabBar。 将TabContent的内容分为上下两部分,上半部高度为100% - 60vp,存放video组件, 下部分高度为60vp,存放进度条。将Tabs组件的zIndex属性设置为2,tabContent的视图就可以堆叠在自定义tabBar之上。再设置hitTestBehavior属性 使被覆盖的tabBar可以响应点击事件。这样就实现tabBar可以响应滑动事件并且tabBar可以响应点击事件的效果。

  1. 创建Tabs组件,将barHeight设置为0。 
Tabs({ index: this.index, controller: this.tabsController }) {
   ...
}
// TODO: 知识点:将zIndex设置为2,TabContent将在tabBar之上,显示的效果就是TabContent外溢的部分在tabBar上。
.zIndex(CONFIGURATION.TABCONTENTOVERFLOWZINDEX)
.scrollable(false)
.barHeight($r('app.integer.tabcontentoverflow_tabs_barheight'))
.animationDuration(CONFIGURATION.TABCONTENTOVERFLOWTABSDURATION)
.onChange((index: number) => {
   this.index = index;
})
// TODO: 知识点:hitTestBehavior属性可以实现在复杂的多层级场景下,一些组件能够响应手势和事件,而一些组件不能响应手势和事件。HitTestMode.Transparent的效果为,自身响应触摸测试,不会阻塞兄弟节点的触摸测试。
.hitTestBehavior(HitTestMode.Transparent)
.id('tabs')
.alignRules({
   top: { anchor: STRINGCONFIGURATION.TABCONTENTOVERFLOWCONTAINER, align: VerticalAlign.Top },
   left: { anchor: STRINGCONFIGURATION.TABCONTENTOVERFLOWCONTAINER, align: HorizontalAlign.Start },
})
  1. 创建自定义tabBar。 
 Row() {
   ForEach(this.tabArray, (item: string, index: number) => {
      Column() {
         Image(this.index === index ? $r(this.imageClickArray[index]) : $r(this.imageArray[index]))
            .width($r('app.integer.tabcontentoverflow_row_column_image_width'))
            .height($r('app.integer.tabcontentoverflow_row_column_image_height'))
         Text($r(item))
            .fontSize($r('app.integer.tabcontentoverflow_row_column_text_font_size'))
            .fontColor(this.index === index ? $r('app.color.tabcontentoverflow_click_color') : $r('app.color.tabcontentoverflow_white'))
      }
      .width($r('app.integer.tabcontentoverflow_row_column_width'))
         .margin({ top: $r('app.integer.tabcontentoverflow_margin_samll') })
         // 为将底部视图扩展到非安全区域,可将原本60vp的高度设置为100vp。
         .height($r('app.integer.tabcontentoverflow_row_column_height'))
         .onClick(() => {
            this.index = index;
            this.tabsController.changeIndex(this.index);
         })
   })
 }
 .offset({
    y: $r('app.integer.tabcontentoverflow_row_offset')
 })
 .width($r('app.string.tabcontentoverflow_full_size'))
 // 扩展至所有非安全区域
 .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.BOTTOM])
 .backgroundColor($r('app.color.tabcontentoverflow_row_background'))
 .justifyContent(FlexAlign.SpaceAround)
 .id(STRINGCONFIGURATION.TABCONTENT_OVERFLOW_TABBAR)
 .alignRules({
    top: { anchor: STRINGCONFIGURATION.TABCONTENT_OVERFLOW_CONTAINER, align: VerticalAlign.Bottom },
    left: { anchor: STRINGCONFIGURATION.TABCONTENT_OVERFLOW_CONTAINER, align: HorizontalAlign.Start },
 })
  1. 将TabContent的内容分为上下两部分,上半部高度为100% - 60vp,存放video组件,下部分高度为60vp,存放进度条,设置滑动手势响应事件。 
RelativeContainer()
{
   Video({
      ...
   })
   .height($r('app.string.tabcontentoverflow_video_height'))
   ...
   
   RelativeContainer() {
   Text()
      ...
   Text()
      ...
   Text()
      ...
}
.id(STRINGCONFIGURATION.TABCONTENT_OVERFLOW_RELATIVE_CONTAINER)
.alignRules({
   top: { anchor: STRINGCONFIGURATION.TABCONTENT_OVERFLOW_VIDEO, align: VerticalAlign.Bottom },
   left: { anchor: STRINGCONFIGURATION.TABCONTENT_OVERFLOW_CONTAINER, align: HorizontalAlign.Start },
})
.width(this.screenW)
.height($r('app.integer.tabcontentoverflow_tabbar_height'))
// 左右拖动触发该手势事件
.gesture(
   PanGesture(this.panOption)
      .onActionStart(() => {
         ...
      })
      /**
       * TODO: 性能知识点: onActionUpdate是系统高频回调函数,避免在函数中进行冗余或耗时操作,例如应该减少或避免在函数打印日志,会有较大的性能损耗。
       * 合理使用系统接口,避免冗余操作:https://gitee.com/harmonyos-cases/cases/blob/master/docs/performance/README.md
       */
      .onActionUpdate((event: GestureEvent) => {
         ...
      })
      .onActionEnd(() => {
         ...
      })
)
}
  1. 将Tabs组件的zIndex属性设置为2,tabContent的视图就可以堆叠在自定义tabBar之上。 
Tabs({ index: this.index, controller: this.tabsController }) {
   TabContent() {
      ...
   }
   ...

   TabContent() {
      this.videoTabContent();
   }

   TabContent() {
      ...
   }
   ...

   TabContent() {
      ...
   }
   ...
}
// TODO: 知识点:将zIndex设置为2,TabContent将在tabBar之上,显示的效果就是TabContent外溢的部分在tabBar上。
.zIndex(CONFIGURATION.TABCONTENT_OVERFLOW_ZINDEX)
...
  1. 再设置hitTestBehavior属性使被覆盖的自定义的tabBar可以响应点击事件。
Tabs({ index: this.index, controller: this.tabsController }) {
   ...
}
// TODO: 知识点:hitTestBehavior属性可以实现在复杂的多层级场景下,一些组件能够响应手势和事件,而一些组件不能响应手势和事件。HitTestMode.Transparent的效果为,自身响应触摸测试,不会阻塞兄弟节点的触摸测试。
.hitTestBehavior(HitTestMode.Transparent)

工程结构&模块类型

   dragtoswitchpictures                             // har包
   |---common
   |   |---Constants.ets                            // 常量类
   |---mainpage
   |   |---TabContentOverFlow.ets                   // 主页面
   |---view
   |   |---Side.ets                                 // 视频信息以及顶部视图

在移动应用开发中,`TabContent`显示在`tabBar`之上的问题通常是由于布局配置不当引起的。以下是一些常见的原因和解决方法: ### 常见原因 1. **布局层级问题**:`TabContent`的布局层级高于`tabBar`。 2. **Z轴顺序问题**:某些框架或库中,组件的渲染顺序可能导致`TabContent`显示在`tabBar`之上。 3. **样式冲突**:样式设置可能导致`TabContent`覆盖了`tabBar`。 ### 解决方法 1. **调整布局层级**: - 确保`tabBar`的布局层级高于`TabContent`。在大多数框架中,可以通过调整XML或HTML中的元素顺序来实现。 ```xml <!-- 示例:Android布局 --> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/tabContent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <!-- Tab内容 --> </FrameLayout> <com.google.android.material.tabs.TabLayout android:id="@+id/tabBar" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- Tab项 --> </com.google.android.material.tabs.TabLayout> </LinearLayout> ``` 2. **调整Z轴顺序**: - 在某些框架中,可以通过设置`z-index`来控制元素的堆叠顺序。 ```css /* 示例:CSS样式 */ .tabBar { z-index: 1; } .tabContent { z-index: 0; } ``` 3. **检查样式冲突**: - 确保没有样式设置导致`TabContent`覆盖了`tabBar`。例如,`position: absolute`可能会导致覆盖问题。 ### 示例代码 以下是一个简单的示例,展示如何在Android实现`TabContent`在`tabBar`之下的布局: ```xml <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/tabContent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <!-- Tab内容 --> </FrameLayout> <com.google.android.material.tabs.TabLayout android:id="@+id/tabBar" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- Tab项 --> </com.google.android.material.tabs.TabLayout> </LinearLayout> ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值