更多内容请访问:与华为官方共建的鸿蒙技术社区https://harmonyos.51cto.com1.SwipeLayout组件功能介绍1.1.功能介绍:SwipeLayout组件是一个滑动删除组件。1.2.在模拟器上运行的效果:2.SwipeLayout的使用方法2.1。新建一个项目,添加组件Har包依赖在application模块中添加HAR,只需将SwipeLayout.har复制到entry\libs目录下(由于build.gradle已经依赖了libs目录下的*.har,所以没有必要修改它)。2.2.修改主页面的布局文件修改主页面的布局文件ability_main.xml,在xml中添加自定义的SwipeLayout,将初始状态显示的view添加到SwipeLayout中作为index为0的子view:2.3。在MainAbilitySlince类中初始化SwipeLayout在onStart函数中,添加如下代码SwipeLayoutswipeLayout=(SwipeLayout)findComponentById(ResourceTable.Id_sample1);DirectionalLayoutright=(DirectionalLayout)findComponentById(ResourceTable.Id_bottom_wrapper);//初始化swipeLayout.initializeSwipe();DirectionalLayoutleft=(DirectionalLayout)findComponentById(ResourceTable.Id_bottom_front);Imageimage3=(Image)findComponentById(ResourceTable.Id_image3);//在各个方向拖拽时将对应的view添加到swipeLayout中swipeLayout.addDrag(SwipeLayout.DragEdge.Left,right);(SwipeLayout.DragEdge.Bottom,image3);3.SwipeLayout开发与实现3.1.新建Module新建Module,类型选择HarmonyOSLibrary,模块名称为SwipeLayout,如图3.2所示。新建一个SwipeLayout类新建一个SwipeLayout类,继承自PositionLayoutSwipeLayout类的主要流程:1.首先,通过xml构造方法给SwipeLayout添加拖拽监听;2.LinkedHashMap3。通过publicvoidaddDrag(DragEdgedragEdge,Componentchild)方法,将可拖动方向和对应的显示视图添加到mDragEdges中,并设置其初始ContentPosition;publicvoidaddDrag(DragEdgedragEdge,Componentchild){mDragEdges.put(dragEdge,child);switch(dragEdge){caseLeft:child.setContentPosition(getWidth(),0);break;caseRight:HiLog.info(label,"Log_addDrag"+child.getHeight());child.setContentPosition(-child.getWidth(),0);break;caseTop:child.setContentPosition(0,getHeight());break;caseBottom:child.setContentPosition(0,-child.getHeight());break;}child.setVisibility(INVISIBLE);addComponent(child,0);}4.在拖动动作的监听回调方法中完成view的更新A.在更新回调if(getSurfaceView().getContentPositionY()+dragInfo.yOffset<=0){close();}elseif(getSurfaceView().getContentPositionY()+dragInfo.yOffset>=getHeight()){open();}else{getSurfaceView().setContentPositionY(getSurfaceView().getContentPositionY()+(float)dragInfo.yOffset);getCurrentBottomView().setContentPositionY(getCurrentBottomView().getContentPositionY()+(float)dragInfo.yOffset);}B.最后判断滑动距离,如果大于设置的滑动距离则直接展开或关闭控件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{打开();}}3.3。编译HAR包使用Gradle,可以将HarmonyOSLibrary模块编译成HAR包。构建HAR包的方法如下:在Gradle构建任务中,双击PackageDebugHar或PackageReleaseHar任务,构建Debug类型或Release类型。HAR构建任务完成后,可以在loadingview>build>outputs>har目录下获取生成的HAR包。项目源码地址:https://github.com/isoftstone-dev/SwipeBackLayout欢迎交流:HWIS-HOS@isoftstone.com更多信息请访问:与华为官方共建的鸿蒙技术社区https://harmonyos。51cto.com