HarmonyOS List如何实现滑动且中间部分高亮显示

HarmonyOS
2024-12-25 13:36:12
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

示例参考如下:

@Entry
@Component
struct ListPage {
  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() {
      Button('centerIndex = 5').onClick(() => {
        this.scrollerForList.scrollToIndex(5, true, ScrollAlign.CENTER)
        console.log(`${this.centerIndex}`)
      })
      Button('centerIndex = 7').onClick(() => {
        this.scrollerForList.scrollToIndex(7, true, ScrollAlign.CENTER)
        console.log(`${this.centerIndex}`)
      })
      Button('centerIndex = 9').onClick(() => {
        this.scrollerForList.scrollToIndex(9, true, ScrollAlign.CENTER)
        console.log(`${this.centerIndex}`)
      })
      Button('centerIndex = 3').onClick(() => {
        this.scrollerForList.scrollToIndex(3, true, ScrollAlign.CENTER)
        console.log(`${this.centerIndex}`)
      })
      Button('centerIndex = 0').onClick(() => {
        this.scrollerForList.scrollToIndex(0, true, ScrollAlign.CENTER)
        console.log(`${this.centerIndex}`)
      })
      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.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
分享
微博
QQ
微信
回复
2024-12-25 15:27:08
相关问题
如何实现纵向逆向滑动的Slider?
1024浏览 • 1回复 待解决
HarmonyOS Text部分文字高亮设置
898浏览 • 1回复 待解决
HarmonyOS 部分文本高亮解决方案
1476浏览 • 1回复 待解决
Text匹配文字高亮显示
1417浏览 • 1回复 待解决
HarmonyOS 怎么高亮显示搜索的文字
921浏览 • 1回复 待解决
如何识别文本中的邮箱并高亮显示
1113浏览 • 1回复 待解决
HarmonyOS Textinput如何实现中间插入
1018浏览 • 1回复 待解决
HarmonyOS 组件List如何禁止滑动
1463浏览 • 1回复 待解决
HarmonyOS list无法滑动
502浏览 • 1回复 待解决
HarmonyOS List联动滑动
585浏览 • 1回复 待解决
HarmonyOS如何拦截list滑动事件?
1142浏览 • 1回复 待解决
如何屏蔽List滑动事件
3025浏览 • 1回复 待解决
HarmonyOS 监听List组件滑动
957浏览 • 1回复 待解决
HarmonyOS list滑动问题
1466浏览 • 1回复 待解决