HarmonyOS Swiper组件的Indicator设置bottom(0)没有贴底

private swiperController: SwiperController = new SwiperController() 
 
@Builder 
content() { 
 Column() { 
  Swiper(this.swiperController) { 
   Stack() { 
    Text("1") 
     .width('100%') 
     .height(155) 
     .backgroundColor('#a067c8ff') 
     .textAlign(TextAlign.Center) 
     .fontSize(30) 
   } 
   .alignContent(Alignment.TopStart) 
   .margin({ left: 4, right: 4 }) 
   .height(172) 
 
   Stack() { 
    Text("2") 
     .width('100%') 
     .height(155) 
     .backgroundColor('#4067c8ff') 
     .textAlign(TextAlign.Center) 
     .fontSize(30) 
   }.alignContent(Alignment.TopStart) 
   .margin({ left: 4, right: 4 }) 
   .height(172) 
 
 
   Stack() { 
    Text("3") 
     .width('100%') 
     .height(172) 
     .backgroundColor('#7067c8ff') 
     .textAlign(TextAlign.Center) 
     .fontSize(30) 
   }.alignContent(Alignment.TopStart) 
   .margin({ left: 4, right: 4 }) 
   .height(172) 
 
  } 
  .loop(true) 
  .autoPlay(true) 
  .interval(5000) 
  .nextMargin(12) 
  .prevMargin(12) 
  .height(172) 
  .indicator( 
   Indicator.dot() 
    .bottom(0) 
    .itemWidth(10) 
    .itemHeight(2) 
    .selectedItemWidth(16) 
    .selectedItemHeight(2) 
    .color('#1E666666') 
    .selectedColor('#666666') 
  ) 
 } 
 .width('100%').height(172).alignSelf(ItemAlign.Start) 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.

如上代码,设置了Indicator.dot().bottom(0)想让指示器贴底,但是发现还有一段距离,不能实现紧贴底部。

HarmonyOS
2024-08-28 19:01:12
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

Indicator 导航点的可见范围边界至实际范围边界存在一定距离,该距离会随着itemWidth、itemHeight、selectedItemWidth、selectedItemHeight等参数变大而变大,因此无法完全靠左。参考地址:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-swiper-V5

目前Indicator与swiper组件的边界有一定距离,bottom(0)不是贴在边界处的效果,以下是尝试解决方法:

1、将swiper分开,上方是内容区,下方是Indicator区域,可设置内容区的宽高调整Indicator区域的大小 ,您可以参考此demo:

https://gitee.com/harmonyos-cases/cases/blob/master/CommonAppDevelopment/feature/indicatorbelowswiper/README.md#/harmonyos-cases/cases/blob/master/CommonAppDevelopment/feature/indicatorbelowswiper/src/main/ets/view/IndicatorBelowSwiper.ets

2、通过自定义指示器,以下是demo:

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 <= 4; 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) => { 
     // 在页面跟手滑动过程中,逐帧触发该回调。 
     let currentIndicatorInfo = this.getCurrentIndicatorInfo(index,extraInfo) 
     this.currentIndex = currentIndicatorInfo.index 
    }) 
    .onAnimationStart((index: number, targetIndex: number, extraInfo: SwiperAnimationEvent) => { 
     // 切换动画开始时触发该回调。下划线跟着页面一起滑动,同时宽度渐变。 
     this.currentIndex = targetIndex 
    }) 
    .onAnimationEnd((index: number, extraInfo: SwiperAnimationEvent) => { 
     console.info("current offset: " + extraInfo.currentOffset) 
    }) 
    Row() { 
     LazyForEach(this.data, (item: string, index: number) => { 
      Column() 
       .width(55) 
       .height(5) 
       .margin(2) 
       .borderRadius(5) 
       .backgroundColor(this.currentIndex===index?Color.Red:Color.Gray) 
     }, (item: string) => item) 
    } 
    .position({x:0,y:150}) 
   } 
  }.width('100%') 
  .margin({ top: 5 }) 
 } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
分享
微博
QQ
微信
回复
2024-08-28 20:11:07
相关问题
HarmonyOS NFC没有反应
745浏览 • 1回复 待解决
Swiperindicator后续会支持自定义吗
2827浏览 • 1回复 待解决
Swiper组件如何设置导航点位置
3568浏览 • 1回复 待解决
HarmonyOS RN Bottom-tabs组件返回问题
813浏览 • 1回复 待解决
Swiper组件设置不跟随手势滑动
1431浏览 • 1回复 待解决
组件高度为0时,如何设置溢出隐藏
1870浏览 • 1回复 待解决