鸿蒙实现右滑加载更多

dart 复制代码
@Entry
@Component
struct Index {
  build() {
    Stack({ alignContent: Alignment.Start }) {
      TabRightMore({ totalSize: 4 }) {
        TabContent() {
          Column().width('100%').height('100%').backgroundColor(Color.Red)
        }

        TabContent() {
          Column().width('100%').height('100%').backgroundColor(Color.Blue)
        }

        TabContent() {
          Column().width('100%').height('100%').backgroundColor(Color.Yellow)
        }

        TabContent() {
          Column().width('100%').height('100%').backgroundColor(Color.Black)
        }
      }
    }
    .height('100%')
    .width('100%')
  }
}

@ComponentV2
struct TabRightMore {
  @Require @Param totalSize: number = 0
  private innerSelectedIndex = 0
  private listScroller: ListScroller = new ListScroller()
  private LAST_INDEX = this.totalSize - 1

  @Monitor('totalSize')
  watchTotalSize() {
    this.LAST_INDEX = this.totalSize - 1
  }

  @Builder
  closerBuilder() {
  }

  // 使用父组件的尾随闭包{}(@Builder装饰的方法)初始化子组件@BuilderParam
  @BuilderParam closer: () => void = this.closerBuilder;

  build() {
    Stack({ alignContent: Alignment.Start }) {
      List({scroller: this.listScroller}) {
        ListItem() {
          Tabs() {
            this.closer()
          }
          .animationDuration(0)
          .edgeEffect(EdgeEffect.None)
          .onAnimationStart((index: number, targetIndex: number) => {
            console.info('ets onGestureRecognizerJudgeBegin child:' + targetIndex)
            this.innerSelectedIndex = targetIndex
          })
          .onGestureRecognizerJudgeBegin((event: BaseGestureEvent, current: GestureRecognizer, others: Array<GestureRecognizer>): GestureJudgeResult => { // 在识别器即将要成功时,根据当前组件状态,设置识别器使能状态
            if (current) {
              let target = current.getEventTargetInfo();
              if (target && current.isBuiltIn() && current.getType() == GestureControl.GestureType.PAN_GESTURE) {
                let panEvent = event as PanGestureEvent;
                if (panEvent && panEvent.velocityX < 0 && this.innerSelectedIndex === this.LAST_INDEX) { // 内层Tabs滑动到尽头
                  return GestureJudgeResult.REJECT;
                }
                if (panEvent && panEvent.velocityX > 0 && this.innerSelectedIndex === 0) { // 内层Tabs滑动到开头
                  return GestureJudgeResult.REJECT;
                }
              }
            }
            return GestureJudgeResult.CONTINUE;
          }, true)
          .barHeight(0)
          .barWidth(0)
          .backgroundColor(Color.Gray)
          .width('100%')
          .height('100%')
        }

        ListItem() {
          Column()
            .backgroundColor(Color.Orange)
            .height('100%')
            .width(100)
        }
      }
      .listDirection(Axis.Horizontal)
      .width('100%')
      .height(200)
      .edgeEffect(EdgeEffect.None)
      .scrollBar(BarState.Off)
      .onTouch((event) => {
        if (event.type === TouchType.Down) {
          console.log('Index000 TouchType.Down')
        } else if (event.type === TouchType.Up || event.type === TouchType.Cancel) {
          console.log('Index000 TouchType.Up Cancel ' + this.listScroller.currentOffset().xOffset)
          this.listScroller.scrollTo({ xOffset: 0, yOffset: 0, animation: true })
        }
      })
    }
    .height('100%')
    .width('100%')
  }
}
相关推荐
程序员潘Sir7 小时前
鸿蒙应用开发从入门到实战(五):ArkUI概述
harmonyos·鸿蒙
程序员潘Sir1 天前
鸿蒙应用开发从入门到实战(四):ArkTS 语言概述
harmonyos·鸿蒙
vipers_2 天前
鸿蒙的编程软件的介绍
鸿蒙·鸿蒙编程软件
被开发耽误的大厨2 天前
鸿蒙项目篇-22-项目功能结构说明-写子页面和导航页面
android·华为·harmonyos·鸿蒙
程序员潘Sir3 天前
鸿蒙应用开发从入门到实战(三):第一个鸿蒙应用
harmonyos·鸿蒙
森之鸟5 天前
开发中使用——鸿蒙子页面跳转到指定Tab页面
harmonyos·鸿蒙
程序员潘Sir5 天前
HarmonyOS编写教师节贺卡
harmonyos·鸿蒙
程序员潘Sir6 天前
HarmonyOS实现快递APP自动识别地址
harmonyos·鸿蒙
HarmonyOS小助手6 天前
【案例+1】HarmonyOS官方模板优秀案例 第7期:金融理财 · 记账应用
harmonyos·鸿蒙·鸿蒙生态