偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

2020征文-手機(jī)圖解鴻蒙列表組件ListContainer

系統(tǒng)
文章由鴻蒙社區(qū)產(chǎn)出,想要了解更多內(nèi)容請(qǐng)前往:51CTO和華為官方戰(zhàn)略合作共建的鴻蒙技術(shù)社區(qū)https://harmonyos.51cto.com/#zz

[[360076]]

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

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

https://harmonyos.51cto.com/#zz

一、什么是ListContainer

ListContainer是用來(lái)呈現(xiàn)連續(xù)、多行數(shù)據(jù)的列表組件,包含一系列相同類型的列表項(xiàng)。如下圖所示:


二、ListContainer的架構(gòu)視圖

ListContainer的架構(gòu)視圖如下所示:

ListContainer作為列表,其中的列表項(xiàng)數(shù)據(jù)是由適配器Adapter提供的,適配器Adapter作為L(zhǎng)istContainer和數(shù)據(jù)源之間的中介&橋梁,將數(shù)據(jù)源中的數(shù)據(jù)映射到要展示的ListContainer中,ListContainer負(fù)責(zé)以列表的形式顯示適配器Adapter提供的數(shù)據(jù)。

三、ListContainer的使用步驟

ListContainer的使用步驟主要包括:

1、創(chuàng)建ListContainer

2、創(chuàng)建列表項(xiàng)的布局

3、使用POJO類封裝數(shù)據(jù)源中與每個(gè)列表項(xiàng)對(duì)應(yīng)的數(shù)據(jù)

4、構(gòu)造數(shù)據(jù)源

5、構(gòu)造適配器Adapter

6、將數(shù)據(jù)源關(guān)聯(lián)到適配器Adapter

7、將適配器Adapter應(yīng)用到ListContainer

四、ListContainer的使用示例

示例的運(yùn)行效果如下圖所示:

開發(fā)步驟如下:

1、創(chuàng)建ListContainer(ability_main.xml)

  1. xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  2.  
  3. ohos:id="$+id:list_container" 
  4.  
  5. ohos:height="match_parent" 
  6.  
  7. ohos:width="match_parent"/> 

 2、創(chuàng)建列表項(xiàng)的布局(item.xml)

  1. xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  2.  
  3. ohos:height="match_content" 
  4.  
  5. ohos:width="match_parent" 
  6.  
  7. ohos:orientation="vertical"
  8.  
  9.  
  10. ohos:id="$+id:name" 
  11.  
  12. ohos:height="50vp" 
  13.  
  14. ohos:width="match_parent" 
  15.  
  16. ohos:padding="5vp" 
  17.  
  18. ohos:auto_font_size="true" 
  19.  
  20. ohos:text_alignment="center"/> 
  21.  
  22.  
  23. ohos:height="1vp" 
  24.  
  25. ohos:width="match_parent" 
  26.  
  27. ohos:background_element="#CCCCCC"/> 

 3、使用POJO類封裝數(shù)據(jù)源中與每個(gè)列表項(xiàng)對(duì)應(yīng)的數(shù)據(jù)(Item.java)

