文档开发者中心
我的
|

ArkUI (方舟UI框架)

UI

HarmonyOS 5

HarmonyOS 抽屉拖动与内部组件高度实时变化 (API12+)

进入页面展示抽屉,抽屉可以上下拖动(最高为屏幕高度的80%,最低为屏幕高度的30%),抽屉内的内容会根据当前抽屉高度判断是否可以滚动,点击抽屉外部不关闭抽屉。

点赞
收藏
回复
1
分享
举报
浏览38 发布于2026-04-02 11:25未知归属地
全部评论
最多点赞
最新发布
最早发布

使用@State实现跟手上下滑动,List实现列表滚动,参考Demo如下:

收起
自动换行
深色代码主题
复制
// EntryAbility.ets
export default class EntryAbility extends UIAbility {
  ...
  onWindowStageCreate(windowStage: window.WindowStage): void {
    ...
    // 获取顶部安全区尺寸
    let topHeight = 0;
    topHeight = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)?.topRect?.height;
    console.info("===" +JSON.stringify(windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)))
    console.log('topHeight = ', topHeight)
    windowClass.on('avoidAreaChange', (data) => { // 监听状态栏高度变化
      if (data.type == window.AvoidAreaType.TYPE_SYSTEM) { // 具体类型判断
        topHeight = data.area.topRect.height
        console.log('topHeight == ', topHeight)
      }
    })
    AppStorage.setOrCreate('topRectHeight', topHeight);

    // 获取底部安全区尺寸 
    let bottomHeight = 0;
    bottomHeight = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR)?.bottomRect?.height
    console.log('bottomHeight = ', bottomHeight)
    windowClass.on('avoidAreaChange', (data) => { // 监听状态栏高度变化
      if (data.type == window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) { // 具体类型判断
        bottomHeight = data.area.bottomRect.height
        console.log('bottomHeight = ', bottomHeight)
      }
    })
    AppStorage.setOrCreate('bottomRectHeight', bottomHeight);

    windowStage.getMainWindow().then((windowClass) => {
      // 获取窗口尺寸,存入AppStorage
      AppStorage.setOrCreate('winWidth', windowClass.getWindowProperties().windowRect.width);
      AppStorage.setOrCreate('winHeight', windowClass.getWindowProperties().windowRect.height);
      // 监听窗口尺寸变化
      windowClass.on('windowSizeChange', (windowSize) => {
        AppStorage.setOrCreate('winWidth', windowSize.width);
        AppStorage.setOrCreate('winHeight', windowSize.height);
      });
    });
    ...
  }
  ...
}
收起
自动换行
深色代码主题
复制
// Index.ets
let items: number[] = [0, 1, 2, 3, 4]

@Entry
@Component
struct Index {
  private scrollerForScroll: Scroller = new Scroller();
  @State heightInfo: number = 0
  heightSum: number = 0
  heightCurr: number = 0
  maxHeight: number = 0
  minHeight: number = 0

  aboutToAppear(): void {
    let winHeight = px2vp(AppStorage.get('winHeight') as number) // 屏幕尺寸
    let topRectHeight = px2vp(AppStorage.get('topRectHeight') as number) // 顶部安全区尺寸
    let bottomRectHeight = px2vp(AppStorage.get('bottomRectHeight') as number) // 底部安全区尺寸
    this.heightSum = winHeight - topRectHeight - bottomRectHeight
    this.maxHeight = this.heightSum * 0.8
    this.minHeight = this.heightSum * 0.3
    this.heightInfo = this.maxHeight
  }

  @Builder
  mineBuilder() {
    Column() {
      Column() {
        Text("移动")
          .fontSize(40)
          .width("80%")
          .height(50)
          .gesture(PanGesture()
            .onActionStart((event: GestureEvent) => {
              this.heightCurr = this.heightInfo
            })
            .onActionUpdate((event: GestureEvent) => {
              this.heightInfo = this.heightCurr - event.offsetY
              if (this.heightInfo > this.maxHeight) {
                this.heightInfo = this.maxHeight
              }
              if (this.heightInfo < this.minHeight) {
                this.heightInfo = this.minHeight
              }
              console.info("height = " + this.heightInfo)
            }))
          .backgroundColor(Color.Orange)
        List({ space: 50, initialIndex: 0, scroller: this.scrollerForScroll }) {
          ForEach(items, (item: number) => {
            ListItem() {
              Column() {
                Text(item.toString())
                  .fontSize(30)
                  .fontColor(Color.Black)
                  .width("100%")
                  .height(40)
                  .backgroundColor(Color.Pink)
              }.width("100%")
            }
          }, (item: string) => item)
        }
        .scrollBar(BarState.Off)
        .backgroundColor(Color.Yellow)
        .height(1)
        .flexGrow(1)
        .flexShrink(1)
        .flexBasis(1)
      }
      .width("100%")
      .height(this.heightInfo)
      .backgroundColor(Color.Grey)
    }.justifyContent(FlexAlign.End)
    .width('100%')
    .height('100%')
  }

  build() {
    Column() {
      this.mineBuilder()
    }
    .backgroundColor(Color.Blue)
  }
}
展开全部
1楼回复于2026-04-02 11:25 未知归属地
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。