HarmonyOS 是否有新闻类频道导航栏案例

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

参考示例如下:

import { componentUtils, display } from '@kit.ArkUI';
import curves from '@ohos.curves';

@Entry
@Component
struct Index {
  @State text: string = ''
  textInputController: TextInputController = new TextInputController()
  @State tabArray: Array<number> = [0, 1, 2, 3, 4]
  @State focusIndex: number = 0
  @State pre: number = 0
  @State index: number = 0
  private controller: TabsController = new TabsController()
  @State test: boolean = false
  @State animationDuration: number = 300
  @State indicatorLeftMargin: number = 0
  @State indicatorWidth: number = 0
  private tabsWidth: number = 0
  private tabWidth: number = 0;
  private scrollController: Scroller = new Scroller();
  private animationCurve: ICurve = curves.interpolatingSpring(7, 1, 328, 34); // 动画曲线

  aboutToAppear(): void {

  }

  // 单独的页签
  @Builder
  Tab(tabName: string, tabItem: number, tabIndex: number) {
    Row({ space: 20 }) {
      Text(tabName)
        .fontSize(18)
        .fontColor(tabIndex === this.focusIndex ? Color.Blue : Color.Black)
        .id(tabIndex.toString())
        .onAreaChange((oldValue: Area, newValue: Area) => {
          if (this.focusIndex === tabIndex && (this.indicatorLeftMargin === 0 || this.indicatorWidth === 0)) {
            if (newValue.position.x != undefined) {
              let positionX = Number.parseFloat(newValue.position.x.toString())
              this.indicatorLeftMargin = Number.isNaN(positionX) ? 0 : positionX
            }
            let width = Number.parseFloat(newValue.width.toString())
            this.indicatorWidth = Number.isNaN(width) ? 0 : width
          }
        })
        .border(this.focusIndex == tabIndex ? {
          width: {
            left: 0,
            right: 0,
            top: 0,
            bottom: 2
          },
          color: { bottom: Color.Blue },
          radius: 0,
          style: {
            bottom: BorderStyle.Solid,
          }
        } : null)
    }
    .justifyContent(FlexAlign.Center)
    .constraintSize({ minWidth: 35 })
    .width(100)
    .height(30)
    .onClick(() => {
      console.debug("maotest onclick " + tabIndex)
      this.controller.changeIndex(tabIndex)
      this.focusIndex = tabIndex
    })
    .backgroundColor("#ffb7b7b7")
  }

  @Builder
  textTest(textName: string) {
    Row({ space: 10 }) {
      Text(textName).fontSize(18)
    }
    .justifyContent(FlexAlign.Center)
    .constraintSize({ minWidth: 20 })
    .height(30)
    .backgroundColor("#ffb7b7b7")
  }

