HarmonyOS官方模板学习之Category Ability(Java)

想了解更多内容,请访问:

创新互联专注于企业成都全网营销、网站重做改版、兰考网站定制设计、自适应品牌网站建设、H5页面制作购物商城网站建设、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为兰考等各大城市提供网站开发制作服务。

和华为官方合作共建的鸿蒙技术社区

https://harmonyos.

Category Ability(Java)

介绍

使用Java语言开发,用于Phone设备的Feature Ability模板,使用XML布局,显示分类页效果。

搭建环境

安装DevEco Studio,详情请参考DevEco Studio下载。

设置DevEco Studio开发环境,DevEco Studio开发环境需要依赖于网络环境,需要连接上网络才能确保工具的正常使用,可以根据如下两种情况来配置开发环境:

如果可以直接访问Internet,只需进行下载HarmonyOS SDK操作。

如果网络不能直接访问Internet,需要通过代理服务器才可以访问,请参考配置开发环境。

代码结构解读

注意:'#'代表注释

功能逻辑代码

 
 
 
 
  1. ├─java 
  2. │  └─com 
  3. │      └─buty 
  4. │          └─categoryabilityjava 
  5. │              │  MainAbility.java 
  6. │              │  MyApplication.java 
  7. │              │ 
  8. │              ├─data 
  9. │              │      CategoryData.java #用来构造分类数据 
  10. │              │ 
  11. │              ├─model 
  12. │              │      CategoryList.java #分类列表模型 
  13. │              │      Item.java #子项模型 
  14. │              │      ItemChild.java #小子项模型 
  15. │              │      ItemList.java #子项列表模型 
  16. │              │ 
  17. │              ├─provider 
  18. │              │      CategoryListProvider.java #继承自BaseItemProvider为ListContainer提供数据 
  19. │              │ 
  20. │              ├─slice 
  21. │              │      MainAbilitySlice.java #主页片 
  22. │              │ 
  23. │              ├─utils 
  24. │              │      ElementUtil.java #获取element颜色值, 
  25. │              │      LogUtils.java #日志工具类 
  26. │              │      Toast.java #Toast工具类 
  27. │              │ 
  28. │              └─view 
  29. │                      GridAdapter.java #grid适配器,适配不同的gridview 
  30. │                      GridView.java #自定义组件,类似grid,继承自TableLayout并且实现Component.LayoutRefreshedListener 
  31. │ 

布局及样式代码

 
 
 
 
  1. └─resources 
  2.     ├─base 
  3.     │  ├─element 
  4.     │  │      color.json 
  5.     │  │      float.json 
  6.     │  │      string.json 
  7.     │  │ 
  8.     │  ├─graphic 
  9.     │  │      background_ability_main.xml 
  10.     │  │      background_item_grid_per.xml 
  11.     │  │      background_search_bar.xml 
  12.     │  │ 
  13.     │  ├─layout 
  14.     │  │      ability_main.xml  #主页面 
  15.     │  │      category_list_per.xml #每个分类组件 
  16.     │  │      item_child_per.xml #分类下面的子类别 
  17.     │  │      item_child_per_grid_per.xml #子类别下的网格项组件 
  18.     │  │      item_grid_per.xml #分类下面的网格项 
  19.     │  │      search_bar.xml #search组件 
  20.     │  │      tab.xml #单个tab组件 
  21.     │  │      title_bar.xml #标题组件 
  22.     │  │ 
  23.     │  ├─media 
  24.     │  │      addIcon.png 
  25.     │  │      icon.png 
  26.     │  │      icon_actived.png 
  27.     │  │      icon_normal.png 
  28.     │  │      rightIcon.png 
  29.     │  │      searchIcon.png 
  30.     │  │ 
  31.     │  └─profile 
  32.     ├─en 
  33.     │  └─element 
  34.     │          string.json 
  35.     │ 
  36.     ├─rawfile 
  37.     └─zh 
  38.         └─element 
  39.                 string.json 

效果展示

我们先看一下运行起来的效果

相关权限

不需要额外申请权限

页面布局

1.ability_main.xml #主页面

页面的布局包括DependentLayout和DirectionalLayout布局。

