• 【Java UI】HarmonyOS 如何集成SlidingDrawer组件


     参考资料

    SlidingDrawer

    api讲解

    • 如何集成

      修改entry的bulid.gradle,代码如下

       implementation 'io.openharmony.tpc.thirdlib:SlidingDrawer:1.0.2'
      

      需要在xml布局添加如下代码片段

      hollowsoft.slidingdrawer.SlidingDrawer>
      

      java 代码需要添加如下代码

      1. SlidingDrawer drawer = new SlidingDrawer(this);
      2. drawer.setOrientation(Component.VERTICAL);//todo 设置方向
      3. drawer.setHandle(new Component(this));//todo 这个必须设置
    • 抽屉布局打开

      drawer.animateOpen();
      
    • 抽屉布局关闭

      drawer.animateClose();
      

    代码运行

    • 绘画xml布局

      在xml布局中绘画一个导航布局和抽屉列表布局,具体参考xml布局注释,代码如下

      1. <DirectionalLayout ohos:id="$+id:main"
      2. xmlns:ohos="http://schemas.huawei.com/res/ohos"
      3. ohos:height="match_parent"
      4. ohos:width="match_parent"
      5. ohos:orientation="vertical">
      6. <DirectionalLayout
      7. ohos:background_element="blue"
      8. ohos:height="80vp"
      9. ohos:width="match_parent"
      10. ohos:orientation="horizontal">
      11. <Text
      12. ohos:left_margin="10vp"
      13. ohos:id="$+id:hos"
      14. ohos:width="80vp"
      15. ohos:height="match_parent"
      16. ohos:text_color="#ed6262"
      17. ohos:text_size="20vp"
      18. ohos:text="菜单"/>
      19. <Text
      20. ohos:width="0vp"
      21. ohos:weight="1"
      22. ohos:height="match_parent"
      23. ohos:text_alignment="center"
      24. ohos:text_size="20vp"
      25. ohos:text_color="#ffffff"
      26. ohos:text="界面标题"/>
      27. <Text
      28. ohos:right_margin="10vp"
      29. ohos:width="80vp"
      30. ohos:height="match_parent"
      31. ohos:text_color="#ed6262"
      32. ohos:text_size="20vp"/>
      33. DirectionalLayout>
      34. <hollowsoft.slidingdrawer.SlidingDrawer
      35. ohos:id="$+id:slide"
      36. ohos:background_element="#20000000"
      37. xmlns:ohos="http://schemas.huawei.com/res/ohos"
      38. ohos:height="500vp"
      39. ohos:width="match_parent"
      40. ohos:orientation="horizontal">
      41. <DirectionalLayout
      42. ohos:width="500vp"
      43. ohos:alignment="top"
      44. ohos:height="match_parent"
      45. ohos:orientation="vertical">
      46. <Text
      47. ohos:id="$+id:text_menu_one"
      48. ohos:height="80vp"
      49. ohos:background_element="#ffffff"
      50. ohos:width="match_parent"
      51. ohos:text_size="20vp"
      52. ohos:text="菜单一"/>
      53. <Text
      54. ohos:id="$+id:text_menu_two"
      55. ohos:top_margin="1vp"
      56. ohos:height="80vp"
      57. ohos:background_element="#ffffff"
      58. ohos:width="match_parent"
      59. ohos:text_size="20vp"
      60. ohos:text="菜单二"/>
      61. <Text
      62. ohos:id="$+id:text_menu_there"
      63. ohos:top_margin="1vp"
      64. ohos:height="80vp"
      65. ohos:background_element="#ffffff"
      66. ohos:width="match_parent"
      67. ohos:text_size="20vp"
      68. ohos:text="菜单三"/>
      69. DirectionalLayout>
      70. hollowsoft.slidingdrawer.SlidingDrawer>
      71. DirectionalLayout>
    • 实现java 代码

    ​ 给“菜单”实现点击事件,控制抽屉布局的打开,然后在实现菜单列表的点击事件,具体代码如下

    1. /**
    2. * Copyright (C) 2021 Huawei Device Co., Ltd.
    3. * Licensed under the Apache License, Version 2.0 (the "License");
    4. * you may not use this file except in compliance with the License.
    5. * You may obtain a copy of the License at
    6. *

    7. * http://www.apache.org/licenses/LICENSE-2.0
    8. *

    9. * Unless required by applicable law or agreed to in writing, software
    10. * distributed under the License is distributed on an "AS IS" BASIS,
    11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12. * See the License for the specific language governing permissions and
    13. * limitations under the License.
    14. */
    15. package com.newdemo.myapplication.slice;
    16. import com.newdemo.myapplication.ResourceTable;
    17. import hollowsoft.slidingdrawer.SlidingDrawer;
    18. import ohos.aafwk.ability.AbilitySlice;
    19. import ohos.aafwk.content.Intent;
    20. import ohos.agp.components.*;
    21. import ohos.agp.window.dialog.ToastDialog;
    22. /**
    23. * MainAbilitySlice class
    24. */
    25. public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    26. private SlidingDrawer drawer1;
    27. private Text LeftText;
    28. @Override
    29. public void onStart(Intent intent) {
    30. super.onStart(intent);
    31. super.setUIContent(ResourceTable.Layout_ability_main);
    32. LeftText=findComponentById(ResourceTable.Id_hos);//todo 菜单按钮
    33. drawer1 =findComponentById(ResourceTable.Id_slide);//todo 抽屉布局
    34. drawer1.setHandle(new Component(this));//todo 这个必须设置
    35. //todo 实现点击菜单的事件
    36. LeftText.setClickedListener(new Component.ClickedListener() {
    37. @Override
    38. public void onClick(Component component) {
    39. drawer1.animateOpen();
    40. }
    41. });
    42. //todo 给菜单列表实现点击事件
    43. findComponentById(ResourceTable.Id_text_menu_one).setClickedListener(this);
    44. findComponentById(ResourceTable.Id_text_menu_two).setClickedListener(this);
    45. findComponentById(ResourceTable.Id_text_menu_there).setClickedListener(this);
    46. }
    47. /**
    48. * 重写点击事件的按钮事件,并给出提示,关闭抽屉布局
    49. * @param component
    50. */
    51. @Override
    52. public void onClick(Component component) {
    53. String myText="";
    54. switch (component.getId()){
    55. case ResourceTable.Id_text_menu_one:
    56. myText="菜单一";
    57. break;
    58. case ResourceTable.Id_text_menu_two:
    59. myText="菜单二";
    60. break;
    61. case ResourceTable.Id_text_menu_there:
    62. myText="菜单三";
    63. break;
    64. }
    65. //todo 提示
    66. new ToastDialog(getContext())
    67. .setText(myText)
    68. .show();
    69. //todo 关闭 抽屉列表
    70. if(drawer1.isOpened()){
    71. drawer1.animateClose();
    72. }
    73. }
    74. }

    运行效果

    a5d25e72929984679022f3d067a999a4_577x982.gif%40900-0-90-f.gif

     欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

  • 相关阅读:
    搭建虚拟通道
    小程序开发平台源码系统+万能门店小程序功能+完整的搭建教程
    Flask练手
    养猫喂国产还是进口生骨肉冻干比较安心?全网热门养猫人必备生骨肉冻干
    Java基础知识面试题(三)(英语答案)
    TensorFlow ImportError: initialization failed
    高精度数字电容传感芯片-MDC04
    Linux 高级指令
    常用设计模式学习总结
    一幅长文细学JavaScript(七)——一幅长文系列
  • 原文地址:https://blog.csdn.net/weixin_44708240/article/details/125896267