自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

鴻蒙HarmonyOS官方模板學(xué)習(xí) 之 Grid Ability(Java)

開發(fā) 后端 OpenHarmony
使用Java語言開發(fā),用于Phone設(shè)備的Feature Ability模板,使用XML布局,顯示內(nèi)容為兩部分網(wǎng)格表,網(wǎng)格每行顯示4個(gè)項(xiàng)目,網(wǎng)格內(nèi)元素可進(jìn)行拖拽排序。

想了解更多內(nèi)容,請?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

Grid Ability(Java)

介紹

使用Java語言開發(fā),用于Phone設(shè)備的Feature Ability模板,使用XML布局,顯示內(nèi)容為兩部分網(wǎng)格表,網(wǎng)格每行顯示4個(gè)項(xiàng)目,網(wǎng)格內(nèi)元素可進(jìn)行拖拽排序。

搭建環(huán)境

安裝DevEco Studio,詳情請參考DevEco Studio下載。

設(shè)置DevEco Studio開發(fā)環(huán)境,DevEco Studio開發(fā)環(huán)境需要依賴于網(wǎng)絡(luò)環(huán)境,需要連接上網(wǎng)絡(luò)才能確保工具的正常使用,可以根據(jù)如下兩種情況來配置開發(fā)環(huán)境:

如果可以直接訪問Internet,只需進(jìn)行下載HarmonyOS SDK操作。

如果網(wǎng)絡(luò)不能直接訪問Internet,需要通過代理服務(wù)器才可以訪問,請參考配置開發(fā)環(huán)境。

代碼結(jié)構(gòu)解讀

注意:'#'代表注釋

后臺功能

  1. gridabilityjava 
  2.      │  MainAbility.java 
  3.      │  MyApplication.java 
  4.      │ 
  5.      ├─component 
  6.      │      DragLayout.java #自定義的拖拽功能組件 
  7.      │      GridView.java #自定義的Grid視圖組件,extends TableLayout 
  8.      │ 
  9.      ├─model 
  10.      │      GridItemInfo.java #Grid item 模型 
  11.      │ 
  12.      ├─provider 
  13.      │      GridAdapter.java #給Grid提供實(shí)例化好的item 組件列表;提供了計(jì)算單個(gè)item的寬度的方法 
  14.      │ 
  15.      ├─slice 
  16.      │      MainAbilitySlice.java #主能力頁,負(fù)責(zé)實(shí)例化自定義的DragLayout拖拽組件 
  17.      │ 
  18.      └─utils 
  19.              AppUtils.java #工具類,提供了從element資源中中獲取value;獲取屏幕的坐標(biāo)的方法 

這是幾個(gè)java類之間的關(guān)系

HarmonyOS官方模板學(xué)習(xí) 之 Grid Ability(Java)-鴻蒙HarmonyOS技術(shù)社區(qū)

頁面資源

  1. resources 
  2.     ├─base 
  3.     │  ├─element 
  4.     │  │      color.json 
  5.     │  │      float.json 
  6.     │  │      integer.json 
  7.     │  │      string.json 
  8.     │  │ 
  9.     │  ├─graphic 
  10.     │  │      background_bottom_button.xml  #頁面底部按鈕形狀 
  11.     │  │      background_bottom_layout.xml  #頁面底部布局形狀 
  12.     │  │      background_item_active_button.xml #grid item 激活形狀 
  13.     │  │      background_item_button.xml    #grid item 默認(rèn)形狀 
  14.     │  │      background_table_layout_down.xml  #下面的 grid 形狀 
  15.     │  │      background_table_layout_up.xml    #上面的 grid 形狀 
  16.     │  │ 
  17.     │  ├─layout 
  18.     │  │      ability_main.xml #主顯示頁面 
  19.     │  │      app_bar_layout.xml #app工具欄布局頁面 
  20.     │  │      grid_item.xml #單個(gè)grid item布局頁面 
  21.     │  │ 
  22.     │  ├─media 
  23.     │  │      5G.png 
  24.     │  │      back.png 
  25.     │  │      back_white.png 

頁面布局

ability_main.xml #主顯示頁

此頁面由DirectionalLayout、StackLayout、DependentLayout 布局構(gòu)成,整體布局是上下布局。

上面時(shí)app工具欄,使用了StackLayout布局,通過includ標(biāo)簽引入到主頁面。

下面是支持拖拽的GridView,由DependentLayout 和DirectionalLayout布局組成,使用的組件有ScrollView、GridView、Text、Button、Image。

HarmonyOS官方模板學(xué)習(xí) 之 Grid Ability(Java)-鴻蒙HarmonyOS技術(shù)社區(qū)

app_bar_layout.xml #app工具欄布局頁面