包括ListContainer、ScrollView、自定义的GridView 等组件

还使用了include标签来导入预设的组件,导入了search组件和标题组件。

2.category_list_per.xml #每个分类组件

分类组件比较简单,DirectionalLayout包含一个Text。

 
 
 
 
  1.  
  2.  
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_content" 
  5.     ohos:width="match_parent" 
  6.     ohos:bottom_margin="$float:defaultMargin" 
  7.     ohos:orientation="vertical" 
  8.     ohos:top_margin="$float:defaultMargin"> 
  9.  
  10.     
  11.         ohos:id="$+id:categoryListPerText" 
  12.         ohos:height="match_parent" 
  13.         ohos:width="match_parent" 
  14.         ohos:bottom_padding="$float:componentPadding" 
  15.         ohos:end_padding="$float:defaultPadding" 
  16.         ohos:multiple_lines="true" 
  17.         ohos:text_alignment="vertical_center|left" 
  18.         ohos:text_size="16vp" 
  19.         ohos:top_padding="$float:componentPadding"/> 

3.item_child_per.xml #分类下面的子类别

包含了一个自定义的GridView,我们可以学习一下如何自定义组件。

4.item_child_per_grid_per.xml #子类别下的网格项组件

比较简单

 
 
 
 
  1.  
  2.  
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_content" 
  5.     ohos:width="60vp" 
  6.     ohos:alpha="0" 
  7.     ohos:top_margin="$float:defaultMargin"> 
  8.  
  9.     
  10.         ohos:height="60vp" 
  11.         ohos:width="match_parent" 
  12.         ohos:background_element="$graphic:background_item_grid_per"/> 
  13.  
  14.     
  15.         ohos:id="$+id:itemChildPerGridPerText" 
  16.         ohos:height="match_content" 
  17.         ohos:width="match_parent" 
  18.         ohos:bottom_padding="$float:componentPadding" 
  19.         ohos:multiple_lines="true" 
  20.         ohos:text_alignment="center" 
  21.         ohos:text_size="16vp" 
  22.         ohos:top_padding="$float:componentPadding"/> 
  23.  

5.item_grid_per.xml #分类下面的网格项

 
 
 
 
  1.  
  2.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.     ohos:height="match_content" 
  4.     ohos:width="108vp" 
  5.     ohos:background_element="$graphic:background_item_grid_per"> 
  6.  
  7.     
  8.         ohos:id="$+id:itemGridPerImage" 
  9.         ohos:height="65vp" 
  10.         ohos:width="match_parent"/> 
  11.  
  12.     
  13.         ohos:id="$+id:itemGridPerText" 
  14.         ohos:height="match_content" 
  15.         ohos:width="match_parent" 
  16.         ohos:align_parent_start="true" 
  17.         ohos:align_parent_top="true" 
  18.         ohos:bottom_padding="$float:componentPadding" 
  19.         ohos:end_padding="$float:defaultPadding" 
  20.         ohos:multiple_lines="true" 
  21.         ohos:start_padding="$float:defaultPadding" 
  22.         ohos:text_alignment="vertical_center" 
  23.         ohos:text_size="16vp" 
  24.         ohos:top_padding="$float:componentPadding"/> 
  25.  

6.search_bar.xml #search组件

 
 
 
 
  1.  
  2.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.     ohos:height="match_content" 
  4.     ohos:width="match_parent" 
  5.     ohos:background_element="$color:colorSubBackground"> 
  6.  
  7.     
  8.         ohos:height="match_content" 
  9.         ohos:width="match_parent" 
  10.         ohos:background_element="$graphic:background_search_bar" 
  11.         ohos:bottom_margin="$float:componentPadding" 
  12.         ohos:end_margin="$float:defaultMargin" 
  13.         ohos:start_margin="$float:defaultMargin" 
  14.         ohos:top_margin="$float:componentPadding"> 
  15.  
  16.         
  17.             ohos:id="$+id:searchImage" 
  18.             ohos:height="match_content" 
  19.             ohos:width="match_content" 
  20.             ohos:image_src="$media:searchIcon" 
  21.             ohos:start_margin="$float:defaultMargin" 
  22.             ohos:vertical_center="true"/> 
  23.  
  24.         
  25.             ohos:id="$+id:searchTextField" 
  26.             ohos:height="match_content" 
  27.             ohos:width="match_parent" 
  28.             ohos:bottom_padding="$float:componentPadding" 
  29.             ohos:element_cursor_bubble="#00000000" 
  30.             ohos:end_of="$id:searchImage" 
  31.             ohos:end_padding="$float:defaultPadding" 
  32.             ohos:hint="$string:search" 
  33.             ohos:multiple_lines="false" 
  34.             ohos:start_padding="$float:defaultPadding" 
  35.             ohos:text_alignment="vertical_center" 
  36.             ohos:text_size="16fp" 
  37.             ohos:top_padding="$float:componentPadding" 
  38.             ohos:vertical_center="true"/> 
  39.      
  40.  
  41.  

