HarmonyOS 鸿蒙Next swiper轮播指示器怎么设置间距?
HarmonyOS 鸿蒙Next swiper轮播指示器怎么设置间距?
.indicator(
new DotIndicator()
.left(1)
.top(66)
.itemWidth(4)
.itemHeight(2)
.selectedItemWidth(4)
.selectedItemHeight(2)
.color(Color.White)
.selectedColor(Color.Red)
)
我只找到设置单个指示器的宽度、高度,没找到每个指示器之间的间距设置,以及滑动的时候,指示器执行动画的线条长度怎么设置。更多关于HarmonyOS 鸿蒙Next swiper轮播指示器怎么设置间距?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
当前swiper自有的属性不支持设置指示器的间距,如果要设置间距,需要自定义UI来实现指示器,会比较复杂。 以下示例代码给出了样例供你参考:
// xxx.ets
class MyDataSource implements IDataSource {
private list: number[] = []
constructor(list: number[]) {
this.list = list
}
totalCount(): number {
return this.list.length
}
getData(index: number): number {
return this.list[index]
}
registerDataChangeListener(listener: DataChangeListener): void {
}
unregisterDataChangeListener() {
}
}@Entry
@Component
struct SwiperExample {
private swiperController: SwiperController = new SwiperController()
private data: MyDataSource = new MyDataSource([])
@State widthLength:number=0
@State heightLength:number=0
@State currentIndex:number=0
// 实现导航点自定义动画
private swiperWidth: number = 0
private getTextInfo(index: number): Record<string, number> {
let strJson = getInspectorByKey(index.toString())
try {
let obj: Record<string, string> = JSON.parse(strJson)
let rectInfo: number[][] = JSON.parse('[' + obj.$rect + ']')
return { 'left': px2vp(rectInfo[0][0]), 'width': px2vp(rectInfo[1][0] - rectInfo[0][0]) }
} catch (error) {
return { 'left': 0, 'width': 0 }
}
}
private getCurrentIndicatorInfo(index: number, event: TabsAnimationEvent): Record<string, number> {
let nextIndex = index
if (index > 0 && event.currentOffset > 0) {
nextIndex--
} else if (index < 3 && event.currentOffset < 0) {
nextIndex++
}
let indexInfo = this.getTextInfo(index)
let nextIndexInfo = this.getTextInfo(nextIndex)
let swipeRatio = Math.abs(event.currentOffset / this.swiperWidth)
let currentIndex = swipeRatio > 0.5 ? nextIndex : index // 页面滑动超过一半,swiper切换到下一页。
let currentLeft = indexInfo.left + (nextIndexInfo.left - indexInfo.left) * swipeRatio
let currentWidth = indexInfo.width + (nextIndexInfo.width - indexInfo.width) * swipeRatio
return { 'index': currentIndex, 'left': currentLeft, 'width': currentWidth }
}
aboutToAppear(): void {
let list: number[] = []
for (let i = 1; i <= 6; i++) {
list.push(i);
}
this.data = new MyDataSource(list)
}build() {
Column({ space: 5 }) {
Stack({ alignContent: Alignment.Bottom }) {
Swiper(this.swiperController) {
LazyForEach(this.data, (item: string) => {
Text(item.toString())
.width('90%')
.height(160)
.backgroundColor(0xAFEEEE)
.textAlign(TextAlign.Center)
.fontSize(30)
}, (item: string) => item)
}
.cachedCount(2)
.index(0)
.autoPlay(false)
.interval(4000)
.loop(true)
.duration(1000)
.itemSpace(0)
.indicator(false)
.curve(Curve.Linear)
.onChange((index: number) => {
console.info(index.toString())
this.currentIndex=index
})
.onGestureSwipe((index: number, extraInfo: SwiperAnimationEvent) => {
console.info("index: " + index)
console.info("current offset: " + extraInfo.currentOffset)
// 在页面跟手滑动过程中,逐帧触发该回调。
let currentIndicatorInfo = this.getCurrentIndicatorInfo(index,extraInfo)
this.currentIndex = currentIndicatorInfo.index
})
.onAnimationStart((index: number, targetIndex: number, extraInfo: SwiperAnimationEvent) => {
console.info("index: " + index)
console.info("targetIndex: " + targetIndex)
console.info("current offset: " + extraInfo.currentOffset)
console.info("target offset: " + extraInfo.targetOffset)
console.info("velocity: " + extraInfo.velocity)
// 切换动画开始时触发该回调。下划线跟着页面一起滑动,同时宽度渐变。
this.currentIndex = targetIndex
})
.onAnimationEnd((index: number, extraInfo: SwiperAnimationEvent) => {
console.info("index: " + index)
console.info("current offset: " + extraInfo.currentOffset)
})Row() {
LazyForEach(this.data, (item: string, index: number) => {
Column()
.width(this.currentIndex===index?15:5)
.height(5)
// 实现设置指示器的间距
.margin(15)
.borderRadius(5)
.backgroundColor(this.currentIndex===index?Color.Red:Color.Gray)
}, (item: string) => item)
}
.margin({bottom:4})
}
}.width('100%')
.margin({ top: 5 })
}
}
更多关于HarmonyOS 鸿蒙Next swiper轮播指示器怎么设置间距?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS(鸿蒙)系统中,设置Next swiper轮播指示器的间距通常涉及调整swiper组件的样式属性。具体步骤如下:
-
定义swiper组件:在XML布局文件中定义swiper组件,并指定轮播图片的数据源。
-
设置指示器样式:通过swiper组件的
indicator
属性或者相关样式配置,来设置指示器的外观,包括间距。 -
调整间距:鸿蒙系统允许通过
indicator_item_space
属性直接设置指示器之间的间距。可以在swiper组件的样式中添加此属性,并赋予适当的值来调整间距。 -
应用样式:确保swiper组件应用了上述样式配置,以便间距设置生效。
例如,可以在swiper组件的样式中添加如下代码来调整指示器间距:
<swiper
ohos:id="$+id:swiper"
ohos:width="match_parent"
ohos:height="match_parent"
ohos:indicator_item_space="20vp"> <!-- 20vp为示例值,可以根据需要调整 -->
<!-- swiper内容 -->
</swiper>
上述代码中,ohos:indicator_item_space
用于设置指示器之间的间距,单位为vp(虚拟像素)。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html