HarmonyOS 鸿蒙Next中如何禁止tabs的吸顶效果

HarmonyOS 鸿蒙Next中如何禁止tabs的吸顶效果 尝试使用了scroll 发现也有吸顶效果,怎么禁用这个吸顶效果呢

11 回复

【背景知识】 Scroll:可滚动的容器组件,当子组件的布局尺寸超过父组件的尺寸时,内容可以滚动。 nestedScroll:设置前后两个方向的嵌套滚动模式,实现与父组件的滚动联动。

【解决方案】 可能由于TabContent内部还嵌套了一个list导致的,滚动的时候优先滚动了list组件导致的。可以给TabContent的内容.nestedScroll,优先滚动自身组件避免吸顶效果。可以参考如下demo:

@Entry
@Component
struct ScrollerTest {
  subsController: TabsController = new TabsController()
  @State arr: string[] = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']
  @State textHeight: number = 200
  @State scrollLength: number = 0

  build() {
    Scroll() {
      Column() {
        Tabs({ barPosition: BarPosition.Start, controller: this.subsController }) {
          TabContent() {
            List({ space: 10 }) {
              ForEach(this.arr, (item: number) => {
                ListItem() {
                  Row() {
                    Text('item' + item)
                      .fontSize(16)
                      .height(72)
                      .fontColor(Color.Black)
                  }
                  .width('100%')
                  .height(72)
                  .justifyContent(FlexAlign.Center)
                }
                .borderRadius(15)
                .backgroundColor(Color.White)
              }, (item: string) => item)
            }
            .padding({ left: 10, right: 10 })
            .width('100%')
            .height('100%')
            .scrollBar(BarState.Off)
            .nestedScroll({
              scrollForward: NestedScrollMode.SELF_FIRST, // 向上滚动SELF_FIRST:自身组件先滚动,滚动到边缘以后父组件滚动。
              scrollBackward: NestedScrollMode.SELF_FIRST
            })
          }
          .tabBar('关注')
          .backgroundColor(Color.Gray)

          TabContent() {
            Text('推荐')
          }
          .tabBar('推荐')
          .backgroundColor(Color.White)
        }
        .backgroundColor('#6dbab8b8')
        //.padding({ top: this.bottomLength })
      }
      .width('100%')
    }
    .edgeEffect(EdgeEffect.Spring)
    .friction(0.6)
    .backgroundColor(Color.White)
    .scrollBar(BarState.Off)
    .width('100%')
    .height('100%')
    .onWillScroll((xOffset: number, yOffset: number) => {
      this.scrollLength += yOffset;
      if (this.scrollLength >= 100) {
        this.textHeight = 100
      }
      if (this.scrollLength < 100) {
        this.textHeight = 200
      }
    })
  }
}

更多关于HarmonyOS 鸿蒙Next中如何禁止tabs的吸顶效果的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


大佬,我tabContent 是空的.只是用一下系统的tabr,

那把这个demo里的tabContent内容置空试试,我这边测试是没有吸顶的。如果仍有问题麻烦提供一个能够复现问题的demo~,

cke_160.jpegcke_757.jpeg

我试了一下不会吸附呀

tabs哪里有吸顶效果,没明白

就是Tabs的最外层有scroll 滑动的时候 它会停留在最上边就像 list 的 stick 一样,

是因为tabs高度问题导致的吧,改一下高度试试,

HarmonyOS的开发者模式提供了很多实用的工具,方便我们进行调试和优化。

试了不行tab的 tabbar 在滑动到顶部的时候会自动吸附,

在HarmonyOS Next中,禁止Tabs组件的吸顶效果可通过设置属性实现。在Tabs组件中,将barMode属性设置为Fixed,同时确保未启用sticky属性。示例代码如下:

Tabs({ barPosition: BarPosition.Start }) {
  // TabContent内容
}
.barMode(BarMode.Fixed)

通过此配置可禁用滚动时的吸顶行为。

在HarmonyOS Next中,可以通过在Tabs组件中设置barMode属性为fixed来禁用吸顶效果。具体代码示例如下:

Tabs({
  barMode: BarMode.Fixed, // 设置为fixed模式,禁用吸顶
  controller: this.tabsController
}) {
  // TabContent内容
}

barMode设置为BarMode.Fixed后,Tabs栏会保持固定位置,不会随着页面滚动产生吸顶效果。

回到顶部