新聞中心
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

創(chuàng)新互聯(lián)專(zhuān)注于企業(yè)成都全網(wǎng)營(yíng)銷(xiāo)、網(wǎng)站重做改版、蘭考網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5頁(yè)面制作、購(gòu)物商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為蘭考等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
和華為官方合作共建的鴻蒙技術(shù)社區(qū)
https://harmonyos.
Category Ability(Java)
介紹
使用Java語(yǔ)言開(kāi)發(fā),用于Phone設(shè)備的Feature Ability模板,使用XML布局,顯示分類(lèi)頁(yè)效果。
搭建環(huán)境
安裝DevEco Studio,詳情請(qǐng)參考DevEco Studio下載。
設(shè)置DevEco Studio開(kāi)發(fā)環(huán)境,DevEco Studio開(kāi)發(fā)環(huán)境需要依賴(lài)于網(wǎng)絡(luò)環(huán)境,需要連接上網(wǎng)絡(luò)才能確保工具的正常使用,可以根據(jù)如下兩種情況來(lái)配置開(kāi)發(fā)環(huán)境:
如果可以直接訪(fǎng)問(wèn)Internet,只需進(jìn)行下載HarmonyOS SDK操作。
如果網(wǎng)絡(luò)不能直接訪(fǎng)問(wèn)Internet,需要通過(guò)代理服務(wù)器才可以訪(fǎng)問(wèn),請(qǐng)參考配置開(kāi)發(fā)環(huán)境。
代碼結(jié)構(gòu)解讀
注意:'#'代表注釋
功能邏輯代碼
- ├─java
- │ └─com
- │ └─buty
- │ └─categoryabilityjava
- │ │ MainAbility.java
- │ │ MyApplication.java
- │ │
- │ ├─data
- │ │ CategoryData.java #用來(lái)構(gòu)造分類(lèi)數(shù)據(jù)
- │ │
- │ ├─model
- │ │ CategoryList.java #分類(lèi)列表模型
- │ │ Item.java #子項(xiàng)模型
- │ │ ItemChild.java #小子項(xiàng)模型
- │ │ ItemList.java #子項(xiàng)列表模型
- │ │
- │ ├─provider
- │ │ CategoryListProvider.java #繼承自BaseItemProvider為L(zhǎng)istContainer提供數(shù)據(jù)
- │ │
- │ ├─slice
- │ │ MainAbilitySlice.java #主頁(yè)片
- │ │
- │ ├─utils
- │ │ ElementUtil.java #獲取element顏色值,
- │ │ LogUtils.java #日志工具類(lèi)
- │ │ Toast.java #Toast工具類(lèi)
- │ │
- │ └─view
- │ GridAdapter.java #grid適配器,適配不同的gridview
- │ GridView.java #自定義組件,類(lèi)似grid,繼承自TableLayout并且實(shí)現(xiàn)Component.LayoutRefreshedListener
- │
布局及樣式代碼
- └─resources
- ├─base
- │ ├─element
- │ │ color.json
- │ │ float.json
- │ │ string.json
- │ │
- │ ├─graphic
- │ │ background_ability_main.xml
- │ │ background_item_grid_per.xml
- │ │ background_search_bar.xml
- │ │
- │ ├─layout
- │ │ ability_main.xml #主頁(yè)面
- │ │ category_list_per.xml #每個(gè)分類(lèi)組件
- │ │ item_child_per.xml #分類(lèi)下面的子類(lèi)別
- │ │ item_child_per_grid_per.xml #子類(lèi)別下的網(wǎng)格項(xiàng)組件
- │ │ item_grid_per.xml #分類(lèi)下面的網(wǎng)格項(xiàng)
- │ │ search_bar.xml #search組件
- │ │ tab.xml #單個(gè)tab組件
- │ │ title_bar.xml #標(biāo)題組件
- │ │
- │ ├─media
- │ │ addIcon.png
- │ │ icon.png
- │ │ icon_actived.png
- │ │ icon_normal.png
- │ │ rightIcon.png
- │ │ searchIcon.png
- │ │
- │ └─profile
- ├─en
- │ └─element
- │ string.json
- │
- ├─rawfile
- └─zh
- └─element
- string.json
效果展示
我們先看一下運(yùn)行起來(lái)的效果
相關(guān)權(quán)限
不需要額外申請(qǐng)權(quán)限
頁(yè)面布局
1.ability_main.xml #主頁(yè)面
頁(yè)面的布局包括DependentLayout和DirectionalLayout布局。
包括ListContainer、ScrollView、自定義的GridView 等組件
還使用了include標(biāo)簽來(lái)導(dǎo)入預(yù)設(shè)的組件,導(dǎo)入了search組件和標(biāo)題組件。
2.category_list_per.xml #每個(gè)分類(lèi)組件
分類(lèi)組件比較簡(jiǎn)單,DirectionalLayout包含一個(gè)Text。
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:bottom_margin="$float:defaultMargin"
- ohos:orientation="vertical"
- ohos:top_margin="$float:defaultMargin">
- ohos:id="$+id:categoryListPerText"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:bottom_padding="$float:componentPadding"
- ohos:end_padding="$float:defaultPadding"
- ohos:multiple_lines="true"
- ohos:text_alignment="vertical_center|left"
- ohos:text_size="16vp"
- ohos:top_padding="$float:componentPadding"/>
3.item_child_per.xml #分類(lèi)下面的子類(lèi)別
包含了一個(gè)自定義的GridView,我們可以學(xué)習(xí)一下如何自定義組件。
4.item_child_per_grid_per.xml #子類(lèi)別下的網(wǎng)格項(xiàng)組件
比較簡(jiǎn)單
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_content"
- ohos:width="60vp"
- ohos:alpha="0"
- ohos:top_margin="$float:defaultMargin">
- ohos:height="60vp"
- ohos:width="match_parent"
- ohos:background_element="$graphic:background_item_grid_per"/>
- ohos:id="$+id:itemChildPerGridPerText"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:bottom_padding="$float:componentPadding"
- ohos:multiple_lines="true"
- ohos:text_alignment="center"
- ohos:text_size="16vp"
- ohos:top_padding="$float:componentPadding"/>
5.item_grid_per.xml #分類(lèi)下面的網(wǎng)格項(xiàng)
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_content"
- ohos:width="108vp"
- ohos:background_element="$graphic:background_item_grid_per">
- ohos:id="$+id:itemGridPerImage"
- ohos:height="65vp"
- ohos:width="match_parent"/>
- ohos:id="$+id:itemGridPerText"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:align_parent_start="true"
- ohos:align_parent_top="true"
- ohos:bottom_padding="$float:componentPadding"
- ohos:end_padding="$float:defaultPadding"
- ohos:multiple_lines="true"
- ohos:start_padding="$float:defaultPadding"
- ohos:text_alignment="vertical_center"
- ohos:text_size="16vp"
- ohos:top_padding="$float:componentPadding"/>
6.search_bar.xml #search組件
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:background_element="$color:colorSubBackground">
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:background_element="$graphic:background_search_bar"
- ohos:bottom_margin="$float:componentPadding"
- ohos:end_margin="$float:defaultMargin"
- ohos:start_margin="$float:defaultMargin"
- ohos:top_margin="$float:componentPadding">
- ohos:id="$+id:searchImage"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:image_src="$media:searchIcon"
- ohos:start_margin="$float:defaultMargin"
- ohos:vertical_center="true"/>
- ohos:id="$+id:searchTextField"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:bottom_padding="$float:componentPadding"
- ohos:element_cursor_bubble="#00000000"
- ohos:end_of="$id:searchImage"
- ohos:end_padding="$float:defaultPadding"
- ohos:hint="$string:search"
- ohos:multiple_lines="false"
- ohos:start_padding="$float:defaultPadding"
- ohos:text_alignment="vertical_center"
- ohos:text_size="16fp"
- ohos:top_padding="$float:componentPadding"
- ohos:vertical_center="true"/>
7.tab.xml #單個(gè)tab組件
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:id="$+id:tab"
- ohos:height="match_content"
- ohos:width="0vp"
- ohos:alignment="center"
- ohos:orientation="vertical"
- ohos:weight="1">
- ohos:id="$+id:bottom_tab_button_image"
- ohos:height="$float:heightTab"
- ohos:width="$float:widthTab"/>
- ohos:id="$+id:bottom_tab_button_text"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:text_alignment="top|horizontal_center"
- ohos:text_color="$color:colorTabTextNormal"
- ohos:text_size="$float:textSizeTab"
- ohos:top_margin="2vp"/>
8.title_bar.xml #標(biāo)題組件
兩個(gè)Text 加上一個(gè)Image
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="$float:defaultHeight"
- ohos:width="match_parent"
- ohos:background_element="$color:colorSubBackground">
- ohos:id="$+id:title_text"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:align_parent_bottom="true"
- ohos:bottom_padding="$float:defaultPadding"
- ohos:end_padding="$float:defaultPadding"
- ohos:multiple_lines="true"
- ohos:start_padding="$float:defaultPadding"
- ohos:text="$string:entry_MainAbility"
- ohos:text_color="$color:appbar_title_color"
- ohos:text_size="$float:appbar_title_text_size"/>
- ohos:id="$+id:describe_text"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:align_parent_bottom="true"
- ohos:bottom_padding="$float:defaultPadding"
- ohos:end_of="$id:title_text"
- ohos:multiple_lines="true"
- ohos:text="$string:describe"
- ohos:text_color="$color:appbar_subtitle_color"
- ohos:text_size="$float:appbar_text_size"/>
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:align_parent_bottom="true"
- ohos:align_parent_end="true"
- ohos:bottom_padding="$float:defaultPadding"
- ohos:end_padding="$float:defaultPadding"
- ohos:image_src="$media:addIcon"/>
業(yè)務(wù)邏輯
1.CategoryListProvider
部分關(guān)鍵代碼
- @Override
- public Component getComponent(int index, Component component, ComponentContainer componentContainer) {
- Component itemComponent = component;
- CategoryListViewHolder viewHolder;
- //獲取布局組件
- if (itemComponent == null) {
- itemComponent =
- LayoutScatter.getInstance(context)
- .parse(ResourceTable.Layout_category_list_per, componentContainer, false);
- }
- //初始化ViewHolder
- viewHolder = new CategoryListViewHolder();
- viewHolder.text = (Text) itemComponent.findComponentById(ResourceTable.Id_categoryListPerText);
- viewHolder.text.setText(getItem(index));
- //對(duì)齊方式
- if (TextTool.isLayoutRightToLeft(Locale.getDefault())) {
- viewHolder.text.setTextAlignment(TextAlignment.VERTICAL_CENTER | TextAlignment.RIGHT);
- } else {
- viewHolder.text.setTextAlignment(TextAlignment.VERTICAL_CENTER | TextAlignment.LEFT);
- }
- //獲取color.json中的顏色值,設(shè)置Text組件的顏色
- if (selectIndex == index) {
- viewHolder.text.setTextColor(new Color(ElementUtil.getColor(context, ResourceTable.Color_primary_color)));
- } else {
- viewHolder.text.setTextColor(new Color(ElementUtil.getColor(context, ResourceTable.Color_primary_default)));
- }
- return itemComponent;
- }
- /**
- * 定義視圖持有者
- */
- private static class CategoryListViewHolder {
- Text text;
- }
2.ElementUtil
學(xué)習(xí)如何獲取element color.json的顏色值,用java代碼設(shè)置給組件。
- package com.buty.categoryabilityjava.utils;
- import ohos.agp.utils.Color;
- import ohos.app.Context;
- import ohos.global.resource.NotExistException;
- import ohos.global.resource.WrongTypeException;
- import java.io.IOException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- * The ElementUtil
- */
- public class ElementUtil {
- /**
- * The getColor
- *
- * @param context context
- * @param resColorId resColorId
- * @return color
- */
- public static int getColor(Context context, int resColorId) {
- try {
- //
- String strColor = context.getResourceManager().getElement(resColorId).getString();
- if (strColor.length() == 7) {
- //返回7位顏色值
- return context.getResourceManager().getElement(resColorId).getColor();
- } else if (strColor.length() == 9) {
- //返回9位顏色值
- return Color.getIntColor(strColor);
- } else {
- return 0x000000;
- }
- } catch (WrongTypeException | NotExistException | IOException e) {
- Logger.getLogger(ElementUtil.class.getName()).log(Level.SEVERE, e.getMessage());
- }
- return 0x000000;
- }
- }
3.GridAdapter
- package com.buty.categoryabilityjava.view;
- import com.buty.categoryabilityjava.utils.LogUtils;
- import ohos.agp.components.Component;
- import ohos.agp.components.Image;
- import ohos.agp.components.LayoutScatter;
- import ohos.agp.components.Text;
- import ohos.app.Context;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.IntStream;
- /**
- * The GridAdapter, adapter of the gridView
- */
- public class GridAdapter {
- private final List
componentList = new ArrayList<>(); - /**
- * The GridAdapter, adapter of the gridView
- *
- * @param context context
- * @param xmlId xmlId, the xml layout of each item of gridView
- * @param data data, the number of key-value also the number of item
- * @param from from, the collection for the key in de data
- * @param to to, the value from the data to the component target
- */
- public GridAdapter(Context context, int xmlId, List
- for (Map
datum : data) { - // get component for the xml
- Component layoutComponent = LayoutScatter.getInstance(context).parse(xmlId, null, false);
- // create each itemComponent for the args:data、from、to
- IntStream.range(0, to.length)
- .forEach(
- index -> {
- Component itemComponent = layoutComponent.findComponentById(to[index]);
- if (itemComponent instanceof Image) {
- Image image = (Image) itemComponent;
- if (datum.get(from[index]) instanceof int[]) {
- try {
- image.setPixelMap(((int[]) datum.get(from[index]))[index]);
- } catch (IndexOutOfBoundsException e) {
- LogUtils.error("GridAdapter", "IndexOutOfBoundsException");
- }
- }
- } else {
- if (itemComponent instanceof Text) {
- Text text = (Text) itemComponent;
- if (datum.get(from[index]) instanceof String[]) {
- try {
- text.setText(((String[]) datum.get(from[index]))[index]);
- } catch (IndexOutOfBoundsException e) {
- LogUtils.error("GridAdapter", "IndexOutOfBoundsException");
- text.setText("null");
- }
- } else if (datum.get(from[index]) instanceof int[]) {
- try {
- text.setText(((int[]) datum.get(from[index]))[index]);
- } catch (IndexOutOfBoundsException e) {
- LogUtils.error("GridAdapter", "IndexOutOfBoundsException");
- text.setText("null");
- }
- } else {
- text.setText("null");
- }
- }
- }
- });
- componentList.add(layoutComponent);
- }
- }
- /**
- * The getComponentList
- *
- * @return componentList
- */
- List
getComponentList() { - return componentList;
- }
- }
4.GridView
- package com.buty.categoryabilityjava.view;
- import ohos.agp.components.AttrSet;
- import ohos.agp.components.Component;
- import ohos.agp.components.ComponentContainer;
- import ohos.agp.components.TableLayout;
- import ohos.agp.utils.TextTool;
- import ohos.app.Context;
- import java.util.Locale;
- /**
- * The GridView, a custom component like grid
- */
- public class GridView extends TableLayout implements Component.LayoutRefreshedListener {
- private OnItemClickListener onItemClickListener;
- private OnRefreshedListener onRefreshedListener;
- private GridAdapter adapter;
- // custom Attr
- private int columnMargin;
- private int rowMargin;
- //add custom attr
- private int rowPadding;
- private boolean isOnRefreshed = false;
- public GridView(Context context) {
- super(contex
文章名稱(chēng):HarmonyOS官方模板學(xué)習(xí)之Category Ability(Java)
分享路徑:http://fisionsoft.com.cn/article/coesjhd.html


咨詢(xún)
建站咨詢(xún)
