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

鴻蒙Tab切換選項(xiàng)卡同時(shí)更換內(nèi)容

系統(tǒng)
Tab選項(xiàng)卡是應(yīng)用程序中最最常用,也是最普遍存在的一種布局形態(tài),無論是在PC端還是在移動(dòng)端,都是一種清晰明了,層級(jí)關(guān)系簡(jiǎn)單的,能夠使用戶明確所處位置。

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

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

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

Tab選項(xiàng)卡是應(yīng)用程序中最最常用,也是最普遍存在的一種布局形態(tài),無論是在PC端還是在移動(dòng)端,都是一種清晰明了,層級(jí)關(guān)系簡(jiǎn)單的,能夠使用戶明確所處位置。Tab選項(xiàng)卡可以置于頁面的底部,比如微信底部選項(xiàng)卡;也可以置于頂部,比如新聞?lì)惖腁PP頂部的類別選項(xiàng);還可以置于左側(cè)或者右側(cè),一般常見的是后臺(tái)應(yīng)用左側(cè)的樹形導(dǎo)航欄。它們存在一個(gè)共同的特性,那就無論我點(diǎn)擊那個(gè)選項(xiàng)卡,都不會(huì)跳轉(zhuǎn)到二級(jí)頁面,而是在同級(jí)頁面中在選項(xiàng)卡下的內(nèi)容區(qū)域中去渲染當(dāng)前選中的選項(xiàng)卡內(nèi)容。比如下面的示例圖片中,我們有三個(gè)Tab,圖片、視頻以及音頻,首次加載時(shí)我們選中的是圖片(Image)選項(xiàng)卡,底部?jī)?nèi)容區(qū)域是一些排列的圖片,如果我們選擇視頻(Video),那么底部的圖片將被移除,視頻列表頁將被載入。


