HarmonyOS基本UI封裝——Dialog 彈出框璃赡、loading加載和List下拉刷新加載更多(四)


highlight: ascetic
theme: cyanosis


前言

文章系列

簡介

鴻蒙基本庫封裝住册,提升鴻蒙開發(fā)效率

安裝

ohpm install @peakmain/library

使用

一、Dialog 彈出框

dialog 彈出框.gif

導入依賴

import { DialogComponent } from '@peakmain/library/Index';
1. 默認彈窗

基本布局

  title: string = '確認刪除訂單';
  message: string = '刪除后將無法恢復雹拄,無法再次操作收奔。';
  leftText: string = '取消';
  rightTextColor: ResourceColor = $r("app.color.color_194d53")//右邊文本顏色,#194d53為默認文本顏色
  rightTextBold: boolean = true//右邊文本是否加粗,默認是加粗
        
  confirm: CustomDialogController = new CustomDialogController({
    builder: DialogComponent({
      title: this.title,
      message: this.message,
      leftText: this.leftText,
      rightTextColor: this.rightTextColor,
      rightTextBold: this.rightTextBold,
      rightTextClick: () => {
        promptAction.showToast({
          message: `點擊了${this.title}`,
        })
        this.confirm.close()
        this.resetDefault()
      }
    }),
    customStyle: true,
    alignment: DialogAlignment.Center,

  })

顯示彈窗

this.confirm.open()
2.提示彈窗
this.title = "提示"
this.leftText = ""
this.rightTextColor = $r("app.color.color_1989fa")
this.confirm.open()
3.彈窗(無標題)
this.title = ""
this.confirm.open()

二滓玖、Loading加載

loading 加載.gif

導入依賴

import { PkLoading } from '@peakmain/library/Index';
1. 默認Loading

基本布局繪制

  title: string = "加載中..."
  isVertical: boolean = true//默認是垂直方向
  loadingColor: ResourceColor = Color.White//默認加載progress顏色為白色
  textColor: ResourceColor = Color.White//默認文本顏色為白色 
  backgroundResourceColor: ResourceColor = "rgba(0,0,0,0.46)"http://默認背景顏色rgba(0,0,0,0.46)
  loading: CustomDialogController = new CustomDialogController({
    builder: PkLoading({
      title: this.title,
      isVertical: this.isVertical,
      loadingColor:this.loadingColor,
      textColor:this.textColor,
      backgroundResourceColor:this.backgroundResourceColor
    }),
    customStyle: true,
    isModal: false,//是否有蒙層,false表示沒有蒙層坪哄,true表示有蒙層
    autoCancel: true,
    cancel: () => { //監(jiān)聽取消事件
      this.resetData()
    }
  })

開啟loading

this.loading.open()
2. 自定義加載文案
 this.title = "數據加載中..."
 this.loading.open()
3. 水平方向
this.isVertical = false
this.loading.open()
4. 自定義顏色(背景顏色、文本顏色势篡、加載顏色)
this.loadingColor=Color.Pink
this.textColor=Color.Pink
this.backgroundResourceColor=Color.Blue
this.loading.open()

三翩肌、List列表

List 列表.gif

導入依賴

import { PkList } from '@peakmain/library/Index'
1. 基本使用
//數據源
@State arr: String[] = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
@State
page: number = 1 // 第幾頁
pageSize: number = 2 //共幾頁
PkList({
  dataSource: this.arr,//數據源
  finished: this.page >= this.pageSize,//是否已完成,分頁加載
  renderItem: (item) => {
    this.renderItem(item)//每一條item布局
  },
}).margin({
  bottom: 20
})
@Builder
renderItem(item: object) {
   Column() {
      Text('' + item)
         .width('100%')
         .height(100)
         .fontSize(16)
         .textAlign(TextAlign.Center)
         .borderRadius(10)
         .backgroundColor(Color.White)
   }.margin({
      bottom: 10,
      left: 10, right: 10
   })
      .border({
         width: 0.5,
         color: Color.Red
      })
      .borderRadius(20)
}
2. 加載更多:調用onLoad方法即可
PkList({
  dataSource: this.arr,//數據源
  finished: this.page >= this.pageSize,//是否已完成,分頁加載
  renderItem: (item) => {
    this.renderItem(item)
  },
  onLoad: async () => {
    await this.getNewData(false)
  }
}).margin({
  bottom: 20
})
async getNewData(isRefresh: boolean) {
   console.log("執(zhí)行了getNewData..." + isRefresh)
   const tmp = await this.getData(isRefresh)
   if (isRefresh) {
      this.arr = tmp
   } else {
      this.arr.push(...tmp)
   }
}
getData(isRefresh: boolean) {
   console.log("執(zhí)行了getData..." + isRefresh)
   return new Promise<String[]>((resolve) => {
      let tmp: String[]
      setTimeout(() => {
         if (!isRefresh) {
            this.page++
            tmp = ['new_0', 'new_1', 'new_2', 'new_3', 'new_4', 'new_5']
         } else {
            this.page = 1
            tmp = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
         }
         console.log("當前頁數:" + this.page)
         resolve(tmp); // 執(zhí)行完成后調用 resolve
      }, 2000);
   });
}
3. 下拉刷新:onRefresh方法即可
PkList({
  dataSource: this.arr,//數據源
  finished: this.page >= this.pageSize,//是否已完成,分頁加載
  onRefresh: async () => {//下拉刷新
    await this.getNewData(true)
  },
  renderItem: (item) => {
    this.renderItem(item)
  },
  onLoad: async () => {//加載更多
    await this.getNewData(false)
  }
}).margin({
  bottom: 20
})
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市殊霞,隨后出現的幾起案子摧阅,更是在濱河造成了極大的恐慌,老刑警劉巖绷蹲,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件棒卷,死亡現場離奇詭異,居然都是意外死亡祝钢,警方通過查閱死者的電腦和手機比规,發(fā)現死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來拦英,“玉大人蜒什,你說我怎么就攤上這事“坦溃” “怎么了灾常?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長铃拇。 經常有香客問我钞瀑,道長,這世上最難降的妖魔是什么慷荔? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任雕什,我火速辦了婚禮,結果婚禮上显晶,老公的妹妹穿的比我還像新娘贷岸。我一直安慰自己,他們只是感情好磷雇,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布偿警。 她就那樣靜靜地躺著,像睡著了一般唯笙。 火紅的嫁衣襯著肌膚如雪户敬。 梳的紋絲不亂的頭發(fā)上落剪,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天,我揣著相機與錄音尿庐,去河邊找鬼。 笑死呢堰,一個胖子當著我的面吹牛抄瑟,可吹牛的內容都是我干的。 我是一名探鬼主播枉疼,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼皮假,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了骂维?” 一聲冷哼從身側響起惹资,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎航闺,沒想到半個月后褪测,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡潦刃,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年侮措,在試婚紗的時候發(fā)現自己被綠了咬像。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片轿偎。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖吹榴,靈堂內的尸體忽然破棺而出胧洒,到底是詐尸還是另有隱情畏吓,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布卫漫,位于F島的核電站菲饼,受9級特大地震影響,放射性物質發(fā)生泄漏汛兜。R本人自食惡果不足惜巴粪,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望粥谬。 院中可真熱鬧肛根,春花似錦、人聲如沸漏策。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽掺喻。三九已至芭届,卻和暖如春储矩,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背褂乍。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工持隧, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人逃片。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓屡拨,卻偏偏與公主長得像,于是被迫代替她去往敵國和親褥实。 傳聞我的和親對象是個殘疾皇子呀狼,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內容