当前位置: 首页 > article >正文

HarmonyOS 5.0应用开发——NodeContainer自定义占位节点

【高心星出品】

文章目录

      • NodeContainer自定义占位节点
        • 案例
        • 开发步骤
        • 全部代码

NodeContainer自定义占位节点

NodeContainer是用来占位的系统组件,主要用于自定义节点以及自定义节点树的显示,支持组件的通用属性,对通用属性的处理请参考默认左上角对齐的Stack组件。

NodeController提供了一系列生命周期回调,通过makeNode回调返回一个 FrameNode 节点树的根节点。将FrameNode节点树挂载到对应的NodeContainer下。同时提供了aboutToAppear、aboutToDisappear、aboutToResize、onTouchEvent、rebuild五个回调方法用于监听对应的NodeContainer的状态。

在这里如果使用NodeContainer可以有两种使用方式:

nodecontainer-->nodectroller-->framnode--->typenode
nodecontainer-->nodectroller-->framenode-->buildernode-->builder构建函数
案例

下面我们编写一个案例,用typenode生成布局和用builder构建函数生成布局两种方式来创建framnode,并通过nodecontroller挂在到nodecontainer中。

运行结果

运行过程中"布局文本"是build函数中的组件,在其下面有builder构建函数生成的节点也有通过typenode生成的几点,我们通过按钮点击来切换。

在这里插入图片描述

开发步骤

全局构建函数

interface param{
  text1:string
  text2:string
}
// 全局构建函数
@Builder
function buildcontainer(p:param){
  Column({space:10}){
    Text(p.text1).fontSize(22)
    Text(p.text2).fontSize(22)
  }.width('100%')
  .padding(10)
}

typenode生成函数

//使用typenode生成framenode
function gentypenode(uicontext:UIContext){
  let row=typeNode.createNode(uicontext,'Row')
  row.initialize()
  row.attribute.width('100%')
  row.attribute.padding(20)
  row.attribute.backgroundColor(Color.Red)
  row.attribute.justifyContent(FlexAlign.Center)
  let button=typeNode.createNode(uicontext,'Button')
  button.initialize('typenode节点')
  row.appendChild(button)
  return row
}

nodecontroller对象加载framenode

在这里面根据type选择挂载builder构建函数还是typenode,根据isshow决定是否显示布局。

rebuild方法会引起nodectroller重新调用makeNode方法。

class mynodectl extends NodeController{
  node:BuilderNode<[param]>|null=null
  private isshow:boolean=true
  // 根据不同的type决定是typenode或者是构建函数
  private _type: string = 'builder'

  public settype(value: string) {
    this._type = value
  }

  constructor(type: string) {
    super()
    this._type = type
  }

  toshow(){
    this.isshow=true
    // 重新构建节点 重新调用makenode方法
    this.rebuild()
  }
  tohide(){
    this.isshow=false
    // 重新构建节点 重新调用makenode方法
    this.rebuild()
  }

  makeNode(uiContext: UIContext): FrameNode | null {
    if (!this.isshow){
      return null
    }
    if(this.node==null)
    {
      // 将build构建函数转化成节点
      if(this._type=='builder') {
        this.node = new BuilderNode(uiContext)
        this.node.build(wrapBuilder<[param]>(buildcontainer), { text1: 'builder文本节点1', text2: 'builder文本节点2' })
      }else{
       return gentypenode(uiContext)
      }
    }
    return this.node.getFrameNode()
  }

}

页面代码

这里面通过调用tohide或者toshow来调控布局的显示,通过settype来调控显示的布局内容是来自builder还是typenode。

@Entry
@Component
struct Nodecontainerpage {
  @State message: string = 'Hello World';
  // 定义节点对象
  nodelayout:mynodectl=new mynodectl('type')
  build() {
    Column({space:20}){
      Text('布局文本').fontSize(22)
        .onClick(()=>{
          this.nodelayout.settype('builder')
          this.nodelayout.toshow()
        })
      //nodecontainer-->nodectroller-->framnode--->typenode
      //nodecontainer-->nodectroller-->framenode-->buildernode-->builder构建函数
      NodeContainer(this.nodelayout)
        .onClick(()=>{
          this.nodelayout.tohide()
        })
    }
    .height('100%')
    .width('100%')
  }
}
全部代码
import { BuilderNode, NodeController, typeNode, UIContext } from '@kit.ArkUI'

