本項(xiàng)目基于 ArkUI 中 TS 擴(kuò)展的聲明式開發(fā)范式,關(guān)于語法和概念直接看官網(wǎng)。
基于 TS 擴(kuò)展的聲明式開發(fā)范式 1:
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ui-ts-overview-0000001192705715
基于 TS 擴(kuò)展的聲明式開發(fā)范式 2:
https://developer.harmonyos.com/cn/docs/documentation/doc-references/ts-framework-directory-0000001111581264
本文介紹列表滑動(dòng)刪除:
-
列表中只允許滑出其中一項(xiàng)
-
如果有打開的項(xiàng),點(diǎn)擊或滑動(dòng)其他項(xiàng)都會(huì)關(guān)閉打開的項(xiàng)
-
點(diǎn)擊刪除,刷新列表界面
主要知識(shí)點(diǎn)
可滑動(dòng)的容器組件(Scroll):
https://developer.harmonyos.com/cn/docs/documentation/doc-references/ts-container-scroll-0000001163543527
觸摸事件(onTouch):
https://developer.harmonyos.com/cn/docs/documentation/doc-references/ts-universal-events-touch-0000001158261221
實(shí)現(xiàn)思路
我把界面精簡(jiǎn)了一下,減少代碼量,幫助更好的理解主要邏輯。
①item 布局 主要使用 scroll 包裹內(nèi)容,scroll 設(shè)置為橫向滑動(dòng)。部分代碼如下:
.....
Scroll(){
Row(){
Text('內(nèi)容數(shù)據(jù)')
.width('100%').height(65)
Button(){
Text('刪除')
}
.width(100).height(65)
}
}.scrollable(ScrollDirection.Horizontal)//設(shè)置為橫向滑動(dòng)
.....
②Scroll 容器給 Scroll 容器綁定滑動(dòng)組件的控制器,只用到其中的一個(gè)方法:滑動(dòng)到指定位置 scrollTo。
scrollTo(
value:{
xOffset:number|string,yOffset:number|string,animation?
:{duration:number,curve:Curve}
}
);
看源碼得知可以設(shè)置動(dòng)畫時(shí)間,注意:時(shí)間目前好像不能設(shè)置 300 毫秒以上,往下設(shè)置可以。
部分代碼如下:
.....
//初始化控制器
privatescroller=newScroller()
Scroll(scroller){//控制器綁定到滑動(dòng)容器中
Row(){
Text('內(nèi)容數(shù)據(jù)')
.width('100%').height(65)
Button(){
Text('刪除')
}
.width(100).height(65)
}
}.scrollable(ScrollDirection.Horizontal)
Button(){
Text('點(diǎn)擊回到原位')
}.onClick(()=>{
scroller.scrollTo({xOffset:0,yOffset:0,animation:{duration:200,curve:Curve.Linear}})
})
.....
③設(shè)置觸摸事件根據(jù)移動(dòng)的偏移量,判斷大于刪除布局寬度的一半則:打開刪除布局。
部分代碼如下:
.....
//初始化控制器
privatescroller=newScroller()
//按下的x軸坐標(biāo)
privatedownX=0
//刪除按鈕的寬度
privatedeleteWidth=100
Scroll(scroller){//控制器綁定到滑動(dòng)容器中
Row(){
Text('內(nèi)容數(shù)據(jù)')
.width('100%').height(65)
Button(){
Text('刪除')
}
.width(this.deleteWidth).height(65)
}
}.scrollable(ScrollDirection.Horizontal)
.onTouch((event:TouchEvent)=>{//觸摸事件
//根據(jù)觸摸類型判斷
switch(event.type){
caseTouchType.Down://觸摸按下
//記錄按下的x軸坐標(biāo)
this.downX=event.touches[0].x
break
caseTouchType.Up://觸摸抬起
//觸摸抬起,根據(jù)x軸總偏移量,判斷是否打開刪除
letxOffset=event.touches[0].x-this.downX
//滑到目標(biāo)x軸的位置
vartoxOffset=0
//偏移量超過刪除按鈕一半且左滑,設(shè)置打開
if(Math.abs(xOffset)>vp2px(this.deleteWidth)/2&&xOffset0){
//刪除布局寬度
toxOffset=vp2px(this.deleteWidth)
}
//滑動(dòng)指定位置,設(shè)置動(dòng)畫
item.scroller.scrollTo({xOffset:toxOffset,yOffset:0,
animation:{duration:300,curve:Curve.Linear}})
//重置按下的x軸坐標(biāo)
this.downX=0
break
}
})
.....
④使用列表加載需要主要的點(diǎn):
-
需要給每個(gè) item 綁定控制器,這樣才能控制對(duì)應(yīng)的 item 打開或關(guān)閉
-
打開的 item 記錄一下數(shù)據(jù),點(diǎn)擊內(nèi)容或刪除、滑動(dòng)其他 item:如果有帶打開的 item,進(jìn)行關(guān)閉
以下是完整代碼,可直接粘貼運(yùn)行使用:
classTestData{
content:string
scroller:Scroller
constructor(content:string,scroller:Scroller){
this.content=content
this.scroller=scroller
}
}
@Entry
@Component
structSlidingDeleteList{
//刪除按鈕的寬度
privatedeleteWidth=100
//按下的x軸坐標(biāo)
privatedownX=0
//已經(jīng)打開刪除的數(shù)據(jù)
privateopenDeleteData:TestData=null
//測(cè)試數(shù)據(jù)
@StateprivatelistData:Array=[
{content:'內(nèi)容數(shù)據(jù)1',scroller:newScroller()},{content:'內(nèi)容數(shù)據(jù)2',scroller:newScroller()},
{content:'內(nèi)容數(shù)據(jù)3',scroller:newScroller()},{content:'內(nèi)容數(shù)據(jù)4',scroller:newScroller()},
{content:'內(nèi)容數(shù)據(jù)5',scroller:newScroller()},{content:'內(nèi)容數(shù)據(jù)6',scroller:newScroller()},
{content:'內(nèi)容數(shù)據(jù)7',scroller:newScroller()},{content:'內(nèi)容數(shù)據(jù)8',scroller:newScroller()},
]
@BuilderCustomItem(item:TestData){
Scroll(item.scroller){
Row(){
Text(item.content)
.width('100%').height(65)
.fontSize(16).textAlign(TextAlign.Center)
.onClick(()=>{
//如果刪除按鈕打開,關(guān)閉刪除按鈕且返回
if(this.openDeleteData!=null){
this.openDeleteData.scroller.scrollTo({xOffset:0,yOffset:0,
animation:{duration:100,curve:Curve.Linear}})
this.openDeleteData=null
return
}
console.log('========點(diǎn)擊內(nèi)容=========')
})
Button(){
Text('刪除')
.fontSize(15)
.fontColor(Color.White)
}
.type(ButtonType.Normal)
.width(this.deleteWidth).height(65)
.backgroundColor(Color.Red)
.onClick(()=>{
//刪除當(dāng)前數(shù)據(jù)
this.listData.splice(this.listData.indexOf(item),1)
//關(guān)閉刪除按鈕
if(this.openDeleteData!=null){
this.openDeleteData.scroller.scrollTo({xOffset:0,yOffset:0,
animation:{duration:100,curve:Curve.Linear}})
this.openDeleteData=null
}
console.log('========點(diǎn)擊刪除=========')
})
}
}.scrollable(ScrollDirection.Horizontal)
.onTouch((event:TouchEvent)=>{//觸摸事件
//判斷是否有打開刪除組件,有則關(guān)閉
if(this.openDeleteData!=null&&this.openDeleteData!=item){
this.openDeleteData.scroller.scrollTo({xOffset:0,yOffset:0,
animation:{duration:100,curve:Curve.Linear}})
}
//根據(jù)觸摸類型判斷
switch(event.type){
caseTouchType.Down://觸摸按下
//記錄按下的x軸坐標(biāo)
this.downX=event.touches[0].x
break
caseTouchType.Up://觸摸抬起
//觸摸抬起,根據(jù)x軸總偏移量,判斷是否打開刪除
letxOffset=event.touches[0].x-this.downX
//防止消費(fèi)點(diǎn)擊事件
if(xOffset==0){
return
}
//滑到x軸的位置
vartoxOffset=0
//開啟刪除的對(duì)象置為null
this.openDeleteData=null;
//偏移量超過刪除按鈕一半且左滑,設(shè)置打開
if(Math.abs(xOffset)>vp2px(this.deleteWidth)/2&&xOffset0){
//刪除布局寬度
toxOffset=vp2px(this.deleteWidth)
this.openDeleteData=item
}
//滑動(dòng)指定位置,設(shè)置動(dòng)畫
item.scroller.scrollTo({xOffset:toxOffset,yOffset:0,
animation:{duration:300,curve:Curve.Linear}})
//重置按下的x軸坐標(biāo)
this.downX=0
break
}
})
}
build(){
Column(){
List(){
ForEach(this.listData,item=>{
ListItem(){
this.CustomItem(item)
}
},item=>item.toString())
}.divider({color:'#f1efef',strokeWidth:1})
}
.width('100%')
.height('100%')
}
}
結(jié)尾
因?yàn)?ArkUI 聲明式開發(fā),是鴻蒙新出的東西,API 還不是那么完善,后續(xù)跟進(jìn)官網(wǎng)更新。 以下是需要優(yōu)化點(diǎn):ArkUI 中的 TS 沒有 JS 中的新出的插槽概念,要不然直接封裝成組件,提供兩個(gè)對(duì)外的接口,一個(gè)傳入內(nèi)容布局、一個(gè)操作布局,就像 Android 的組件庫一樣,使用者不需要知道內(nèi)部實(shí)現(xiàn)。 作者:梁青松
-
接口
+關(guān)注
關(guān)注
33文章
8885瀏覽量
152990 -
API
+關(guān)注
關(guān)注
2文章
1555瀏覽量
63315 -
JS
+關(guān)注
關(guān)注
0文章
78瀏覽量
18349 -
鴻蒙
+關(guān)注
關(guān)注
57文章
2469瀏覽量
43652
原文標(biāo)題:在鴻蒙上實(shí)現(xiàn)聊天列表滑動(dòng)刪除功能!
文章出處:【微信號(hào):gh_834c4b3d87fe,微信公眾號(hào):OpenHarmony技術(shù)社區(qū)】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
Get這個(gè)秘籍,鴻蒙原生應(yīng)用頁面滑動(dòng)絲滑無比
HarmonyOS NEXT 原生應(yīng)用開發(fā):社交聊天對(duì)話過程實(shí)現(xiàn)
鴻蒙原生頁面高性能解決方案上線OpenHarmony社區(qū) 助力打造高性能原生應(yīng)用
HarmonyOS NEXT 應(yīng)用開發(fā)練習(xí):智能視頻推薦
滑動(dòng)變阻器的優(yōu)缺點(diǎn)對(duì)比
滑動(dòng)變阻器的接線方法 滑動(dòng)變阻器的常見故障與維修
滑動(dòng)變阻器的應(yīng)用領(lǐng)域 滑動(dòng)變阻器在電路中的作用
滑動(dòng)變阻器有哪些分類?
Taro 鴻蒙技術(shù)內(nèi)幕系列(二):如何讓 W3C 標(biāo)準(zhǔn)的 CSS跑在鴻蒙上

滑動(dòng)變阻器的作用是什么?
滑動(dòng)變阻器調(diào)到最大阻值的目的
探究電流與電壓的關(guān)系滑動(dòng)變阻器的作用
鴻蒙ArkTS聲明式開發(fā):跨平臺(tái)支持列表 SwipeGesture之基礎(chǔ)手勢(shì)

鴻蒙ArkUI開發(fā):常用布局【 創(chuàng)建列表(List)】

評(píng)論