成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

鴻蒙Tab切換選項卡同時更換內(nèi)容

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

想了解更多內(nèi)容,請訪問:

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

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

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


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

  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選項卡。

  1. //列表項 
  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、選項卡切換狀態(tài)需要監(jiān)聽選項卡的選中狀態(tài)。我們可以在監(jiān)聽事件中對選項卡的業(yè)務(wù)邏輯做處理,其提供給我們?nèi)N方法,一當(dāng)前選中的Tab,二取消選中的Tab,三再次選中當(dāng)前Tab。在這三個方法中,我們可以具體的實現(xiàn)業(yè)務(wù)邏輯,比如當(dāng)前選中的Tab被連續(xù)點擊時,我們可以刷新我們呈現(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、頂部選項卡我們已經(jīng)實現(xiàn)了,那么怎么去實現(xiàn)點擊后內(nèi)容的更改呢?這里就需要用到一個新的組件ScrollView,ScrollView是一種帶有滾動功能的組件。如果我們使用常規(guī)的布局組件,內(nèi)容超出范圍后,上下左右滾動是需要我們自己去重寫,而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、首先初始化兩個TableLayout組件,在其中添加多個子組件,用于顯示圖片列表和視頻列表(僅供參考學(xué)習(xí),未實現(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)聽中去做顯示處理。選中圖片后,需要加載圖片列表,選中視頻后,需要加載視頻列表的同時移除圖片列表,不然會存在小問題,你也可以嘗試一下。看看出現(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的序號去判斷選中后的操作,你也可以使用它的text屬性來判斷。到這里基本的功能已經(jīng)實現(xiàn)了,我們運行查看一下效果。

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

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

想了解更多內(nèi)容,請訪問:

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瀏覽器選項卡

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ù)器

2022-01-09 17:05:54

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

2023-03-03 09:36:15

Windows微軟

2023-08-02 07:10:16

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

2010-01-27 16:30:47

Android選項卡

2020-07-28 16:28:55

微軟Windows 10 Windows 10X

2020-12-25 06:56:11

Edge瀏覽器

2021-09-13 05:00:09

監(jiān)控Trends 性能

2022-05-02 09:27:43

微軟Windows 11

2021-08-05 11:00:17

微軟Edge瀏覽器

2014-04-30 17:43:36

偽基站USIM卡

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

微軟資源管理器
點贊
收藏

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

主站蜘蛛池模板: 亚洲免费三区 | 欧美又大粗又爽又黄大片视频 | 日本久久福利 | 91精品国产综合久久久久久 | caoporn免费| 欧美一区二区三区小说 | 欧美性猛交一区二区三区精品 | 青青草一区 | 亚洲www | 亚洲97 | 天天弄天天操 | 日韩欧美一区二区三区四区 | 在线成人免费视频 | 亚洲日韩中文字幕一区 | 精品国产1区2区3区 在线国产视频 | 午夜小视频在线播放 | 羞羞网站在线免费观看 | 99精品视频免费在线观看 | 国产精品福利久久久 | 国产亚洲精品一区二区三区 | 日韩电影一区二区三区 | 嫩草懂你的影院入口 | 亚洲精品国产电影 | 国产视频一区二区在线观看 | 精品一区二区三区入口 | 久草免费在线视频 | 亚洲国产69 | 日本在线视| 欧美视频二区 | 国产成人jvid在线播放 | 国产精品自产拍在线观看蜜 | 国产免费麻豆视频 | 成人欧美一区二区三区视频xxx | 福利在线观看 | 国产97在线 | 日韩 | av网址在线 | 91麻豆精品国产91久久久久久久久 | 成人精品在线观看 | www.国产一区 | 午夜精品久久久久久久久久久久 | 91免费视频观看 |