HarmonyOS list组件点击后,滚动居中

HarmonyOS
2024-12-25 08:12:07
1145浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

参考下这个demo,使用scrollToIndex属性,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-scroll-V5#scrolltoindex

@Entry
@Component
struct CityList {
  private listScroller: Scroller = new Scroller();
  @State focusIndex: number = 0
  @State allListString: string[] = ["111", "222", "333", "444", "555", "666", "777", "888", "999"]

  build() {
    List({ scroller: this.listScroller }) {
      ForEach(this.allListString, (item: string, index: number) => {
        ListItem() {
          Text(item).onClick(() => {
            this.listScroller.scrollToIndex(index, true, ScrollAlign.CENTER)
          }).width(80)
            .textAlign(TextAlign.Center)
        }
      }, (item: string) => item)
    }
    .scrollBar(BarState.Off)
    .listDirection(Axis.Horizontal)
    .backgroundColor("#FFF1F3")
    .alignListItem(ListItemAlign.Center)
    .height(60)
    .margin({ top: 100 })
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-25 09:53:28


相关问题
HarmonyOS List组件滚动监听
851浏览 • 1回复 待解决
如何获取List组件滚动滚动的距离
3494浏览 • 1回复 待解决
HarmonyOS List组件默认滚动到最底部
1204浏览 • 1回复 待解决
list组件无法滚动到底部
2330浏览 • 1回复 待解决
HarmonyOS 自动横向滚动List
766浏览 • 1回复 待解决
HarmonyOS scroll和list滚动冲突
1133浏览 • 1回复 待解决
list 支持循环滚动吗?
2871浏览 • 1回复 待解决
HarmonyOS Row组件怎么居中
578浏览 • 1回复 待解决
HarmonyOS List页面如何主动停止滚动
833浏览 • 1回复 待解决