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

鴻蒙HarmonyOS三方件開(kāi)發(fā)指南-SwipeLayout側(cè)滑刪除

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

[[384271]]

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

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

https://harmonyos.51cto.com

1. SwipeLayout組件功能介紹

1.1.功能介紹:

SwipeLayout組件是一個(gè)側(cè)滑刪除組件。

1.2. 模擬器上運(yùn)行效果:

2. SwipeLayout使用方法

2.1. 新建工程,增加組件Har包依賴

在應(yīng)用模塊中添加HAR,只需要將SwipeLayout.har復(fù)制到entry\libs目錄下即可(由于build.gradle中已經(jīng)依賴的libs目錄下的*.har,因此不需要再做修改)。

2.2. 修改主頁(yè)面的布局文件

修改主頁(yè)面的布局文件ability_main.xml,將自定義的SwipeLayout添加到xml中,將初始狀態(tài)下展示的視圖添加到SwipeLayout作為index為0的子視圖:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:id="$+id:total1" 
  5.     ohos:height="match_parent" 
  6.     ohos:width="match_parent" 
  7.     ohos:background_element="gray" 
  8.     ohos:orientation="vertical"
  9.     <com.isoftstone.swipelayout.SwipeLayout 
  10.         ohos:id="$+id:sample2" 
  11.         ohos:height="80vp" 
  12.         ohos:width="match_parent" 
  13.         ohos:orientation="horizontal"
  14.         <Text 
  15.             ohos:id="$+id:bottom_layout1" 
  16.             ohos:height="match_parent" 
  17.             ohos:width="match_parent" 
  18.             ohos:background_element="white" 
  19.             ohos:multiple_lines="true" 
  20.             ohos:padding="10" 
  21.             ohos:text="要有最樸素的生活和最遙遠(yuǎn)的夢(mèng)想,即使明天天寒地凍,山高水遠(yuǎn),路遠(yuǎn)馬亡。" 
  22.             ohos:text_alignment="left" 
  23.             ohos:text_size="14fp" 
  24.             ohos:visibility="visible"
  25.         </Text> 
  26.         <DirectionalLayout 
  27.             ohos:id="$+id:bottom_wrapper1" 
  28.             ohos:height="match_parent" 
  29.             ohos:width="360px" 
  30.             ohos:background_element="#ddff00" 
  31.             ohos:orientation="horizontal" 
  32.             ohos:visibility="visible"
  33.             <Text 
  34.                 ohos:id="$+id:Texts1" 
  35.                 ohos:height="match_parent" 
  36.                 ohos:width="180px" 
  37.                 ohos:background_element="#7B1FA2" 
  38.                 ohos:left_padding="25" 
  39.                 ohos:right_padding="25" 
  40.                 ohos:text="收藏" 
  41.                 ohos:text_alignment="center" 
  42.                 ohos:text_color="#DC143C" 
  43.                 ohos:text_size="14fp" 
  44.                 ohos:visibility="visible" 
  45.                 /> 
  46.             <Text 
  47.                 ohos:id="$+id:texts2" 
  48.                 ohos:height="match_parent" 
  49.                 ohos:width="180px" 
  50.                 ohos:background_element="#C7C7CC" 
  51.                 ohos:left_padding="25" 
  52.                 ohos:right_padding="25" 
  53.                 ohos:text="刪除" 
  54.                 ohos:text_alignment="center" 
  55.                 ohos:text_color="#DC143C" 
  56.                 ohos:text_size="14fp" 
  57.                 ohos:visibility="visible" 
  58.                 /> 
  59.         </DirectionalLayout> 
  60.         <Image 
  61.             ohos:id="$+id:images3" 
  62.             ohos:height="match_parent" 
  63.             ohos:width="match_parent" 
  64.             ohos:background_element="gray" 
  65.             ohos:image_src="$media:star" 
  66.             /> 
  67.         <DirectionalLayout 
  68.             ohos:id="$+id:bottom_fronts" 
  69.             ohos:height="match_parent" 
  70.             ohos:width="match_content" 
  71.             ohos:background_element="#ddff00" 
  72.             ohos:orientation="horizontal" 
  73.             ohos:visibility="visible"
  74.             <Image 
  75.                 ohos:id="$+id:images1" 
  76.                 ohos:height="match_parent" 
  77.                 ohos:width="180px" 
  78.                 ohos:background_element="green" 
  79.                 ohos:image_src="$media:star"/> 
  80.             <Image 
  81.                 ohos:id="$+id:images2" 
  82.                 ohos:height="match_parent" 
  83.                 ohos:width="180px" 
  84.                 ohos:background_element="red" 
  85.                 ohos:image_src="$media:trash"/> 
  86.         </DirectionalLayout> 
  87.     </com.isoftstone.swipelayout.SwipeLayout> 
  88.     <Image 
  89.         ohos:id="$+id:images" 
  90.         ohos:height="match_content" 
  91.         ohos:width="match_content" 
  92.         ohos:background_element="green" 
  93.         ohos:image_src="$media:star" 
  94.         ohos:layout_alignment="horizontal_center" 
  95.         ohos:top_margin="100vp"/> 
  96. </DirectionalLayout> 

2.3. 初始化SwipeLayout