7.tab.xml #单个tab组件

 
 
 
 
  1.  
  2.  
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:id="$+id:tab" 
  5.     ohos:height="match_content" 
  6.     ohos:width="0vp" 
  7.     ohos:alignment="center" 
  8.     ohos:orientation="vertical" 
  9.     ohos:weight="1"> 
  10.  
  11.     
  12.         ohos:id="$+id:bottom_tab_button_image" 
  13.         ohos:height="$float:heightTab" 
  14.         ohos:width="$float:widthTab"/> 
  15.  
  16.     
  17.         ohos:id="$+id:bottom_tab_button_text" 
  18.         ohos:height="match_content" 
  19.         ohos:width="match_parent" 
  20.         ohos:text_alignment="top|horizontal_center" 
  21.         ohos:text_color="$color:colorTabTextNormal" 
  22.         ohos:text_size="$float:textSizeTab" 
  23.         ohos:top_margin="2vp"/> 
  24.  
  25.  

8.title_bar.xml #标题组件

两个Text 加上一个Image

 
 
 
 
  1.  
  2.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.     ohos:height="$float:defaultHeight" 
  4.     ohos:width="match_parent" 
  5.     ohos:background_element="$color:colorSubBackground"> 
  6.  
  7.     
  8.         ohos:id="$+id:title_text" 
  9.         ohos:height="match_content" 
  10.         ohos:width="match_content" 
  11.         ohos:align_parent_bottom="true" 
  12.         ohos:bottom_padding="$float:defaultPadding" 
  13.         ohos:end_padding="$float:defaultPadding" 
  14.         ohos:multiple_lines="true" 
  15.         ohos:start_padding="$float:defaultPadding" 
  16.         ohos:text="$string:entry_MainAbility" 
  17.         ohos:text_color="$color:appbar_title_color" 
  18.         ohos:text_size="$float:appbar_title_text_size"/> 
  19.  
  20.     
  21.         ohos:id="$+id:describe_text" 
  22.         ohos:height="match_content" 
  23.         ohos:width="match_content" 
  24.         ohos:align_parent_bottom="true" 
  25.         ohos:bottom_padding="$float:defaultPadding" 
  26.         ohos:end_of="$id:title_text" 
  27.         ohos:multiple_lines="true" 
  28.         ohos:text="$string:describe" 
  29.         ohos:text_color="$color:appbar_subtitle_color" 
  30.         ohos:text_size="$float:appbar_text_size"/> 
  31.  
  32.     
  33.         ohos:height="match_content" 
  34.         ohos:width="match_content" 
  35.         ohos:align_parent_bottom="true" 
  36.         ohos:align_parent_end="true" 
  37.         ohos:bottom_padding="$float:defaultPadding" 
  38.         ohos:end_padding="$float:defaultPadding" 
  39.         ohos:image_src="$media:addIcon"/> 
  40.  
  41.  

业务逻辑

1.CategoryListProvider