1、首先修改ability_main.xml代碼,在布局中添加頂部選項(xiàng)卡列表。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:background_element="#444444" 
  7.     ohos:orientation="vertical"
  8.     <TabList 
  9.         ohos:id="$+id:tab_list" 
  10.         ohos:weight="1" 
  11.         ohos:top_margin="10vp" 
  12.         ohos:tab_margin="24vp" 
  13.         ohos:tab_length="140vp" 
  14.         ohos:text_size="20fp" 
  15.         ohos:height="36vp" 
  16.         ohos:width="match_parent" 
  17.         ohos:layout_alignment="center" 
  18.         ohos:orientation="horizontal" 
  19.         ohos:text_alignment="center" 
  20.         ohos:normal_text_color="#999999" 
  21.         ohos:selected_text_color="#FFFFFF" 
  22.         ohos:selected_tab_indicator_color="#FFFFFF" 
  23.         ohos:selected_tab_indicator_height="2vp"/> 
  24. </DirectionalLayout> 

 2、在MainAbilitySlice中為TabList容器添加Tab選項(xiàng)卡。

  1. //列表項(xiàng) 
  2. TabList tabList = (TabList) findComponentById(ResourceTable.Id_tab_list); 
  3.  
  4. TabList.Tab imageTab = tabList.new Tab(getContext()); 
  5. imageTab.setText("Image"); 
  6. tabList.addTab(imageTab, true); 
  7. scrollView.addComponent(imageGrid); 
  8.  
  9. TabList.Tab videoTab = tabList.new Tab(getContext()); 
  10. videoTab.setText("Video"); 
  11. tabList.addTab(videoTab); 
  12.  
  13. TabList.Tab audioTab = tabList.new Tab(getContext()); 
  14. audioTab.setText("Audio"); 
  15. tabList.addTab(audioTab); 

 3、選項(xiàng)卡切換狀態(tài)需要監(jiān)聽選項(xiàng)卡的選中狀態(tài)。我們可以在監(jiān)聽事件中對(duì)選項(xiàng)卡的業(yè)務(wù)邏輯做處理,其提供給我們?nèi)N方法,一當(dāng)前選中的Tab,二取消選中的Tab,三再次選中當(dāng)前Tab。在這三個(gè)方法中,我們可以具體的實(shí)現(xiàn)業(yè)務(wù)邏輯,比如當(dāng)前選中的Tab被連續(xù)點(diǎn)擊時(shí),我們可以刷新我們呈現(xiàn)的內(nèi)容。

  1. tabList.addTabSelectedListener(new TabList.TabSelectedListener() { 
  2.  
  3. @Override 
  4.  
  5. public void onSelected(TabList.Tab tab) { 
  6.  
  7. //TODO 具體業(yè)務(wù)邏輯代碼 
  8.  
  9.  
  10. @Override 
  11.  
  12. public void onUnselected(TabList.Tab tab) { 
  13.  
  14. //TODO 具體業(yè)務(wù)邏輯代碼 
  15.  
  16.  
  17. @Override 
  18.  
  19. public void onReselected(TabList.Tab tab) { 
  20.  
  21. //TODO 具體業(yè)務(wù)邏輯代碼 
  22.  
  23.  
  24. }); 

 4、頂部選項(xiàng)卡我們已經(jīng)實(shí)現(xiàn)了,那么怎么去實(shí)現(xiàn)點(diǎn)擊后內(nèi)容的更改呢?這里就需要用到一個(gè)新的組件ScrollView,ScrollView是一種帶有滾動(dòng)功能的組件。如果我們使用常規(guī)的布局組件,內(nèi)容超出范圍后,上下左右滾動(dòng)是需要我們自己去重寫,而ScrollView組件只需要設(shè)置它的子組件方向即可。修改ability_main.xml代碼添加ScrollView組件。

  1. xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  2.  
  3. ohos:height="match_parent" 
  4.  
  5. ohos:width="match_parent" 
  6.  
  7. ohos:background_element="#444444" 
  8.  
  9. ohos:orientation="vertical"
  10.  
  11.  
  12. ... /> 
  13.  
  14.  
  15. ohos:id="$+id:tab_content" 
  16.  
  17. ohos:height="match_parent" 
  18.  
  19. ohos:width="match_parent" 
  20.  
  21. ohos:padding="10vp" 
  22.  
  23. ohos:weight="9"

 5、首先初始化兩個(gè)TableLayout組件,在其中添加多個(gè)子組件,用于顯示圖片列表和視頻列表(僅供參考學(xué)習(xí),未實(shí)現(xiàn)真正的圖片和視頻)。

  1. private void initVideoGrid() { 
  2.  
  3. videoGrid = new TableLayout(this); 
  4.  
  5. videoGrid.setWidth(MATCH_PARENT); 
  6.  
  7. videoGrid.setHeight(MATCH_CONTENT); 
  8.  
  9. videoGrid.setColumnCount(1); 
  10.  
  11. videoGrid.setRowCount(2); 
  12.  
  13. for (int i = 1; i < 4; i++) { 
  14.  
  15. Text text = new Text(this); 
  16.  
  17. text.setWidth(MATCH_PARENT); 
  18.  
  19. text.setHeight(800); 
  20.  
  21. text.setTextAlignment(TextAlignment.CENTER); 
  22.  
  23. text.setText("第" + i + "塊視頻"); 
  24.  
  25. ShapeElement shapeElement = new ShapeElement(); 
  26.  
  27. shapeElement.setCornerRadius(20); 
  28.  
  29. shapeElement.setRgbColor(new RgbColor(192,0,255)); 
  30.  
  31. text.setBackground(shapeElement); 
  32.  
  33. text.setPadding(10,10,10,10); 
  34.  
  35. text.setMarginsTopAndBottom(10,10); 
  36.  
  37. text.setMarginsLeftAndRight(20,20); 
  38.  
  39. text.setTextSize(50); 
  40.  
  41. videoGrid.addComponent(text); 
  42.  
  43.  
  44.  
  45. private void initImageGrid() { 
  46.  
  47. imageGrid = new TableLayout(this); 
  48.  
  49. imageGrid.setWidth(MATCH_PARENT); 
  50.  
  51. imageGrid.setHeight(MATCH_CONTENT); 
  52.  
  53. imageGrid.setColumnCount(4); 
  54.  
  55. imageGrid.setRowCount(2); 
  56.  
  57. for (int i = 1; i <= 10; i++) { 
  58.  
  59. Text text = new Text(this); 
  60.  
  61. text.setWidth(400); 
  62.  
  63. text.setHeight(400); 
  64.  
  65. text.setTextAlignment(TextAlignment.CENTER); 
  66.  
  67. text.setText("第" + i + "塊圖片"); 
  68.  
  69. ShapeElement shapeElement = new ShapeElement(); 
  70.  
  71. shapeElement.setCornerRadius(20); 
  72.  
  73. shapeElement.setRgbColor(new RgbColor(0,192,255)); 
  74.  
  75. text.setBackground(shapeElement); 
  76.  
  77. text.setPadding(10,10,10,10); 
  78.  
  79. text.setMarginsTopAndBottom(10,10); 
  80.  
  81. text.setMarginsLeftAndRight(20,20); 
  82.  
  83. text.setTextSize(50); 
  84.  
  85. imageGrid.addComponent(text); 
  86.  
  87.  

 6、 構(gòu)建好各自的Tab的內(nèi)容后,我們需要在選擇監(jiān)聽中去做顯示處理。選中圖片后,需要加載圖片列表,選中視頻后,需要加載視頻列表的同時(shí)移除圖片列表,不然會(huì)存在小問題,你也可以嘗試一下??纯闯霈F(xiàn)了什么問題。

  1. tabList.addTabSelectedListener(new TabList.TabSelectedListener() { 
  2.  
  3. @Override 
  4.  
  5. public void onSelected(TabList.Tab tab) { 
  6.  
  7. if (tab.getPosition() == 0) { 
  8.  
  9. scrollView.addComponent(imageGrid); 
  10.  
  11. else if (tab.getPosition() == 1) { 
  12.  
  13. scrollView.addComponent(videoGrid); 
  14.  
  15. else { 
  16.  
  17. scrollView.addComponent(new DirectionalLayout(getContext())); 
  18.  
  19.  
  20. HiLog.info(label, "onSelected:" + tab.getPosition()); 
  21.  
  22.  
  23. @Override 
  24.  
  25. public void onUnselected(TabList.Tab tab) { 
  26.  
  27. if (tab.getPosition() == 0) { 
  28.  
  29. scrollView.removeComponent(imageGrid); 
  30.  
  31. else if (tab.getPosition() == 1) { 
  32.  
  33. scrollView.removeComponent(videoGrid); 
  34.  
  35. else { 
  36.  
  37. scrollView.removeComponent(new DirectionalLayout(getContext())); 
  38.  
  39.  
  40. HiLog.info(label, "onUnselected:" + tab.getText()); 
  41.  
  42.  
  43. @Override 
  44.  
  45. public void onReselected(TabList.Tab tab) { 
  46.  
  47. HiLog.info(label, "onReselected:" + tab.getText()); 
  48.  
  49.  
  50. }); 
  51.  

 在這里我是以Tab的序號(hào)去判斷選中后的操作,你也可以使用它的text屬性來判斷。到這里基本的功能已經(jīng)實(shí)現(xiàn)了,我們運(yùn)行查看一下效果。

圖片中有個(gè)小Bug,你發(fā)現(xiàn)了嗎?應(yīng)該怎么解決這個(gè)問題呢?

©著作權(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 來源: 鴻蒙社區(qū)
相關(guān)推薦

2022-04-27 16:41:57

微軟Edge瀏覽器

2020-05-28 08:33:28

JavaScript開發(fā)技術(shù)

2020-08-13 12:02:44

JavaScript瀏覽器選項(xiàng)卡

2021-03-31 19:24:26

Edge微軟功能

2011-09-08 15:50:21

windows XP無線網(wǎng)絡(luò)配置

2021-01-05 10:54:12

Edge微軟服務(wù)器

2023-03-03 09:36:15

Windows微軟

2022-01-09 17:05:54

Windows 11操作系統(tǒng)微軟

2023-08-02 07:10:16

筆記本電腦內(nèi)存

2010-01-27 16:30:47

Android選項(xiàng)卡

2020-07-28 16:28:55

微軟Windows 10 Windows 10X

2020-12-25 06:56:11

Edge瀏覽器

2021-08-05 11:00:17

微軟Edge瀏覽器

2021-09-13 05:00:09

監(jiān)控Trends 性能

2014-04-30 17:43:36

偽基站USIM卡

2022-05-02 09:27:43

微軟Windows 11

2022-04-28 13:21:01

微軟Windows 11瀏覽器

2023-07-28 00:19:35

2019-06-10 08:53:05

2023-07-14 08:32:32

微軟資源管理器
點(diǎn)贊
收藏

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