在MainAbilitySlince類的onStart函數(shù)中,增加如下代碼。

  1. SwipeLayout swipeLayout = (SwipeLayout) findComponentById(ResourceTable.Id_sample1); 
  2. DirectionalLayout right = (DirectionalLayout) findComponentById(ResourceTable.Id_bottom_wrapper); 
  3. //初始化 
  4. swipeLayout.initializeSwipe(); 
  5. DirectionalLayout left = (DirectionalLayout) findComponentById(ResourceTable.Id_bottom_front); 
  6. Image image3 = (Image) findComponentById(ResourceTable.Id_image3); 
  7. //將各個(gè)方向拖拽時(shí)對(duì)應(yīng)展示的視圖添加到swipeLayout 
  8. swipeLayout.addDrag(SwipeLayout.DragEdge.Leftright); 
  9. swipeLayout.addDrag(SwipeLayout.DragEdge.Rightleft); 
  10. swipeLayout.addDrag(SwipeLayout.DragEdge.Bottom, image3); 

3. SwipeLayout開(kāi)發(fā)實(shí)現(xiàn)

3.1. 新建一個(gè)Module

新建一個(gè)Module,類型選擇HarmonyOS Library,模塊名為SwipeLayout,如圖

3.2. 新建一個(gè)SwipeLayout類

新建一個(gè)SwipeLayout類,繼承自PositionLayout類

SwipeLayout的主要流程:

1. 首先通過(guò)xml的構(gòu)造方法,為SwipeLayout添加拖拽監(jiān)聽(tīng);

2. 將LinkedHashMap

3. 通過(guò)public void addDrag(DragEdge dragEdge, Component child) 方法將可拖拽的方向和對(duì)應(yīng)展示的視圖添加到mDragEdges,并設(shè)置其初始的ContentPosition;

  1. public void addDrag(DragEdge dragEdge, Component child) { 
  2.     mDragEdges.put(dragEdge, child); 
  3.     switch (dragEdge) { 
  4.         case Left
  5.             child.setContentPosition(getWidth(), 0); 
  6.             break; 
  7.         case Right
  8.             HiLog.info(label, "Log_addDrag" + child.getHeight()); 
  9.             child.setContentPosition(-child.getWidth(), 0); 
  10.             break; 
  11.         case Top
  12.             child.setContentPosition(0, getHeight()); 
  13.             break; 
  14.         case Bottom: 
  15.             child.setContentPosition(0, -child.getHeight()); 
  16.             break; 
  17.     } 
  18.     child.setVisibility(INVISIBLE); 
  19.     addComponent(child, 0); 

4.在拖拽動(dòng)作的監(jiān)聽(tīng)回調(diào)方法中完成對(duì)視圖的更新

A.在update回調(diào)中設(shè)置打開(kāi)和關(guān)閉的邊界以及邊界內(nèi)的位置刷新

  1. if (getSurfaceView().getContentPositionY() + dragInfo.yOffset <= 0) { 
  2.     close(); 
  3. else if (getSurfaceView().getContentPositionY() + dragInfo.yOffset >= getHeight()) { 
  4.     open(); 
  5. else { 
  6.     getSurfaceView().setContentPositionY(getSurfaceView().getContentPositionY() + (float) dragInfo.yOffset); 
  7.     getCurrentBottomView().setContentPositionY(getCurrentBottomView().getContentPositionY() + (float) dragInfo.yOffset); 

B.在end中判斷滑動(dòng)的距離,如果大于設(shè)定的滑動(dòng)距離則直接將控件展開(kāi)或者關(guān)閉

  1. if (isCloseBeforeDrag && mDragDistanceY < 0) { 
  2.     if (Math.abs(mDragDistanceY) >= mWillOpenPercentAfterClose * getBottomViewHeight()) { 
  3.         open(); 
  4.     } else { 
  5.         close(); 
  6.     } 
  7. if (!isCloseBeforeDrag && mDragDistanceY > 0) { 
  8.     if (Math.abs(mDragDistanceY) >= mWillOpenPercentAfterClose * getBottomViewHeight()) { 
  9.         close(); 
  10.     } else { 
  11.         open(); 
  12.     } 

3.3. 編譯HAR包

利用Gradle可以將HarmonyOS Library庫(kù)模塊構(gòu)建為HAR包,構(gòu)建HAR包的方法如下:

在Gradle構(gòu)建任務(wù)中,雙擊PackageDebugHar或PackageReleaseHar任務(wù),構(gòu)建Debug類型或Release類型的HAR。

待構(gòu)建任務(wù)完成后,可以loadingview> bulid > outputs > har目錄中,獲取生成的HAR包。

項(xiàng)目源代碼地址:https://github.com/isoftstone-dev/SwipeBackLayout

歡迎交流:HWIS-HOS@isoftstone.com

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

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

https://harmonyos.51cto.com

 

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

2021-02-24 15:22:47

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

2021-03-01 09:48:24

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

2021-01-13 09:40:31

鴻蒙HarmonyOS開(kāi)發(fā)

2021-02-04 13:06:38

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

2021-04-16 09:28:18

鴻蒙HarmonyOS應(yīng)用

2021-06-28 14:48:03

鴻蒙HarmonyOS應(yīng)用

2021-01-18 09:52:20

鴻蒙HarmonyOS開(kāi)發(fā)

2021-02-04 09:45:19

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

2021-02-26 14:15:27

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

2021-01-12 12:04:40

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

2021-01-21 13:21:18

鴻蒙HarmonyOSPhotoview組件

2021-01-20 09:54:56

鴻蒙HarmonyOS開(kāi)發(fā)

2021-05-12 15:17:39

鴻蒙HarmonyOS應(yīng)用

2021-03-31 09:50:25

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

2021-01-22 17:33:03

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

2021-04-12 09:36:54

鴻蒙HarmonyOS應(yīng)用

2021-03-19 17:42:01

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

2021-04-20 09:42:20

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

2021-04-15 17:47:38

鴻蒙HarmonyOS應(yīng)用

2015-03-31 18:13:09

swipelistvi
點(diǎn)贊
收藏

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