部分关键代码

 
 
 
 
  1. @Override 
  2.     public Component getComponent(int index, Component component, ComponentContainer componentContainer) { 
  3.  
  4.  
  5.         Component itemComponent = component; 
  6.         CategoryListViewHolder viewHolder; 
  7.  
  8.         //获取布局组件 
  9.         if (itemComponent == null) { 
  10.             itemComponent = 
  11.                     LayoutScatter.getInstance(context) 
  12.                             .parse(ResourceTable.Layout_category_list_per, componentContainer, false); 
  13.         } 
  14.  
  15.         //初始化ViewHolder 
  16.         viewHolder = new CategoryListViewHolder(); 
  17.         viewHolder.text = (Text) itemComponent.findComponentById(ResourceTable.Id_categoryListPerText); 
  18.         viewHolder.text.setText(getItem(index)); 
  19.  
  20.         //对齐方式 
  21.         if (TextTool.isLayoutRightToLeft(Locale.getDefault())) { 
  22.             viewHolder.text.setTextAlignment(TextAlignment.VERTICAL_CENTER | TextAlignment.RIGHT); 
  23.         } else { 
  24.             viewHolder.text.setTextAlignment(TextAlignment.VERTICAL_CENTER | TextAlignment.LEFT); 
  25.         } 
  26.         //获取color.json中的颜色值,设置Text组件的颜色 
  27.         if (selectIndex == index) { 
  28.             viewHolder.text.setTextColor(new Color(ElementUtil.getColor(context, ResourceTable.Color_primary_color))); 
  29.         } else { 
  30.             viewHolder.text.setTextColor(new Color(ElementUtil.getColor(context, ResourceTable.Color_primary_default))); 
  31.         } 
  32.  
  33.         return itemComponent; 
  34.     } 
  35.  
  36.     /** 
  37.      * 定义视图持有者 
  38.      */ 
  39.     private static class CategoryListViewHolder { 
  40.         Text text; 
  41.     } 

2.ElementUtil