HarmonyOS官方模板學(xué)習(xí) 之 Grid Ability(Java)-鴻蒙HarmonyOS技術(shù)社區(qū)

grid_item.xml #單個(gè)grid item布局頁面

HarmonyOS官方模板學(xué)習(xí) 之 Grid Ability(Java)-鴻蒙HarmonyOS技術(shù)社區(qū)

后臺邏輯

1.初始化上面的GridView

先構(gòu)建item模擬數(shù)據(jù)列表,將構(gòu)建好的數(shù)據(jù)傳遞給GridAdapter 初始化item組件列表,通過GridView.setAdapter方法給每個(gè)item組件綁定長按事件,并設(shè)置GridView的TAG屬性(TAG就是指上面的GridView還是下面的GridView)。

  1. /** 
  2.  * 初始化上面的Grid item 
  3.  */ 
  4. private void initUpListItem() { 
  5.     //構(gòu)建item模擬數(shù)據(jù)列表 
  6.     List<GridItemInfo> upperItemList = new ArrayList<>(); 
  7.     for (int i = 0; i < UP_ITEM_COUNT; i++) { 
  8.         int iconId = icons[i]; 
  9.         String text = texts[i]; 
  10.         upperItemList.add(new GridItemInfo(text, iconId, UP_GRID_TAG)); 
  11.     } 
  12.  
  13.     GridView gridView = (GridView) slice.findComponentById(ResourceTable.Id_grid_view_up); 
  14.     //將構(gòu)建好的數(shù)據(jù)傳遞給GridAdapter 初始化item組件列表 
  15.     GridAdapter adapter = new GridAdapter(slice.getContext(), upperItemList); 
  16.  
  17.     //通過GridView.setAdapter方法給每個(gè)item組件綁定長按事件 
  18.     gridView.setAdapter(adapter, longClickListener); 
  19.     //設(shè)置GridView的TAG屬性 
  20.     gridView.setTag(UP_GRID_TAG); 

2.初始化下面的GridView

邏輯同上

  1. /** 
  2.  * 初始化下面的Grid item 
  3.  */ 
  4. private void initDownListItem() { 
  5.     String itemText = AppUtils.getStringResource(slice.getContext(), ResourceTable.String_grid_item_text); 
  6.     List<GridItemInfo> lowerItemList = new ArrayList<>(); 
  7.     for (int i = 0; i < DOWN_ITEM_COUNT; i++) { 
  8.         //隨意取的圖標(biāo) 
  9.         int iconId = icons[i + 5]; 
  10.         String text = texts[i + 5]; 
  11.         lowerItemList.add(new GridItemInfo(text, iconId, DOWN_GRID_TAG)); 
  12.     } 
  13.  
  14.     if (slice.findComponentById(ResourceTable.Id_grid_view_down) instanceof GridView) { 
  15.         GridView gridView = (GridView) slice.findComponentById(ResourceTable.Id_grid_view_down); 
  16.         GridAdapter adapter = new GridAdapter(slice.getContext(), lowerItemList); 
  17.         gridView.setAdapter(adapter, longClickListener); 
  18.         gridView.setTag(DOWN_GRID_TAG); 
  19.     } 

 3.初始化底部的按鈕

這個(gè)地方做了一個(gè)屏幕適配,就是根據(jù)屏幕的寬度、邊距來設(shè)置按鈕的寬度,

同時(shí)添加了按鈕的監(jiān)聽事件,點(diǎn)擊按鈕 關(guān)閉當(dāng)前Ability。

  1. /** 
  2.  * Calculating button width based on screen width. 
  3.  * The actual width is the screen width minus the margin of the buttons. 
  4.  * 設(shè)置底部 2個(gè)按鈕的寬度 
  5.  */ 
  6. private void initBottomItem() { 
  7.     int screenWidth = AppUtils.getScreenInfo(slice.getContext()).getPointXToInt(); 
  8.     //計(jì)算按鈕寬度 
  9.     int buttonWidth = (screenWidth - AttrHelper.vp2px(80, slice.getContext())) / 2; 
  10.  
  11.     Component leftButton = slice.findComponentById(ResourceTable.Id_bottom_left_button); 
  12.     leftButton.setWidth(buttonWidth); 
  13.     //關(guān)閉Ability 
  14.     leftButton.setClickedListener(component -> slice.terminateAbility()); 
  15.  
  16.     Component rightButton = slice.findComponentById(ResourceTable.Id_bottom_right_button); 
  17.     rightButton.setWidth(buttonWidth); 
  18.     //關(guān)閉Ability 
  19.     rightButton.setClickedListener(component -> slice.terminateAbility()); 
  20.  

4.初始化app工具欄

這個(gè)沒做什么,似乎是想根據(jù)本地化信息,設(shè)置返回箭頭的方向,因?yàn)橛械恼Z言是從右往左看的。

  1. /** 
  2.  * 檢查指定 Locale 的文本布局是否從右到左。 
  3.  * 設(shè)置返回箭頭的方向 
  4.  */ 
  5. private void initAppBar() { 
  6.     if (TextTool.isLayoutRightToLeft(Locale.getDefault())) { 
  7.         Image appBackImg = (Image) slice.findComponentById(ResourceTable.Id_left_arrow); 
  8.         appBackImg.setRotation(180); 
  9.     } 

5.初始化監(jiān)聽事件

包括返回按鈕的返回事件、ScrollView的touch事件。

touch事件包含大量的細(xì)節(jié)操作,如拖拽時(shí)有一個(gè)陰影效果,滾動條的處理,拖拽交換結(jié)束的處理,過渡效果,上下grid 有效區(qū)域的計(jì)算,拖拽完成將拖拽的組件添加到對應(yīng)grid的操作等,參照著拿來用吧。

  1. /** 
  2.  * 初始化監(jiān)聽事件,包括返回按鈕返回事件、ScrollView的touch事件 
  3.  */ 
  4. private void initEventListener() { 
  5.  
  6.     //‘返回按鈕’的監(jiān)聽事件 
  7.     if (slice.findComponentById(ResourceTable.Id_left_arrow) instanceof Image) { 
  8.         Image backIcon = (Image) slice.findComponentById(ResourceTable.Id_left_arrow); 
  9.         // 
  10.         backIcon.setClickedListener(component -> slice.terminateAbility()); 
  11.     } 
  12.  
  13.     //ScrollView的 Touch事件監(jiān)聽,拿來用就可以了 
  14.     scrollView.setTouchEventListener( 
  15.             (component, touchEvent) -> { 
  16.                 //按下屏幕的位置 
  17.                 MmiPoint downScreenPoint = touchEvent.getPointerScreenPosition(touchEvent.getIndex()); 
  18.  
  19.                 switch (touchEvent.getAction()) { 
  20.                     //表示第一根手指觸摸屏幕。這表示交互的開始 
  21.                     case TouchEvent.PRIMARY_POINT_DOWN: 
  22.                         currentDragX = (int) downScreenPoint.getX(); 
  23.                         currentDragY = (int) downScreenPoint.getY(); 
  24.                         //獲取指針?biāo)饕鄬τ谄莆恢玫?nbsp;x 和 y 坐標(biāo)。 
  25.                         MmiPoint downPoint = touchEvent.getPointerPosition(touchEvent.getIndex()); 
  26.                         scrollViewTop = (int) downScreenPoint.getY() - (int) downPoint.getY(); 
  27.                         scrollViewLeft = (int) downScreenPoint.getX() - (int) downPoint.getX(); 
  28.                         return true
  29.                     //表示最后一個(gè)手指從屏幕上抬起。這表示交互結(jié)束 
  30.                     case TouchEvent.PRIMARY_POINT_UP: 
  31.                         //恢復(fù)下面grid的描述 
  32.                         changeTableLayoutDownDesc(ResourceTable.String_down_grid_layout_desc_text); 
  33.                     case TouchEvent.CANCEL: 
  34.                         if (isViewOnDrag) { 
  35.                             selectedView.setScale(1.0f, 1.0f); 
  36.                             selectedView.setAlpha(1.0f); 
  37.                             selectedView.setVisibility(Component.VISIBLE); 
  38.                             isViewOnDrag = false
  39.                             isScroll = false
  40.                             return true
  41.                         } 
  42.                         break; 
  43.  
  44.                     //表示手指在屏幕上移動 
  45.                     case TouchEvent.POINT_MOVE: 
  46.                         if (!isViewOnDrag) { 
  47.                             break; 
  48.                         } 
  49.                         int pointX = (int) downScreenPoint.getX(); 
  50.                         int pointY = (int) downScreenPoint.getY(); 
  51.  
  52.  
  53.                         this.exchangeItem(pointX, pointY); 
  54.  
  55.                         if (UP_GRID_TAG.equals(selectedView.getTag())) { 
  56.                             this.swapItems(pointX, pointY); 
  57.                         } 
  58.                         this.handleScroll(pointY); 
  59.                         return true
  60.                 } 
  61.                 return false
  62.             } 
  63.     ); 
  64.  

復(fù)制歸納總結(jié)

1.自定義組件在構(gòu)造函數(shù)中傳遞slice

這樣的目的是便于獲取頁面的其它組件。

  1. Component itemLayout=LayoutScatter.getInstance(slice.getContext()) 
  2. .parse(ResourceTable.Layout_grid_item, nullfalse); 

需要注意的是slice指代的是頁面,但是自定義組件往往是有自己的布局文件的,一般不在slice中,所以不要通過slice獲取自定義組件的子組件,獲取不到,不過可以通過LayoutScatter獲取

  1. //錯(cuò)誤的方式 
  2. Component gridItem=  slice.findComponentById(ResourceTable.Layout_grid_item); 
  3. //正確的方式 
  4. Component gridItem = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_grid_item, nullfalse); 

2.單位轉(zhuǎn)換vp2px

java組件對象寬高、邊距的單位默認(rèn)時(shí)px,

從element中獲取的值需要進(jìn)行單位轉(zhuǎn)換,可以使用AttrHelper.vp2px 將vp轉(zhuǎn)換為px。

  1. if (gridItem.findComponentById(ResourceTable.Id_grid_item_text) instanceof Text) { 
  2.     Text textItem = (Text) gridItem.findComponentById(ResourceTable.Id_grid_item_text); 
  3.     textItem.setText(item.getItemText()); 
  4.     textItem.setTextSize(AttrHelper.fp2px(10, context)); 

3.子組件的獲取

獲取一個(gè)組件對象后,可以使用該組件對象的findComponentById方法繼續(xù)獲取內(nèi)部的子組件

  1. Component gridItem = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_grid_item, nullfalse); 
  2. Image imageItem = (Image) gridItem.findComponentById(ResourceTable.Id_grid_item_image); 

4.TableLayout的使用

TableLayout繼承自ComponentContainer,提供用于在帶有表格的組件中排列組件的布局。

TableLayout 提供了用于對齊和排列組件的接口,以在帶有表格的組件中顯示組件。 排列方式、行列數(shù)、元件位置均可配置。

例如 removeAllComponents();可以用來清除 ComponentContainer 管理的所有組件,addComponent 用來將組件添加到ComponentContainer 容器中。示例中GridView就是繼承自TableLayout。

  1. /** 
  2.  * The setAdapter 
  3.  * 
  4.  * @param adapter adapter 
  5.  * @param longClickedListener longClickedListener 
  6.  */ 
  7. void setAdapter(GridAdapter adapter, LongClickedListener longClickedListener) { 
  8.     //清除 ComponentContainer 管理的所有組件 
  9.     removeAllComponents(); 
  10.     //遍歷item組件列表 
  11.     for (int i = 0; i < adapter.getComponentList().size(); i++) { 
  12.         //為組件中的長按事件注冊一個(gè)監(jiān)聽器(組件被點(diǎn)擊并按?。?nbsp;
  13.         adapter.getComponentList().get(i).setLongClickedListener(longClickedListener); 
  14.         //將組件添加到容器中 
  15.         addComponent(adapter.getComponentList().get(i)); 
  16.     } 

效果展示

示例代碼模擬了一下手機(jī)控制中心,編輯快捷開關(guān)的效果

原效果模擬效果

文章相關(guān)附件可以點(diǎn)擊下面的原文鏈接前往下載

原文鏈接:https://harmonyos.51cto.com/posts/4776

想了解更多內(nèi)容,請?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2021-06-28 14:41:36

鴻蒙HarmonyOS應(yīng)用

2021-06-18 14:55:57

鴻蒙HarmonyOS應(yīng)用

2021-05-28 17:01:49

鴻蒙HarmonyOS應(yīng)用

2021-08-30 18:34:35

鴻蒙HarmonyOS應(yīng)用

2020-12-30 11:08:50

鴻蒙HarmonyOShelloWorld

2020-11-17 11:48:44

HarmonyOS

2020-11-25 12:02:02

TableLayout

2023-03-08 08:33:44

Javajavac命令

2020-03-26 10:43:57

CSS Grid Ge Grid代碼

2021-10-18 10:14:26

鴻蒙HarmonyOS應(yīng)用

2021-01-06 10:59:14

鴻蒙HarmonyOSPage Abilit

2022-02-17 21:05:26

AbilityJS FAJava PA

2021-01-04 11:44:05

鴻蒙HarmonyOSAbilitySlic

2020-12-22 11:09:20

鴻蒙Feature AbiAbilitySlic

2021-03-05 15:55:10

鴻蒙HarmonyOS應(yīng)用開發(fā)

2020-11-05 11:47:52

HarmonyOS

2020-12-01 15:54:08

鴻蒙構(gòu)建系統(tǒng)

2018-08-08 15:57:05

csshtml前端

2021-01-12 09:42:05

鴻蒙HarmonyOSPage Abilit

2020-12-28 11:19:06

鴻蒙HarmonyOSPage Abilit
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號