写点什么

【每日学点 HarmonyOS Next 知识】 状态变量、公共 Page、可见区域变化回调、接收参数、拖拽排序控件

作者:轻口味
  • 2025-03-11
    北京
  • 本文字数:3732 字

    阅读完需:约 12 分钟

【每日学点HarmonyOS Next知识】 状态变量、公共Page、可见区域变化回调、接收参数、拖拽排序控件

1、HarmonyOS 在定时器里面改变 @state 修饰的变量,无法更新 UI 吗?

将函数 function 写成了封装函数的形式就可以了


@Entry@Componentstruct Index {  @State acSetValve: number = 0;
aboutToAppear(): void { setInterval(() => { this.acSetValve += 200; console.log('hahhah' + this.acSetValve) } ,100) }
build() { Row() { Column() { Text(this.acSetValve.toString()) .fontSize(50) .fontWeight(FontWeight.Bold) } .width('100%') } .height('100%') }}
复制代码

2、HarmonyOS 写一个公共的 page,然后其他具体的 page 继承它,在公共 page 的做一些处理?

demo 参考:


//主页面  FirstTest.etsimport { FirstTest2 } from "./FirstTest2"
@Entry@Componentstruct FirstTest { @State message1: string = 'Hello World'; // 显隐控制设置为不占用
build() { Column() { Text("FirstTest:" + this.message1) .fontSize(25) .fontWeight(FontWeight.Bold) //FirstTest2页面数据传递参考API:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/ArkTS-link-0000001820999565#ZH-CN_TOPIC_0000001857916041__%E6%A6%82%E8%BF%B0 //FirstTest2为Component修饰的组件,并且进行export,然后在FirstTest进行导入直接使用即可,可以多个页面复用 FirstTest2({message2:this.message1}); } }}
//复用组件 FirstTest2.ets@Componentexport struct FirstTest2 { @State test:string = "FirstTest2" @Link message2: string ;
build() { Row() { Column() { Text("FirstTest2:" + this.test) .fontSize(25) .fontWeight(FontWeight.Bold)
Text("FirstTest2:" + this.message2) .fontSize(25) .fontWeight(FontWeight.Bold) } .width('100%') } .height('100%') }}
复制代码

3、HarmonyOS onVisibleAreaChange 事件没有触发?

添加 onVisibleAreaChange 事件监听,只有第一屏可以触发,切到第二屏没有触发。影响埋点上报


onVisibleAreaChange 规格上不支持监控由改变 offset 引起的组件区域变化。若要实现切屏监控,请使用 swiper 组件。


文档参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-swiper-V5

4、HarmonyOS router.pushNamedRoute 参数传递 在接收页面怎么接收?

可以通过 router.getParams();接收参数,API 链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-router-V5#ZH-CN_TOPIC_0000001884917590__routergetparams


参考 demo 如下:


import router from '@ohos.router';class innerParams {  array:number[]  constructor(tuple:number[]) {    this.array = tuple  }}
class routerParams { text:string data:innerParams constructor(str:string, tuple:number[]) { this.text = str this.data = new innerParams(tuple) }}
@Entry@Componentstruct Router1 {
build() {
Column(){ Column(){ Text("我是页面1").fontSize(50) } Column(){
Button("跳转") .onClick(()=>{ router.pushUrl({ url: 'pages/Router11', params: new routerParams('message' ,[123,456,789]) }) }) } } .width('100%') .height('100%') }}import router from '@ohos.router';
class innerParams { array:number[]
constructor(tuple:number[]) { this.array = tuple }}
class routerParams { text:string data:innerParams constructor(str:string, tuple:number[]) { this.text = str this.data = new innerParams(tuple) }}
@Entry@Componentstruct Router11 {
aboutToAppear(): void { let s:number[]=((router.getParams() as routerParams).data as innerParams).array console.log("*********:"+s); } build() { Column(){ Text("123").fontSize(50) Row(){ Text("33333").fontSize(50) } } .width('100%') .height('100%') .opacity(0.5) .backgroundColor(Color.Pink)
}}
复制代码

5、HarmonyOS 拖拽排序以及控件平移效果是否有工具类可以用,当前控件数据数量上限无法满足?

拖拽排序以及控件平移效果是否有工具类可以用,当前控件数据数量上限无法满足


动画效果可以参考以下 demo:


@Entry@Componentstruct AnimationPage {  @Provide desX: number = 0;  @Provide desY: number = 0;
@Builder TabBar(index: number) { Column() { Image($r("app.media.icon")) .width(38) .height(38) Text(index == 1 ? "分类" : index.toString()) } .onAreaChange((oldValue: Area, newValue: Area) => { if (index == 1) { console.log("onAreaChange========= newValue:" + JSON.stringify(newValue)) this.desX = newValue.globalPosition.x as number this.desY = newValue.globalPosition.y as number } }) }
build() { Column() { Category() Column() { Image($r("app.media.icon")) .width(38) .height(38) } .onAreaChange((oldValue: Area, newValue: Area) => { console.log("onAreaChange========= newValue:" + JSON.stringify(newValue)) this.desX = newValue.globalPosition.x as number this.desY = newValue.globalPosition.y as number }) } .width("100%") .height("100%") }}
@Componentstruct Category {
@State dataList:number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
build() { Column() { Flex({direction:FlexDirection.Row, justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap}) { ForEach(this.dataList, (item:number) => { ListItemComponent() }) } .width('100%') .height('80%') .backgroundColor(Color.White) } }}
@Observedclass AnimationBean { animationAppear: Boolean = false; id:number = -1;}
@Componentstruct ListItemComponent { @State animationList: AnimationBean[] = [] static index: number = 0; @Consume desX: number; @Consume desY: number;
@Provide orgX: number = 0; @Provide orgY: number = 0;

@Builder BuildAnimationItem() { Image($r("app.media.app_icon")) .width(41) .height(41) .id("test") .onClick(() => { this.startAnimation(); }) .onAreaChange((oldValue:Area, newValue:Area) => { this.orgX = newValue.globalPosition.x as number this.orgY = newValue.globalPosition.y as number console.log("onAreaChage==========" + JSON.stringify(newValue)) }) }
startAnimation() { const animation = new AnimationBean(); animation.id = ListItemComponent.index++ this.animationList.push(animation) animateTo({ duration: 800, curve: Curve.FastOutLinearIn, onFinish: () => { animation.animationAppear = false; this.animationList = this.animationList.filter((item: AnimationBean) => { return item.animationAppear }) } }, () => { animation.animationAppear = true; }) }
@Builder BuildItem() { Row() { Stack() { this.BuildAnimationItem()
ForEach(this.animationList, (item: AnimationBean) => { AnimationChild({ item: item , AnimationView:this.BuildAnimationItem}) }, (item: AnimationBean, index: number) => { return item.id.toLocaleString() }) } } .width("20%") .height(60) }
build() { this.BuildItem() }}

@Componentstruct AnimationChild { @ObjectLink item: AnimationBean; @Consume desX: number; @Consume desY: number; @Consume orgX: number; @Consume orgY: number;
@BuilderParam AnimationView:() => void;
build() { Row() { this.AnimationView() } .zIndex(5) .translate(this.item.animationAppear ? { x: (this.desX - this.orgX -20), y: (this.desY - this.orgY) } : { x: 0 }) }}
复制代码


发布于: 刚刚阅读数: 5
用户头像

轻口味

关注

🏆2021年InfoQ写作平台-签约作者 🏆 2017-10-17 加入

Android、音视频、AI相关领域从业者。 欢迎加我微信wodekouwei拉您进InfoQ音视频沟通群 邮箱:qingkouwei@gmail.com

评论

发布
暂无评论
【每日学点HarmonyOS Next知识】 状态变量、公共Page、可见区域变化回调、接收参数、拖拽排序控件_HarmonyOS_轻口味_InfoQ写作社区