学习如何获取element color.json的颜色值,用java代码设置给组件。

 
 
 
 
  1. package com.buty.categoryabilityjava.utils; 
  2.  
  3. import ohos.agp.utils.Color; 
  4. import ohos.app.Context; 
  5. import ohos.global.resource.NotExistException; 
  6. import ohos.global.resource.WrongTypeException; 
  7.  
  8. import java.io.IOException; 
  9. import java.util.logging.Level; 
  10. import java.util.logging.Logger; 
  11.  
  12. /** 
  13.  * The ElementUtil 
  14.  */ 
  15. public class ElementUtil { 
  16.     /** 
  17.      * The getColor 
  18.      * 
  19.      * @param context    context 
  20.      * @param resColorId resColorId 
  21.      * @return color 
  22.      */ 
  23.     public static int getColor(Context context, int resColorId) { 
  24.         try { 
  25.             // 
  26.             String strColor = context.getResourceManager().getElement(resColorId).getString(); 
  27.  
  28.             if (strColor.length() == 7) { 
  29.                 //返回7位颜色值 
  30.                 return context.getResourceManager().getElement(resColorId).getColor(); 
  31.             } else if (strColor.length() == 9) { 
  32.                 //返回9位颜色值 
  33.                 return Color.getIntColor(strColor); 
  34.             } else { 
  35.                 return 0x000000; 
  36.             } 
  37.         } catch (WrongTypeException | NotExistException | IOException e) { 
  38.             Logger.getLogger(ElementUtil.class.getName()).log(Level.SEVERE, e.getMessage()); 
  39.         } 
  40.         return 0x000000; 
  41.     } 

3.GridAdapter

 
 
 
 
  1. package com.buty.categoryabilityjava.view; 
  2.  
  3. import com.buty.categoryabilityjava.utils.LogUtils; 
  4.  
  5. import ohos.agp.components.Component; 
  6. import ohos.agp.components.Image; 
  7. import ohos.agp.components.LayoutScatter; 
  8. import ohos.agp.components.Text; 
  9. import ohos.app.Context; 
  10.  
  11. import java.util.ArrayList; 
  12. import java.util.List; 
  13. import java.util.Map; 
  14. import java.util.stream.IntStream; 
  15.  
  16. /** 
  17.  * The GridAdapter, adapter of the gridView 
  18.  */ 
  19. public class GridAdapter { 
  20.     private final List componentList = new ArrayList<>(); 
  21.  
  22.     /** 
  23.      * The GridAdapter, adapter of the gridView 
  24.      * 
  25.      * @param context context 
  26.      * @param xmlId   xmlId, the xml layout of each item of gridView 
  27.      * @param data    data, the number of key-value also the number of item 
  28.      * @param from    from, the collection for the key in de data 
  29.      * @param to      to, the value from the data to the component target 
  30.      */ 
  31.     public GridAdapter(Context context, int xmlId, List> data, String[] from, int[] to) { 
  32.         for (Map datum : data) { 
  33.             // get component for the xml 
  34.             Component layoutComponent = LayoutScatter.getInstance(context).parse(xmlId, null, false); 
  35.  
  36.             // create each itemComponent for the args:data、from、to 
  37.             IntStream.range(0, to.length) 
  38.                     .forEach( 
  39.                             index -> { 
  40.                                 Component itemComponent = layoutComponent.findComponentById(to[index]); 
  41.                                 if (itemComponent instanceof Image) { 
  42.                                     Image image = (Image) itemComponent; 
  43.                                     if (datum.get(from[index]) instanceof int[]) { 
  44.                                         try { 
  45.                                             image.setPixelMap(((int[]) datum.get(from[index]))[index]); 
  46.                                         } catch (IndexOutOfBoundsException e) { 
  47.                                             LogUtils.error("GridAdapter", "IndexOutOfBoundsException"); 
  48.                                         } 
  49.                                     } 
  50.                                 } else { 
  51.                                     if (itemComponent instanceof Text) { 
  52.                                         Text text = (Text) itemComponent; 
  53.                                         if (datum.get(from[index]) instanceof String[]) { 
  54.                                             try { 
  55.                                                 text.setText(((String[]) datum.get(from[index]))[index]); 
  56.                                             } catch (IndexOutOfBoundsException e) { 
  57.                                                 LogUtils.error("GridAdapter", "IndexOutOfBoundsException"); 
  58.                                                 text.setText("null"); 
  59.                                             } 
  60.                                         } else if (datum.get(from[index]) instanceof int[]) { 
  61.                                             try { 
  62.                                                 text.setText(((int[]) datum.get(from[index]))[index]); 
  63.                                             } catch (IndexOutOfBoundsException e) { 
  64.                                                 LogUtils.error("GridAdapter", "IndexOutOfBoundsException"); 
  65.                                                 text.setText("null"); 
  66.                                             } 
  67.                                         } else { 
  68.                                             text.setText("null"); 
  69.                                         } 
  70.                                     } 
  71.                                 } 
  72.                             }); 
  73.  
  74.             componentList.add(layoutComponent); 
  75.         } 
  76.     } 
  77.  
  78.     /** 
  79.      * The getComponentList 
  80.      * 
  81.      * @return componentList 
  82.      */ 
  83.     List getComponentList() { 
  84.         return componentList; 
  85.     } 

4.GridView

 
 
 
 
  1. package com.buty.categoryabilityjava.view; 
  2.  
  3. import ohos.agp.components.AttrSet; 
  4. import ohos.agp.components.Component; 
  5. import ohos.agp.components.ComponentContainer; 
  6. import ohos.agp.components.TableLayout; 
  7. import ohos.agp.utils.TextTool; 
  8. import ohos.app.Context; 
  9.  
  10. import java.util.Locale; 
  11.  
  12. /** 
  13.  * The GridView, a custom component like grid 
  14.  */ 
  15. public class GridView extends TableLayout implements Component.LayoutRefreshedListener { 
  16.     private OnItemClickListener onItemClickListener; 
  17.     private OnRefreshedListener onRefreshedListener; 
  18.  
  19.     private GridAdapter adapter; 
  20.  
  21.     // custom Attr 
  22.     private int columnMargin; 
  23.     private int rowMargin; 
  24.  
  25.     //add custom attr 
  26.     private int rowPadding; 
  27.  
  28.     private boolean isOnRefreshed = false; 
  29.  
  30.     public GridView(Context context) { 
  31.         super(contex

    本文标题:HarmonyOS官方模板学习之Category Ability(Java)
    网页链接:http://www.shufengxianlan.com/qtweb/news13/482263.html

    网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

    广告

    声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联