  build() {
    Column() {
      Stack({ alignContent: Alignment.TopStart }) {
        Column() {
          // 页签
          Row({ space: 8 }) {
            Scroll(this.scrollController) {
              Row() {
                ForEach(this.tabArray, (item: number, index: number) => {
                  this.Tab("页签 " + item, item, index)
                })
              }
              .justifyContent(FlexAlign.Start)
            }
            .scrollable(ScrollDirection.Horizontal)
            .scrollBar(BarState.Off)
            .width('80%')
            .backgroundColor("#ffb7b7b7")
            .onScrollFrameBegin((offset: number) => {
              let modePosition: componentUtils.ComponentInfo =
                componentUtils.getRectangleById(this.focusIndex.toString());
              try {
                this.indicatorLeftMargin = px2vp(modePosition.windowOffset.x);
              } catch (error) {
                console.error("")
              }
              return { offsetRemain: offset };
            })

            this.textTest('更多')

          }
          .alignItems(VerticalAlign.Bottom)
          .width('100%')
          .backgroundColor("#ffb7b7b7")

        }
        .alignItems(HorizontalAlign.Start)
        .width('100%')


        Column()
          .height(10)
          .width("20%")
          .margin({ left: '80%', top: 28 })
          .backgroundColor("#ffb7b7b7")
      }
      .height(40)
      .width('100%')
      .backgroundColor("#ffb7b7b7")

      //tabs
      Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
        ForEach(this.tabArray, (item: number, index: number) => {
          TabContent() {
            Text('我是页面 ' + item + " 的内容的内容的内容的内容的内容的内容的内容的内容的内容的内容的内容的内容的内容")
              .height(300)
              .width('100%')
              .fontSize(30)
          }
          .backgroundColor(Color.Blue)
        })
      }
      .onAreaChange((oldValue: Area, newValue: Area) => {
        let width = Number.parseFloat(newValue.width.toString())
        this.tabsWidth = Number.isNaN(width) ? 0 : width
      })
      .width('100%')
      .barHeight(0)
      .animationDuration(100)
      .onChange((index: number) => {
        console.log('foo change')
        this.focusIndex = index
      })
      .onAnimationStart((index: number, targetIndex: number, event: TabsAnimationEvent) => {
        console.debug("onAnimationStart event.currentOffset: " + event.currentOffset)
        this.focusIndex = targetIndex
        let targetIndexInfo = this.getTextInfo(targetIndex)
        this.startAnimateTo(this.animationDuration, targetIndexInfo.left, targetIndexInfo.width)
      })
      .onAnimationEnd((index: number, event: TabsAnimationEvent) => {
        // 切换动画结束时触发该回调。下划线动画停止。
        let currentIndicatorInfo = this.getCurrentIndicatorInfo(index, event)
        this.startAnimateTo(0, currentIndicatorInfo.left, currentIndicatorInfo.width)
        console.debug("onAnimationEnd left: " + currentIndicatorInfo.left + ", widtch:" +
        currentIndicatorInfo.width)

        this.scrollIntoView(currentIndicatorInfo.index);
      })

      .onGestureSwipe((index: number, event: TabsAnimationEvent) => {
        // 在页面跟手滑动过程中,逐帧触发该回调。

        let currentIndicatorInfo = this.getCurrentIndicatorInfo(index, event)

        this.focusIndex = currentIndicatorInfo.index
        this.indicatorLeftMargin = currentIndicatorInfo.left
        this.tabWidth = currentIndicatorInfo.width
        this.indicatorWidth = currentIndicatorInfo.width
        console.debug("onGestureSwipe focusIndex: " + this.focusIndex
          + ", indicatorLeftMargin: " + this.indicatorLeftMargin + ", indicatorWidth: " + this.indicatorWidth)
      })
    }
    .height('100%')
  }

  private scrollIntoView(currentIndex: number): void {
    const indexInfo = this.getTextInfo(currentIndex);
    let tabPositionLeft = indexInfo.left;
    let tabWidth = indexInfo.width;
    // 获取屏幕宽度,单位vp
    const screenWidth = this.getDisplayWidth();
    //当前滚动的偏移量
    const currentOffsetX: number = this.scrollController.currentOffset().xOffset;

    // 将页签bar定位在正中间
    this.scrollController.scrollTo({
      xOffset: currentOffsetX + tabPositionLeft - screenWidth / 2 + tabWidth,
      yOffset: 0,
      animation: {
        duration: this.animationDuration,
        // 动画曲线
        curve: this.animationCurve,
      }
    });
  }

  // 获取屏幕宽度,单位vp
  private getDisplayWidth(): number {
    const displayInfo = display.getDefaultDisplaySync()
    return displayInfo != null ? px2vp(displayInfo.width) : 0;
  }

  private getTextInfo(index: number): Record<string, number> {
    let modePosition: componentUtils.ComponentInfo = componentUtils.getRectangleById(index.toString());
    try {
      return { 'left': px2vp(modePosition.windowOffset.x), 'width': px2vp(modePosition.size.width) }
    } catch (error) {
      return { 'left': 0, 'width': 0 }
    }
  }

  private getCurrentIndicatorInfo(index: number, event: TabsAnimationEvent): Record<string, number> {
    let nextIndex = index
    if (index > 0 && event.currentOffset > 0) {
      nextIndex--
    } else if (index < 4 && event.currentOffset < 0) {
      nextIndex++
    }

    let indexInfo = this.getTextInfo(index)
    let nextIndexInfo = this.getTextInfo(nextIndex)
    let swipeRatio = Math.abs(event.currentOffset / this.tabsWidth)
    console.info('event.currentOffset:' + event.currentOffset + 'nextIndex:' + nextIndex + ', index: ' + index +
      ", swipeRatio: " + swipeRatio)
    console.info('nextIndex:' + nextIndex + ', index: ' + index + ", swipeRatio: " + swipeRatio)
    let currentIndex = swipeRatio > 0.5 ? nextIndex : index // 页面滑动超过一半,tabBar切换到下一页。
    let currentLeft = indexInfo.left + (nextIndexInfo.left - indexInfo.left) * swipeRatio
    let currentWidth = indexInfo.width + (nextIndexInfo.width - indexInfo.width) * swipeRatio
    this.focusIndex = currentIndex
    console.info('index:' + currentIndex, 'left:' + currentLeft, 'width:' + currentWidth)
    return { 'index': currentIndex, 'left': currentLeft, 'width': currentWidth }
  }

  private startAnimateTo(duration: number, leftMargin: number, width: number) {
    animateTo({
      duration: duration, // 动画时长
      curve: Curve.Linear, // 动画曲线
      iterations: 1, // 播放次数
      playMode: PlayMode.Normal, // 动画模式
      onFinish: () => {
        console.info('play end')
      }
    }, () => {
      this.indicatorLeftMargin = leftMargin
      //  this.tabWidth = width
      this.indicatorWidth = width
    })
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS 是否新闻频道管理的案例
31浏览 • 1回复 待解决
HarmonyOS 需要频道导航功能样例
58浏览 • 1回复 待解决
HarmonyOS 新闻app demo
49浏览 • 2回复 待解决
HarmonyOS 怎么禁用原生导航
45浏览 • 1回复 待解决
HarmonyOS 怎么去掉底部导航
88浏览 • 1回复 待解决
获取状态导航高度
683浏览 • 1回复 待解决
Navigation如何隐藏导航
2208浏览 • 1回复 待解决
OceanBase业务案例哪些?
3756浏览 • 1回复 待解决
HarmonyOS上对导航怎么适配?
29浏览 • 1回复 待解决
HarmonyOS 如何获取底部导航高度?
55浏览 • 1回复 待解决
HarmonyOS 导航主题色如何配置
77浏览 • 1回复 待解决
HarmonyOS 如何获取顶部导航高度
21浏览 • 1回复 待解决
HarmonyOS 导航跳转后重置栈
20浏览 • 1回复 待解决
关于sidebar侧边遮挡导航的问题
488浏览 • 1回复 待解决
如何获取状态导航高度
2940浏览 • 1回复 待解决
如何获取状态导航高度?
270浏览 • 0回复 待解决