interface param{
  text1:string
  text2:string
}
// 全局构建函数
@Builder
function buildcontainer(p:param){
  Column({space:10}){
    Text(p.text1).fontSize(22)
    Text(p.text2).fontSize(22)
  }.width('100%')
  .padding(10)
}

//使用typenode生成framenode
function gentypenode(uicontext:UIContext){
  let row=typeNode.createNode(uicontext,'Row')
  row.initialize()
  row.attribute.width('100%')
  row.attribute.padding(20)
  row.attribute.backgroundColor(Color.Red)
  row.attribute.justifyContent(FlexAlign.Center)
  let button=typeNode.createNode(uicontext,'Button')
  button.initialize('typenode节点')
  row.appendChild(button)
  return row
}

class mynodectl extends NodeController{
  node:BuilderNode<[param]>|null=null
  private isshow:boolean=true
  // 根据不同的type决定是typenode或者是构建函数
  private _type: string = 'builder'

  public settype(value: string) {
    this._type = value
  }

  constructor(type: string) {
    super()
    this._type = type
  }

  toshow(){
    this.isshow=true
    // 重新构建节点 重新调用makenode方法
    this.rebuild()
  }
  tohide(){
    this.isshow=false
    // 重新构建节点 重新调用makenode方法
    this.rebuild()
  }

  makeNode(uiContext: UIContext): FrameNode | null {
    if (!this.isshow){
      return null
    }
    if(this.node==null)
    {
      // 将build构建函数转化成节点
      if(this._type=='builder') {
        this.node = new BuilderNode(uiContext)
        this.node.build(wrapBuilder<[param]>(buildcontainer), { text1: 'builder文本节点1', text2: 'builder文本节点2' })
      }else{
       return gentypenode(uiContext)
      }
    }
    return this.node.getFrameNode()
  }

}

@Entry
@Component
struct Nodecontainerpage {
  @State message: string = 'Hello World';
  // 定义节点对象
  nodelayout:mynodectl=new mynodectl('type')
  build() {
    Column({space:20}){
      Text('布局文本').fontSize(22)
        .onClick(()=>{
          this.nodelayout.settype('builder')
          this.nodelayout.toshow()
        })
      //nodecontainer-->nodectroller-->framnode--->typenode
      //nodecontainer-->nodectroller-->framenode-->buildernode-->builder构建函数
      NodeContainer(this.nodelayout)
        .onClick(()=>{
          this.nodelayout.tohide()
        })
    }
    .height('100%')
    .width('100%')
  }
}
de
      //nodecontainer-->nodectroller-->framenode-->buildernode-->builder构建函数
      NodeContainer(this.nodelayout)
        .onClick(()=>{
          this.nodelayout.tohide()
        })
    }
    .height('100%')
    .width('100%')
  }
}

http://www.kler.cn/a/539352.html

相关文章:

  • 【网络安全.渗透测试】Cobalt strike(CS)工具使用说明
  • 51单片机之引脚图(详解)
  • C# 封送和远程编程介绍
  • 通过Demo案例的形式弄懂Java中的设计模式
  • deepseek API开发简介
  • 【C++语法】【STL】“for ( auto c : str )”类型的循环
  • 每日一题——数组中出现次数超过一半的数字
  • 生成式聊天机器人 -- 基于Pytorch + Global Attention + 双向 GRU 实现的SeqToSeq模型 -- 下
  • AI安全最佳实践:AI云原生开发安全评估矩阵(下)
  • ESP32S3基于espidf ADC使用
  • Leetcode Hot100 76-80
  • 【算法-动态规划】、子序列累加和必须被7整除的最大累加和
  • 机器学习 网络安全 GitHub 机器人网络安全
  • 工业 4G 路由器助力消防领域,守卫生命安全防线
  • ASP.NET Core SignalR的分布式部署
  • 【Uniapp-Vue3】UniCloud云数据库获取指定字段的数据
  • 【蓝桥杯嵌入式】8_IIC通信-eeprom读写
  • 【Android开发AI实战】选择目标跟踪基于opencv实现——运动跟踪
  • 硬盘会莫名增加大量视频和游戏的原因
  • MoMask:可将文本描述作为输入并生成相应的高质量人体运动动作
  • 三种Excel文本连接方法!
  • C#Halcon窗体鼠标交互生成菜单
  • Android网络优化之-HTTPDNS
  • PHP-trim
  • 2025_2_9 C语言中队列
  • Docker 部署 RabbitMQ | 自带延时队列