前言
輪播圖作為應用程序中最普通使用的控件被廣泛應用,相信對于來發(fā)者來說并不陌生。在 Android 中實現(xiàn)一個 輪播圖很多選擇使用第三方的插件,畢竟在有限的開發(fā)排期中自己動手去實現(xiàn)一個輪播圖 并不那么簡單,需要考慮的細節(jié)很多。不過在 HarmonyOS 中實現(xiàn)一個輪播圖卻是十分的簡單,本篇文章教你在最短的時間內(nèi)快速實現(xiàn)一個自定義的 輪播圖,建議點贊收藏!
實現(xiàn)效果
需求分析
- 支持自定義循環(huán)播放,自動播放。
- 支持自定義播放時間間隔。
- 支持橫向和豎向輪播。
- 支持自定義指示器。
技術實現(xiàn)
- 在鴻蒙原生開發(fā)中,為開發(fā)者提供了很多自由度高的控件,要想實現(xiàn)一個輪播圖,最簡單的方式就是選擇合適的控件,這里使用的是官方提供的 Swiper 控件。Swiper 控件支持多種功能屬性。
swiperController: SwiperController = new SwiperController()
Swiper(this.swiperController)
- 選擇好 Swiper 控件后,需要為 Swiper 控件提供數(shù)據(jù)。這里使用 LazyForEach 懶加載的方式,提高性能。注意如果使用 LazyForEach 的 方式就需要搭配****IDataSource 使用 。
@State data: LazyData< PhotoData > = new LazyData()
Swiper(this.swiperController) {
LazyForEach(this.data, (item: PhotoData, index: number) = > {
Image($r(`app.media.` + item.id))
.width('100%')
.height('30%')
}, (item: PhotoData) = > JSON.stringify(item))
}
export class LazyData< T > implements IDataSource{
// 監(jiān)聽器
private listeners: DataChangeListener[] = []
private array:Array< T > = []
totalCount(): number {
return this.array.length
}
getData(index: number): T {
return this.array[index]
}
registerDataChangeListener(listener: DataChangeListener): void {
if (this.listeners.indexOf(listener)< 0) {
this.listeners.push(listener)
}
}
unregisterDataChangeListener(listener: DataChangeListener): void {
const index = this.listeners.indexOf(listener)
if (index >=0) {
this.listeners.splice(index,1)
}
}
push(data:T){
this.array.push(data)
}
}
自定義LazyData 類,實現(xiàn) IDataSource 接口,并實現(xiàn)其中的方法即可。
- 簡單實現(xiàn)了展示功能后,接下來為輪播圖提供一些基本屬性。
.loop(true) //是否輪播
.autoPlay(true) //是否自動播放
.interval(this.duration) //播放間隔時間
.indicator(true) //是否顯示指示器
.vertical(false) //是否豎向播放
.indicatorStyle({selectedColor:this.bgColor,color:Color.White}) //指示器的相關配置
- 由于 Swiper 提供的默認指示器可能不滿足實際的開發(fā)需求,這里實現(xiàn)一個自定義的指示器。首先將默認指示器設置為 false,然后實現(xiàn) onChange 方法,用來獲取圖片切換的下標。
.indicator(false)
.onChange((index:number)= >{
this.currentIndex = index
})
- 自定義指示器組件。
@Builder
progressView() {
Row({ space: 5 }) {
LazyForEach(this.data, (item: PhotoData, index: number) = > {
Stack({ alignContent: Alignment.Start }) {
Row()
.zIndex(1)
.width(this.currentIndex >= index && !this.slide ? '100%' : '0')
.height(2)
.borderRadius(2)
.backgroundColor(this.bgColor)
.animation(!this.slide ? {
duration: this.duration - 400,
curve: Curve.Linear,
iterations: 1,
playMode: PlayMode.Normal,
onFinish: () = > {
if (this.currentIndex === this.data.totalCount() - 1) {
this.duration = 400;
this.currentIndex = -1;
}
}
} : { duration: 0, iterations: 1 })
}
.width('100%')
.height(2)
.borderRadius(2)
.backgroundColor(this.currentIndex >= index && this.slide ? this.bgColor : Color.Grey)
.layoutWeight(1)
}, (item: PhotoData) = > JSON.stringify(item))
}
.width('50%')
.height(50)
}
總結
在鴻蒙實際開發(fā)中,實現(xiàn)一個輪播圖是十分方便的,但是這并不是意味著所有功能都簡單,還有一些看似簡單的效果實現(xiàn)起來卻是十分的復雜。學會的小伙伴趕緊動手試試吧!
-
Android
+關注
關注
12文章
3971瀏覽量
129877 -
應用程序
+關注
關注
38文章
3328瀏覽量
58872 -
HarmonyOS
+關注
關注
80文章
2145瀏覽量
32489
發(fā)布評論請先 登錄
HarmonyOS實戰(zhàn):快速實現(xiàn)一個上下滾動的廣告控件
KiCad 中的自定義規(guī)則(KiCon 演講)

HarmonyOS實戰(zhàn):自定義時間選擇器

HarmonyOS實戰(zhàn):高德地圖自定義定位圖標展示

HarmonyOS應用自定義鍵盤解決方案
如何添加自定義單板
如何快速創(chuàng)建用戶自定義Board和App工程

Altium Designer 15.0自定義元件設計

think-cell:自定義think-cell(四)

think-cell;自定義think-cell(一)

創(chuàng)建自定義的基于閃存的引導加載程序(BSL)

如何創(chuàng)建TestStand自定義步驟

評論