HarmonyOS 横向列表条目自动无限循环滚动?

向列表条目自动无限循环滚动,比如数组5条数据,怎么实现无限循环滚动

我用Scroll来实现,数据做不到无限循环,有没有什么控件可以实现数据无线循环滚动效果

ListItem() {

  Column() {
    Row() {
      Scroll(this.scrollerColumn) {
        Row({ space: 5 }) {
          ForEach(item.subSectionList, (item: SubSection) => {
            Text(item.subsectionname)
              .fontSize(14)
              .fontColor(Color.Red)
              .border({
                style: BorderStyle.Solid,
                width: 1,
                color: Color.Red,
                radius: 10
              })
              .padding({
                left: 5,
                right: 5,
                top: 3,
                bottom: 3
              })
              .margin({ left: 10 })
              .onClick(() => {
                router.pushUrl({
                  url: 'pages/newsdetail/NewsDetailPage',
                  params: {
                    id: item.subsectionid,
                  }
                }, router.RouterMode.Single)
              })
          })

        }
      }.scrollable(ScrollDirection.Horizontal) //设置滚动方向
      .scrollBar(BarState.Off)
      .width('100%')

    }
  }.backgroundColor('#F6F6F6')
  .padding({ top: 15, bottom: 15 })

}
阅读 495
1 个回答

参考示例如下:

class Item {
  img: ResourceStr = ''
  title?: string = ''
}

@Entry
@Component
struct Index {
  index: number = 0
  @State NoteLists: Array<Item> = [{ img: $r('app.media.app_icon'), title: '头像1号' },
    { img: $r('app.media.app_icon'), title: '头像2号' }, { img: $r('app.media.app_icon'), title: '头像3号' },
    { img: $r('app.media.app_icon'), title: '头像4号' }, { img: $r('app.media.app_icon'), title: '头像5号' },
    { img: $r('app.media.app_icon'), title: '头像6号' }]
  scroller: Scroller = new Scroller()

  build() {
    Row() {
      Column() {
        Scroll(this.scroller) {
          Row() {
            ForEach(this.NoteLists, (item: Item) => {
              Column() {
                Image(item.img).width(50).height(50)
                Text(item.title)
              }
              .width(80).height(80).justifyContent(FlexAlign.SpaceBetween)
            })
          }
        }.width('100%')
        .scrollable(ScrollDirection.Horizontal).scrollBar(BarState.Off)
      }.width('100%')
    }.height('100%')
  }

  onPageShow() {
    setInterval(() => {
      // 点击后滑动到指定位置,即下滑100.0vp的距离
      if (this.index == 6) {
        this.index = 0
      }
      console.log(""index is:"" + this.index)
      let currentIndex: number = this.index;
      let xOffset: number = this.scroller.currentOffset().xOffset;
      this.NoteLists.push(this.NoteLists[currentIndex]);
      this.index = currentIndex + 1;
      this.scroller.scrollTo({ xOffset: xOffset + 20, yOffset: 0 })
    }, 100)
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进