HarmonyOS 如何实现list组件item滑动到中间高亮显示

HarmonyOS
2024-12-24 18:00:34
775浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

可参考以下示例:

@Entry
@Component
struct ListExample {
  private arr: number[] = []
  private scrollerForList: Scroller = new Scroller()

  @State color: number = Color.White
  @State centerIndex : number = 0
  aboutToAppear() {
    for (let i = 0; i < 10; i++) {
      this.arr.push(i)
    }
  }

  build() {
    Column() {
      Row() {
        List({ space: 20, initialIndex: 0, scroller: this.scrollerForList }) {
          ForEach(this.arr, (item: number) => {
            ListItem() {
              Text('' + item)
                .width('100%').height(100).fontSize(16)
                .textAlign(TextAlign.Center).backgroundColor(item===this.centerIndex?Color.Pink:Color.White)
            }
            .borderRadius(10)
            .width('40%')
            .height('80%')

          }, (item: number) => JSON.stringify(item))
        }
        .chainAnimation(true)
        .edgeEffect(EdgeEffect.Spring)
        .listDirection(Axis.Horizontal)
        .height('100%')
        .width('100%')
        .scrollSnapAlign(ScrollSnapAlign.CENTER)
        .borderRadius(10)
        .backgroundColor(0xDCDCDC)
        .onScrollIndex((firstIndex: number, lastIndex: number, centerIndex: number) => {
          console.info('first' + firstIndex)
          console.info('last' + lastIndex)
          console.info('center' + centerIndex)
          this.centerIndex = centerIndex
        })
      }
      .width('100%')
      .height('100%')
      .backgroundColor(0xDCDCDC)
      .padding({ top: 10 })
    }
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-24 20:26:18


相关问题
HarmonyOS List组件指定item刷新实现方案
911浏览 • 1回复 待解决
HarmonyOS 组件List如何禁止滑动
1508浏览 • 1回复 待解决
HarmonyOS List组件默认滚动到最底部
1194浏览 • 1回复 待解决
list组件无法滚动到底部
2308浏览 • 1回复 待解决
HarmonyOS 监听List组件滑动
983浏览 • 1回复 待解决
HarmonyOS list组件如何设置匀速滑动
573浏览 • 1回复 待解决