高阶组件树视图基本用法

针对多级节点数据的展示和操作的一套方案,能够进行节点新增,删除,修改,折叠等操作。

HarmonyOS
2024-05-26 14:03:08
2317浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
e_lion

使用的核心API

TreeView

核心代码解释

import { 
  TreeController, 
  TreeListener, 
  TreeListenerManager, 
  TreeListenType, 
  NodeParam, 
  TreeView, 
  CallbackParam 
} from '@ohos.arkui.advanced.TreeView' 
 
@Entry 
@Component 
struct TreeViewDemo { 
  private treeController: TreeController = new TreeController(); 
  private treeListener: TreeListener = TreeListenerManager.getInstance().getTreeListener(); 
  @State clickNodeId: number = 0; 
 
  // 销毁相关监听 
  aboutToDisappear(): void { 
    this.treeListener.off(TreeListenType.NODE_CLICK, undefined); 
    this.treeListener.off(TreeListenType.NODE_ADD, undefined); 
    this.treeListener.off(TreeListenType.NODE_DELETE, undefined); 
    this.treeListener.off(TreeListenType.NODE_MOVE, undefined); 
  } 
 
  aboutToAppear(): void { 
    this.treeListener.on(TreeListenType.NODE_CLICK, (callbackParam: CallbackParam) => { 
      this.clickNodeId = callbackParam.currentNodeId; 
    }) 
    this.treeListener.on(TreeListenType.NODE_ADD, (callbackParam: CallbackParam) => { 
      this.clickNodeId = callbackParam.currentNodeId; 
    }) 
    this.treeListener.on(TreeListenType.NODE_DELETE, (callbackParam: CallbackParam) => { 
      this.clickNodeId = callbackParam.currentNodeId; 
    }) 
    this.treeListener.once(TreeListenType.NODE_MOVE, (callbackParam: CallbackParam) => { 
      this.clickNodeId = callbackParam.currentNodeId; 
    }) 
    let normalResource: Resource = $r('app.media.icon'); 
    let selectedResource: Resource = $r('app.media.avatar'); 
    let editResource: Resource = $r('app.media.2'); 
    let nodeParam: NodeParam = { 
      parentNodeId: -1, // 父节点Id 
      currentNodeId: 1, // 节点Id 
      isFolder: true, // 是否折叠 
      icon: normalResource, // 默认图标 
      selectedIcon: selectedResource, // 选中图标 
      editIcon: editResource, // 编辑态图标 
      primaryTitle: "Number1", // 节点标题内容 
      secondaryTitle: "6" // 节点副标题文字内容 
    }; 
    this.treeController 
      .addNode(nodeParam) 
      .addNode({ parentNodeId: 1, currentNodeId: 2, isFolder: false, primaryTitle: "项目1_1" }) 
      .addNode({ parentNodeId: -1, currentNodeId: 7, isFolder: true, primaryTitle: "目录2" }) 
      .addNode({ 
        parentNodeId: -1, 
        currentNodeId: 23, 
        isFolder: true, 
        icon: normalResource, 
        selectedIcon: selectedResource, 
        editIcon: editResource, 
        primaryTitle: "目录3" 
      }) 
      .addNode({ parentNodeId: -1, currentNodeId: 24, isFolder: false, primaryTitle: "项目4" }) 
      .addNode({ 
        parentNodeId: -1, 
        currentNodeId: 31, 
        isFolder: true, 
        icon: normalResource, 
        selectedIcon: selectedResource, 
        editIcon: editResource, 
        primaryTitle: "目录5", 
        secondaryTitle: "0" 
      }) 
      .buildDone(); 
    this.treeController.refreshNode(-1, "父节点", "子节点"); 
  } 
 
  build() { 
    Column() { 
      SideBarContainer(SideBarContainerType.Embed) { 
        TreeView({ treeController: this.treeController }) 
        Row() { 
          Divider().vertical(true).strokeWidth(2).color(0x000000).lineCap(LineCapStyle.Round) 
          Column({ space: 30 }) { 
            Text('ClickNodeId=' + this.clickNodeId).fontSize('16fp') 
            Button('Add', { type: ButtonType.Normal, stateEffect: true }) 
              .borderRadius(8).backgroundColor(0x317aff).width(90) 
              .onClick((event: ClickEvent) => { 
                this.treeController.addNode(); 
              }) 
            Button('Modify', { type: ButtonType.Normal, stateEffect: true }) 
              .borderRadius(8).backgroundColor(0x317aff).width(90) 
              .onClick((event: ClickEvent) => { 
                this.treeController.modifyNode(); 
              }) 
            Button('Remove', { type: ButtonType.Normal, stateEffect: true }) 
              .borderRadius(8).backgroundColor(0x317aff).width(120) 
              .onClick((event: ClickEvent) => { 
                this.treeController.removeNode(); 
              }) 
          }.height('100%').width('70%').alignItems(HorizontalAlign.Start).margin(10) 
        } 
      } 
      .focusable(true) 
      .showControlButton(false) 
      .showSideBar(true) 
    } 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.

实现效果

适配的版本信息

IDE:DevEco Studio 4.0.3.600

SDK:HarmoneyOS 4.0.10.11

分享
微博
QQ
微信
回复
2024-05-27 16:58:43
相关问题
系统Tabs组件用法有哪些?
1351浏览 • 1回复 待解决
关于数组的高阶函数降维操作
753浏览 • 1回复 待解决
HarmonyOS 组件在其父视图上的位置
1037浏览 • 1回复 待解决
HarmonyOS 月视图模式的日历组件参考
961浏览 • 1回复 待解决
HarmonyOS 图片浏览和裁剪的视图组件
623浏览 • 1回复 待解决
HarmonyOS NavPathStack 用法
838浏览 • 1回复 待解决
getWindow().setBackground用法
5698浏览 • 1回复 待解决
HarmonyOS 查看项目整体依赖
1131浏览 • 1回复 待解决
import依赖较大如何优化
2220浏览 • 1回复 待解决
HarmonyOS axios用法咨询
879浏览 • 1回复 待解决
如何优化过多的import依赖
1152浏览 • 1回复 待解决
HarmonyOS UIViewer提供控件
434浏览 • 1回复 待解决
HarmonyOS Prop的用法
799浏览 • 1回复 待解决
关于measureTextSize的用法
1393浏览 • 1回复 待解决