【每日学点鸿蒙知识】RelativeContainer组件、List回弹、Flutter方法调用、Profiler工具等

1、RelativeContainer组件auto对齐规则?

当height设置为auto,这时候为什么子组件设置

复制代码
top:{anchor: "__container__",align: VerticalAlign.Top}

后auto就不生效了呢,anchor锚点不是默认top对齐的吗?

这是为了避免二次布局导致过大的性能开销,给高度标记了auto之后,在垂直方向上,RelativeContainer会依赖组件间在垂直方向上的布局这样会导致二次布局,因为子组件此时是相对容器的垂直方向上的布局,那这个时候,父组件的布局又要依赖子组件布局之后的结果再次进行布局。

2、如何实现scroll、list单边回弹效果?

在onDidScroll里获取currentOffset().yOffset来控制单边回弹效果,在list组件还可以通过onScrollIndex实现单边回弹效果。

scroll组件

复制代码
@Entry
@Component
struct Index {
  @State yOffset: number = 0
  scroller: Scroller = new Scroller()
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

  build() {
    Stack({ alignContent: Alignment.TopStart }) {
      Scroll(this.scroller) {
        Column() {
          ForEach(this.arr, (item: number) => {
            Text(item.toString())
              .width('90%')
              .height(150)
              .backgroundColor(0xFFFFFF)
              .borderRadius(15)
              .fontSize(16)
              .textAlign(TextAlign.Center)
              .margin({ top: 10 })
          }, (item: string) => item)
        }.width('100%')
      }
      .scrollable(ScrollDirection.Vertical) // 滚动方向纵向
      .scrollBar(BarState.On) // 滚动条常驻显示
      .scrollBarColor(Color.Gray) // 滚动条颜色
      .scrollBarWidth(10) // 滚动条宽度
      .friction(0.6)
      .edgeEffect(this.yOffset <= 0 ? EdgeEffect.Spring : EdgeEffect.None) // 滚动到边沿后回弹
      .onDidScroll(() => {
        this.yOffset = this.scroller.currentOffset().yOffset;
      })
    }.width('100%').height('100%').backgroundColor(0xDCDCDC)
  }
}

List组件

复制代码
@Entry
@Component
struct Index {
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  @State isTop: boolean = true
  build() {
    Column() {
      List({ space: 20, initialIndex: 0 }) {
        ForEach(this.arr, (item: number) => {
          ListItem() {
            Text('' + item)
              .width('100%')
              .height(100)
              .fontSize(16)
              .textAlign(TextAlign.Center)
              .borderRadius(10)
              .backgroundColor(0xFFFFFF)
          }
        }, (item: string) => item)
      }
      .listDirection(Axis.Vertical) // 排列方向
      .scrollBar(BarState.Off)
      .friction(0.6)
      .divider({ strokeWidth: 2, color: 0xFFFFFF, startMargin: 20, endMargin: 20 }) // 每行之间的分界线
      .edgeEffect(this.isTop ? EdgeEffect.None : EdgeEffect.Spring)
      .onScrollIndex((firstIndex: number, lastIndex: number, centerIndex: number) => {
        if (firstIndex === 0) {
          this.isTop = true
        } else {
          this.isTop = false
        }
      })
      .width('90%')
    }
    .width('100%')
    .height('100%')
    .backgroundColor(0xDCDCDC)
    .padding({ top: 5 })
  }
}

3、HarmonyOS 能调用Flutter的Dart接口的代码么?

使用ohos平台的 MethodChannel#invokeMethod 方法,可以实现HarmonyOS调用dart代码中的方法的功能。

4、使用IDE自带profiler工具检测CPU,Current process显示为0%, Other process显示偏高。这里显示数据含义是什么?如何使用工具分析CPU高原因?

otherprocess包含了桌面应用、系统的一些后台服务等。

CPU分析具体可以参考官方指导文档 CPU活动分析板块:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-insight-session-cpu-V5

5、HarmonyOS 报错 Error while Deploy Hap?

打包的har包,在壳工程内,编译通过,但是安装时失败。

复制代码
04/10 14:01:54: Install Failed: error: failed to install bundle.
code:9568278
error: install version code not same.
$ tiaoyou shell rm -rf data/local/tmp/f47e1222b8c64dbe92f86bc3b55cc3d2
Error while Deploy Hap

该报错是因为需要安装的应用和系统中已经安装的应用,app.json的versionCode字段不一致。

  1. 可能开发者使用IDE的debug按钮安装了该应用,后来又通过打包之后hdc install 的方式进行了重复安装,可以使用命令查看已安装应用的debug字段信息:

    bm dump -n 应用bundleName | grep debug

普通应用卸载安装:

复制代码
hdc uninstall 应用bundleName  

清空应用数据:

复制代码
hdc shell bm clean -d -n 应用bundleName  
  1. 也可能是保存的数据应用版本和新安装的版本不一致导致的 ===》Run configuration-General,去掉保存数据Keep Application Data的勾选。
相关推荐
GitLqr5 分钟前
Flutter 居然能直接跑在 Apple Watch 上了?玩转 flutter-watchos
flutter·全栈·apple watch
math_hongfan1 小时前
鸿蒙存储异常高级排查:文件损坏检测/数据恢复/读写失败重试/磁盘空间预警系统性根治方案
学习·华为·harmonyos·鸿蒙
lilian2331 小时前
Harmony os 技术实战|拼豆制图10:把取消、解析失败和保存失败写成可恢复状态机
java·javascript·华为·harmonyos
2501_919749032 小时前
华为鸿蒙美缝剂实用APP—小羊美缝
华为·harmonyos
2501_919749032 小时前
华为鸿蒙积攒年度高光APP—小羊高光
华为·harmonyos
math_hongfan3 小时前
鸿蒙存储碎片高级清理机制:数据库Vacuum/文件碎片整理/缓存过期清理/主动空间回收策略
jvm·数据库·学习·缓存·华为·harmonyos·鸿蒙
梦想不只是梦与想3 小时前
鸿蒙UI测试框架:UITest
harmonyos·uitest·ui测试框架
你听得到113 小时前
排查 App 问题:别只盯着报错,把前后发生的事情串起来
android·前端·flutter
天空之城--4 小时前
近一周Android Flutter行业动态与实用参考
android·flutter
智塑未来5 小时前
鸿蒙系统小红书隐私保护——应用锁开启指南
服务器·前端·harmonyos