1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/harmonyos-cases-cases

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Клонировать/Скачать
README.md 4.7 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
chenyongjian Отправлено 09.10.2024 10:19 b990afb

自定义视图实现Tab效果

介绍

本示例介绍使用Text、List等组件,添加点击事件onclick,动画,animationTo实现自定义Tab效果。

此案例有新升级,可以参考自定义动效tab

效果预览图

使用说明

  1. 点击页签进行切换,选中态页签字体放大加粗,颜色由灰变黑,起到强调作用,同时,底部颜色条横线位移到当前选中页签下方,内容区翻页到当前选中页签对应区域。

实现思路

  1. 页签实现:添加onClick方法,记录点击的index,index变化后,改变页签颜色、字体大小,使用animateTo方法实现页签切换动画。 源码参考CustomView.ets
Text(title)
  .textAlign(TextAlign.Center)
  .height($r('app.integer.custom_view_width_and_height_value4'))
  .width(this.titleLengthRadix3 * title.length)
  .fontColor(this.currentIndex == idx ?
            (this.wantGoIndex == idx ? $r('app.color.custom_view_background_color1'):$r('app.color.custom_view_background_color2')):
            (this.wantGoIndex == idx ? $r('app.color.custom_view_background_color1'):$r('app.color.custom_view_background_color2')))
  .fontSize(this.currentIndex == idx ? $r('app.integer.custom_view_font_size2') : $r('app.integer.custom_view_font_size1'))
  .fontWeight(this.currentIndex == idx ? FontWeight.Bold : FontWeight.Normal)
  .onClick(() => {
    if (this.currentIndex != idx) {
      // 记录点击index
      this.wantGoIndex = idx;
      // 动画效果
      animateTo({
        duration: Math.abs(idx - this.currentIndex) * this.durationRadix,
        curve: Curve.EaseInOut,
        iterations: this.iterationsDefault,
        playMode: PlayMode.Normal,
        onFinish: () => {
          this.currentIndex = idx;
          this.scroller.scrollToIndex(this.currentIndex, true, ScrollAlign.START);
        }
      }, () => {
        this.transitionX = this.getTransitionX(idx);
      })
    }
  })
  1. 内容区实现:使用List,添加滑动手势来进行页面的切换,手势响应后,使用scrollToIndex方法来实现平滑的滑动到相应index。 源码参考CustomView.ets
PanGesture(this.panOption)
  .onActionUpdate((event:GestureEvent) => {
    if (!this.isStartAction) {
      this.isStartAction = true;
      if (event.offsetX < this.judgmentValue) {
        if (this.currentIndex < this.titleArray.length - this.currentIndexRadix) {
          let temIndex: number = this.currentIndex + this.currentIndexRadix;
          this.scroller.scrollToIndex(temIndex, true, ScrollAlign.START);
          this.wantGoIndex = temIndex;
          animateTo({
            duration: Math.abs(temIndex - this.currentIndex) * this.durationRadix,
            curve: Curve.EaseInOut,
            iterations: this.iterationsDefault,
            playMode: PlayMode.Normal,
            onFinish: () => {
              this.currentIndex = temIndex;
            }
          }, () => {
            this.transitionX = this.getTransitionX(temIndex);
          })
        }
      } else {
        if (this.currentIndex > this.judgmentValue) {
          let temIndex: number = this.currentIndex - this.currentIndexRadix;
          this.scroller.scrollToIndex(temIndex, true, ScrollAlign.START);
          this.wantGoIndex = temIndex;
          animateTo({
            duration: Math.abs(temIndex - this.currentIndex) * this.durationRadix,
            curve: Curve.EaseInOut,
            iterations: this.iterationsDefault,
            playMode: PlayMode.Normal,
            onFinish: () => {
              this.currentIndex = temIndex;
            }
          }, () => {
            this.transitionX = this.getTransitionX(temIndex);
          })
        }
      }
    }
  })

高性能知识点

scrollToIndex方法,开启smooth动效时,会对经过的所有item进行加载和布局计算,当大量加载item时会导致性能问题

工程结构&模块类型

customview                                       // har类型
|---view
|   |---CustomView.ets                           // 视图层-自定义视图实现Tab效果

模块依赖

utils

参考资料

PanGesture 事件

animateTo 显示动画

scrollToIndex 方法

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://api.gitlife.ru/oschina-mirror/harmonyos-cases-cases.git
git@api.gitlife.ru:oschina-mirror/harmonyos-cases-cases.git
oschina-mirror
harmonyos-cases-cases
harmonyos-cases-cases
master