POJO類指的是:只包含屬性和相應(yīng)的getter和setter,而沒有業(yè)務(wù)邏輯的類。

  1. public class Item { 
  2.  
  3. private String name
  4.  
  5. public Item(String name) { 
  6.  
  7. this.name = name
  8.  
  9.  
  10. public String getName() { 
  11.  
  12. return name
  13.  
  14.  
  15. public void setName(String name) { 
  16.  
  17. this.name = name
  18.  
  19.  

 4、構(gòu)造數(shù)據(jù)源(MainAbilitySlice.java) 

  1. public class MainAbilitySlice extends AbilitySlice { 
  2.  
  3. @Override 
  4.  
  5. public void onStart(Intent intent) { 
  6.  
  7. super.onStart(intent); 
  8.  
  9. super.setUIContent(ResourceTable.Layout_ability_main); 
  10.  
  11. List list = getData(); 
  12.  
  13.  
  14. private List getData() { 
  15.  
  16. List list = new ArrayList<>(); 
  17.  
  18. for (int i = 1; i <= 100; i++) { 
  19.  
  20. list.add(new Item("Item " + i)); 
  21.  
  22.  
  23. return list; 
  24.  
  25.  

 5、構(gòu)造適配器Adapter(MyItemProvider.java)

常用的適配器類是RecycleItemProvider,繼承該類時(shí)需要重寫四個(gè)方法:getCount()、getItem()、getItemId()和getComponent()。其中,對(duì)于方法getComponent(),當(dāng)某個(gè)列表項(xiàng)從不可見變?yōu)榭梢姇r(shí)會(huì)自動(dòng)調(diào)用該方法,在該方法中適配器Adapter會(huì)從數(shù)據(jù)源取數(shù)據(jù),并將返回的數(shù)據(jù)封裝在一個(gè)Component對(duì)象中,以便將該對(duì)象返回給ListContainer,從而將數(shù)據(jù)映射到對(duì)應(yīng)的列表項(xiàng)。

  1. public class MyItemProvider extends RecycleItemProvider { 
  2.  
  3. private List list; 
  4.  
  5. private AbilitySlice slice; 
  6.  
  7. public MyItemProvider(List list, AbilitySlice slice) { 
  8.  
  9. this.list = list; 
  10.  
  11. this.slice = slice; 
  12.  
  13.  
  14. @Override 
  15.  
  16. public int getCount() { 
  17.  
  18. return list.size(); 
  19.  
  20.  
  21. @Override 
  22.  
  23. public Object getItem(int position) { 
  24.  
  25. return list.get(position); 
  26.  
  27.  
  28. @Override 
  29.  
  30. public long getItemId(int position) { 
  31.  
  32. return position; 
  33.  
  34.  
  35. @Override 
  36.  
  37. public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) { 
  38.  
  39. convertComponent = LayoutScatter.getInstance(slice) 
  40.  
  41. .parse(ResourceTable.Layout_item, nullfalse); 
  42.  
  43. Text text = (Text) convertComponent.findComponentById(ResourceTable.Id_name); 
  44.  
  45. text.setText(list.get(position).getName()); 
  46.  
  47. return convertComponent; 
  48.  
  49.  

 6、將數(shù)據(jù)源關(guān)聯(lián)到適配器Adapter(MainAbilitySlice.java) 

  1. public class MainAbilitySlice extends AbilitySlice { 
  2.  
  3. @Override 
  4.  
  5. public void onStart(Intent intent) { 
  6.  
  7. ...... 
  8.  
  9. List list = getData(); 
  10.  
  11. MyItemProvider myItemProvider = new MyItemProvider(list, this); 
  12.  
  13.  
  14. ...... 
  15.  

 7、將適配器Adapter應(yīng)用到ListContainer(MainAbilitySlice.java) 

  1. public class MainAbilitySlice extends AbilitySlice { 
  2.  
  3. @Override 
  4.  
  5. public void onStart(Intent intent) { 
  6.  
  7. ...... 
  8.  
  9. MyItemProvider myItemProvider = new MyItemProvider(list, this); 
  10.  
  11. ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container); 
  12.  
  13. listContainer.setItemProvider(myItemProvider); 
  14.  
  15.  
  16. ...... 
  17.  

 五、適配器Adapter的優(yōu)化

對(duì)于上面的第5步,當(dāng)某個(gè)列表項(xiàng)從不可見變?yōu)榭梢姇r(shí),對(duì)于自動(dòng)調(diào)用的方法getComponent(),我們是根據(jù)列表項(xiàng)的布局文件item.xml創(chuàng)建了一個(gè)列表項(xiàng)的實(shí)例。這樣的做法性能較差。因?yàn)橄到y(tǒng)會(huì)對(duì)變?yōu)椴豢梢姷牧斜眄?xiàng)實(shí)例進(jìn)行緩存,所以對(duì)于方法getComponent()中的第二個(gè)參數(shù)convertComponent有可能不為null。當(dāng)convertComponent不為null時(shí),說明系統(tǒng)把緩存中的某個(gè)列表項(xiàng)實(shí)例傳遞過來(lái)了,因此,完全可以復(fù)用該列表項(xiàng)實(shí)例,而沒有必要重新創(chuàng)建一個(gè)列表項(xiàng)實(shí)例。優(yōu)化方式如下:

  1. public class MyItemProvider extends RecycleItemProvider { 
  2.  
  3. ...... 
  4.  
  5. @Override 
  6.  
  7. public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) { 
  8.  
  9. if (convertComponent == null) { 
  10.  
  11. convertComponent = LayoutScatter.getInstance(slice) 
  12.  
  13. .parse(ResourceTable.Layout_item, nullfalse); 
  14.  
  15.  
  16. Text text = (Text) convertComponent.findComponentById(ResourceTable.Id_name); 
  17.  
  18. text.setText(list.get(position).getName()); 
  19.  
  20. return convertComponent; 
  21.  
  22.  

 六、使用ViewHolder對(duì)適配器Adapter做終極優(yōu)化

在上面的代碼中,得到列表項(xiàng)實(shí)例后(不管是新創(chuàng)建的列表項(xiàng)實(shí)例,還是從緩存中獲得的列表項(xiàng)實(shí)例),都要調(diào)用方法findComponentById()以獲得列表項(xiàng)中的子組件。調(diào)用方法findComponentById()是比較耗費(fèi)性能的,所以好的做法是:在新創(chuàng)建列表項(xiàng)實(shí)例時(shí),就調(diào)用方法findComponentById()以獲得列表項(xiàng)中的所有子組件,并且將所有子組件通過ViewHolder綁定到列表項(xiàng)實(shí)例。這樣,當(dāng)從緩存中獲得列表項(xiàng)實(shí)例后,就無(wú)需再調(diào)用方法findComponentById()了,直接獲得列表項(xiàng)實(shí)例綁定的ViewHolder就可以得到所有子組件了。優(yōu)化方式如下:

  1. public class MyItemProvider extends RecycleItemProvider { 
  2.  
  3. ...... 
  4.  
  5. @Override 
  6.  
  7. public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) { 
  8.  
  9. ViewHolder viewHolder; 
  10.  
  11. if (convertComponent == null) { 
  12.  
  13. convertComponent = LayoutScatter.getInstance(slice) 
  14.  
  15. .parse(ResourceTable.Layout_item, nullfalse); 
  16.  
  17. viewHolder = new ViewHolder(); 
  18.  
  19. viewHolder.text = (Text) convertComponent.findComponentById(ResourceTable.Id_name); 
  20.  
  21. convertComponent.setTag(viewHolder); 
  22.  
  23. else { 
  24.  
  25. viewHolder = (ViewHolder) convertComponent.getTag(); 
  26.  
  27.  
  28. viewHolder.text.setText(list.get(position).getName()); 
  29.  
  30. return convertComponent; 
  31.  
  32.  
  33. class ViewHolder { 
  34.  
  35. Text text; 
  36.  
  37.  

 如果你理解了為什么要這么優(yōu)化,相信你會(huì)發(fā)現(xiàn):當(dāng)列表項(xiàng)中只有一個(gè)子組件時(shí),也可以不引入ViewHolder,而是將這個(gè)子組件直接綁定到列表項(xiàng)實(shí)例。代碼如下所示:

  1. public class MyItemProvider extends RecycleItemProvider { 
  2.  
  3. ...... 
  4.  
  5. @Override 
  6.  
  7. public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) { 
  8.  
  9. Text text; 
  10.  
  11. if (convertComponent == null) { 
  12.  
  13. convertComponent = LayoutScatter.getInstance(slice) 
  14.  
  15. .parse(ResourceTable.Layout_item, nullfalse); 
  16.  
  17. text = (Text) convertComponent.findComponentById(ResourceTable.Id_name); 
  18.  
  19. convertComponent.setTag(text); 
  20.  
  21. else { 
  22.  
  23. text = (Text) convertComponent.getTag(); 
  24.  
  25.  
  26. text.setText(list.get(position).getName()); 
  27.  
  28. return convertComponent; 
  29.  
  30.  

 示例源代碼,請(qǐng)見附件。

歡迎訂閱我的專欄【圖解鴻蒙】:

https://harmonyos.51cto.com/column/27

©著作權(quán)歸作者和HarmonyOS技術(shù)社區(qū)共同所有,如需轉(zhuǎn)載,請(qǐng)注明出處,否則將追究法律責(zé)任

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

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

https://harmonyos.51cto.com/#zz

 

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

2021-08-12 14:59:15

鴻蒙HarmonyOS應(yīng)用

2020-12-22 11:09:20

鴻蒙Feature AbiAbilitySlic

2020-12-28 11:19:06

鴻蒙HarmonyOSPage Abilit

2020-12-04 12:42:59

組件鴻蒙Text

2020-12-22 09:48:18

鴻蒙HarmonyOS應(yīng)用程序

2020-12-23 11:36:23

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

2020-12-23 11:24:34

鴻蒙開發(fā)IDE安裝

2020-12-22 11:20:36

鴻蒙HarmonyOS游戲

2020-12-23 11:45:27

鴻蒙HarmonyOSTextField組件

2020-12-28 11:30:07

鴻蒙HarmonyOS分布式

2020-12-09 11:53:24

鴻蒙開發(fā)HelloWord

2020-12-25 10:39:53

鴻蒙開發(fā)JS

2020-12-24 11:24:31

鴻蒙開發(fā)JS

2020-12-24 10:05:54

鴻蒙鴻蒙開發(fā)Hello World

2021-08-25 09:49:48

鴻蒙HarmonyOS應(yīng)用

2020-12-29 09:59:01

鴻蒙HarmonyOS智能家居

2020-12-16 10:05:48

鴻蒙開發(fā)板Onenet平臺(tái)

2020-12-18 11:05:25

鴻蒙HarmonyOS游戲

2020-12-15 11:57:49

Hi3861 HarmonyOS開發(fā)板

2020-12-14 09:58:28

鴻蒙HarmonyOS手表游戲
點(diǎn)贊
收藏

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