HarmonyOS三方件开发指南(13)

Wesley13
• 阅读 358

鸿蒙入门指南,小白速来!0基础学习路线分享,高效学习方法,重点答疑解惑--->【课程入口】

目录:
1. SwipeLayout组件功能介绍
2. SwipeLayout使用方法
3. SwipeLayout开发实现
4.《HarmonyOS三方件开发指南》系列文章合集

1. SwipeLayout组件功能介绍
1.1.功能介绍:
SwipeLayout组件是一个侧滑删除组件。

1.2. 模拟器上运行效果:
 HarmonyOS三方件开发指南(13)

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函数中,增加如下代码。

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,如图

HarmonyOS三方件开发指南(13)

3.2. 新建一个SwipeLayout类
新建一个SwipeLayout类,继承自PositionLayout类
 SwipeLayout的主要流程:

1. 首先通过xml的构造方法,为SwipeLayout添加拖拽监听;
2. 将LinkedHashMap<DragEdge, Component> mDragEdges初始化为空,并确定主界面的显示位置;
3. 通过public void addDrag(DragEdge dragEdge, Component child) 方法将可拖拽的方向和对应展示的视图添加到mDragEdges,并设置其初始的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.在拖拽动作的监听回调方法中完成对视图的更新
A.在update回调中设置打开和关闭的边界以及边界内的位置刷新

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

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
PPDB:今晚老齐直播
【今晚老齐直播】今晚(本周三晚)20:0021:00小白开始“用”飞桨(https://www.oschina.net/action/visit/ad?id1185)由PPDE(飞桨(https://www.oschina.net/action/visit/ad?id1185)开发者专家计划)成员老齐,为深度学习小白指点迷津。
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
2年前
HarmonyOS三方件开发指南(11)——Updownfile
鸿蒙入门指南,小白速来!从萌新到高手,怎样快速掌握鸿蒙开发?【课程入口】(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fharmonyos.51cto.com%2Factivity%2F43%23kyzg)目录:1\.UpDownfile功能介绍(
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这