鴻蒙入門指南,小白速來!0基礎學習路線分享,高效學習方法,重點答疑解惑--->【課程入口】
目錄:
1. SwipeLayout組件功能介紹
2. SwipeLayout使用方法
3. SwipeLayout開發實現
4.《HarmonyOS三方件開發指南》系列文章合集git
1. SwipeLayout組件功能介紹
1.1.功能介紹:
SwipeLayout組件是一個側滑刪除組件。github
1.2. 模擬器上運行效果:
app
2. SwipeLayout使用方法
2.1. 新建工程,增長組件Har包依賴
在應用模塊中添加HAR,只須要將SwipeLayout.har複製到entry\libs目錄下便可(因爲build.gradle中已經依賴的libs目錄下的*.har,所以不須要再作修改)。函數
2.2. 修改主頁面的佈局文件
修改主頁面的佈局文件ability_main.xml,將自定義的SwipeLayout添加到xml中,將初始狀態下展現的視圖添加到SwipeLayout做爲index爲0的子視圖:佈局
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:id="$+id:total1" ohos:height="match_parent" ohos:width="match_parent" ohos:background_element="gray" ohos:orientation="vertical"> <com.isoftstone.swipelayout.SwipeLayout ohos:id="$+id:sample2" ohos:height="80vp" ohos:width="match_parent" ohos:orientation="horizontal"> <Text ohos:id="$+id:bottom_layout1" ohos:height="match_parent" ohos:width="match_parent" ohos:background_element="white" ohos:multiple_lines="true" ohos:padding="10" ohos:text="要有最樸素的生活和最遙遠的夢想,即便明每天寒地凍,山高水遠,路遠馬亡。" ohos:text_alignment="left" ohos:text_size="14fp" ohos:visibility="visible"> </Text> <DirectionalLayout ohos:id="$+id:bottom_wrapper1" ohos:height="match_parent" ohos:width="360px" ohos:background_element="#ddff00" ohos:orientation="horizontal" ohos:visibility="visible"> <Text ohos:id="$+id:Texts1" ohos:height="match_parent" ohos:width="180px" ohos:background_element="#7B1FA2" ohos:left_padding="25" ohos:right_padding="25" ohos:text="收藏" ohos:text_alignment="center" ohos:text_color="#DC143C" ohos:text_size="14fp" ohos:visibility="visible" /> <Text ohos:id="$+id:texts2" ohos:height="match_parent" ohos:width="180px" ohos:background_element="#C7C7CC" ohos:left_padding="25" ohos:right_padding="25" ohos:text="刪除" ohos:text_alignment="center" ohos:text_color="#DC143C" ohos:text_size="14fp" ohos:visibility="visible" /> </DirectionalLayout> <Image ohos:id="$+id:images3" ohos:height="match_parent" ohos:width="match_parent" ohos:background_element="gray" ohos:image_src="$media:star" /> <DirectionalLayout ohos:id="$+id:bottom_fronts" ohos:height="match_parent" ohos:width="match_content" ohos:background_element="#ddff00" ohos:orientation="horizontal" ohos:visibility="visible"> <Image ohos:id="$+id:images1" ohos:height="match_parent" ohos:width="180px" ohos:background_element="green" ohos:image_src="$media:star"/> <Image ohos:id="$+id:images2" ohos:height="match_parent" ohos:width="180px" ohos:background_element="red" ohos:image_src="$media:trash"/> </DirectionalLayout> </com.isoftstone.swipelayout.SwipeLayout> <Image ohos:id="$+id:images" ohos:height="match_content" ohos:width="match_content" ohos:background_element="green" ohos:image_src="$media:star" ohos:layout_alignment="horizontal_center" ohos:top_margin="100vp"/> </DirectionalLayout>
2.3. 初始化SwipeLayout
在MainAbilitySlince類的onStart函數中,增長以下代碼。post
SwipeLayout swipeLayout = (SwipeLayout) findComponentById(ResourceTable.Id_sample1); DirectionalLayout right = (DirectionalLayout) findComponentById(ResourceTable.Id_bottom_wrapper); //初始化 swipeLayout.initializeSwipe(); DirectionalLayout left = (DirectionalLayout) findComponentById(ResourceTable.Id_bottom_front); Image image3 = (Image) findComponentById(ResourceTable.Id_image3); //將各個方向拖拽時對應展現的視圖添加到swipeLayout swipeLayout.addDrag(SwipeLayout.DragEdge.Left, right); swipeLayout.addDrag(SwipeLayout.DragEdge.Right, left); swipeLayout.addDrag(SwipeLayout.DragEdge.Bottom, image3);
3. SwipeLayout開發實現
3.1. 新建一個Module
新建一個Module,類型選擇HarmonyOS Library,模塊名爲SwipeLayout,如圖學習
3.2. 新建一個SwipeLayout類
新建一個SwipeLayout類,繼承自PositionLayout類
SwipeLayout的主要流程:gradle
1. 首先經過xml的構造方法,爲SwipeLayout添加拖拽監聽;
2. 將LinkedHashMap<DragEdge, Component> mDragEdges初始化爲空,並肯定主界面的顯示位置;
3. 經過public void addDrag(DragEdge dragEdge, Component child) 方法將可拖拽的方向和對應展現的視圖添加到mDragEdges,並設置其初始的ContentPosition;ui
public void addDrag(DragEdge dragEdge, Component child) { mDragEdges.put(dragEdge, child); switch (dragEdge) { case Left: child.setContentPosition(getWidth(), 0); break; case Right: HiLog.info(label, "Log_addDrag" + child.getHeight()); child.setContentPosition(-child.getWidth(), 0); break; case Top: child.setContentPosition(0, getHeight()); break; case Bottom: child.setContentPosition(0, -child.getHeight()); break; } child.setVisibility(INVISIBLE); addComponent(child, 0); }
4.在拖拽動做的監聽回調方法中完成對視圖的更新
A.在update回調中設置打開和關閉的邊界以及邊界內的位置刷新url
if (getSurfaceView().getContentPositionY() + dragInfo.yOffset <= 0) { close(); } else if (getSurfaceView().getContentPositionY() + dragInfo.yOffset >= getHeight()) { open(); } else { getSurfaceView().setContentPositionY(getSurfaceView().getContentPositionY() + (float) dragInfo.yOffset); getCurrentBottomView().setContentPositionY(getCurrentBottomView().getContentPositionY() + (float) dragInfo.yOffset); }
B.在end中判斷滑動的距離,若是大於設定的滑動距離則直接將控件展開或者關閉
if (isCloseBeforeDrag && mDragDistanceY < 0) { if (Math.abs(mDragDistanceY) >= mWillOpenPercentAfterClose * getBottomViewHeight()) { open(); } else { close(); } } if (!isCloseBeforeDrag && mDragDistanceY > 0) { if (Math.abs(mDragDistanceY) >= mWillOpenPercentAfterClose * getBottomViewHeight()) { close(); } else { open(); } }
3.3. 編譯HAR包
利用Gradle能夠將HarmonyOS Library庫模塊構建爲HAR包,構建HAR包的方法以下:
在Gradle構建任務中,雙擊PackageDebugHar或PackageReleaseHar任務,構建Debug類型或Release類型的HAR。
待構建任務完成後,能夠loadingview> bulid > outputs > har目錄中,獲取生成的HAR包。
項目源代碼地址:https://github.com/isoftstone-dev/SwipeBackLayout
歡迎交流:HWIS-HOS@isoftstone.com
做者:軟通田可輝
想了解更多內容,請訪問: 51CTO和華爲官方戰略合做共建的鴻蒙技術社區https://harmonyos.51cto.com