鴻蒙HarmonyOS三方件開(kāi)發(fā)指南-SwipeLayout側(cè)滑刪除
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
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的子視圖:
- <?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="要有最樸素的生活和最遙遠(yuǎn)的夢(mèng)想,即使明天天寒地凍,山高水遠(yuǎn),路遠(yuǎn)馬亡。"
- 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函數(shù)中,增加如下代碼。
- 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);
- //將各個(gè)方向拖拽時(shí)對(duì)應(yīng)展示的視圖添加到swipeLayout
- swipeLayout.addDrag(SwipeLayout.DragEdge.Left, right);
- swipeLayout.addDrag(SwipeLayout.DragEdge.Right, left);
- 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;
- 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.在拖拽動(dòng)作的監(jiān)聽(tīng)回調(diào)方法中完成對(duì)視圖的更新
A.在update回調(diào)中設(shè)置打開(kāi)和關(guān)閉的邊界以及邊界內(nèi)的位置刷新
- 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中判斷滑動(dòng)的距離,如果大于設(shè)定的滑動(dòng)距離則直接將控件展開(kāi)或者關(guān)閉
- 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庫(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ū)