HarmonyOS 滑动问题

操作步骤:

1、在其他页面跳转全部应用页面传一个标题

2、如果标题不为空,需要滑动到标题的位置

3、这个this.bgScroller.scrollTo({ xOffset: 0, yOffset: -100 });没有效果

HarmonyOS
2024-12-20 16:45:59
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

可以使用组件的scrollToIndex 方法 ,跳转到指定位置,参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-scroll-V5#scrolltoindex可参考demo:

@Entry
@Component
struct CategoryPage3 {
  @State categoryTabIndex: number = 1;
  @State scrollerIndex: number = 1;
  @State sideClickFlag: boolean = false;
  @State tabTitle:string[] = ['页签1','页签2','页签3','页签4','页签5'];
  scroller: Scroller = new Scroller();
  build() {
    Column() {
      Tabs({
        index: this.categoryTabIndex,
        barPosition: BarPosition.End
      })
      {
        ForEach(this.tabTitle, (item: string, index?: number) => {
          TabContent() {
            GridRow({
              columns: 4
            }) {
              GridCol() {
                SideListComponent({
                  sideListIndex: $scrollerIndex,
                  sideClickFlag: $sideClickFlag,
                  scroller: this.scroller = new Scroller()
                })
              }
              GridCol({
                span: 3
              }) {
                DetailListComponent({
                  sideListIndex: $scrollerIndex,
                  sideClickFlag: $sideClickFlag,
                  scroller: this.scroller
                })
                  .margin( { top: "29vp" })
              }
            }
            .margin({
              left: "24vp",
              right: "24vp"
            })
          }
          .tabBar(this.TabBottom(item, index))
        }, (item: string, index?: number) => index + JSON.stringify(item))
      }
      .layoutWeight(1)
      .barWidth('100%')
      .barHeight("56vp")
      .barMode(BarMode.Fixed)
      .onChange((index: number) => {
        this.categoryTabIndex = index;
      })
    }
    .backgroundColor("#F1F3F5")
  }
  @Builder TabBottom(item: string, index?: number) {
    Column() {
      Text(item)
        .width('100%')
        .height("14vp")
        .fontSize("10fp")
        .fontWeight(500)
        .fontColor(this.categoryTabIndex === index ? "#007DFF" : "#99182431")
        .textAlign(TextAlign.Center)
        .margin({ bottom: "7vp" })
    }
    .justifyContent(FlexAlign.Center)
    .height('100%')
    .width('100%')
  }
}
@Component
export struct DetailListComponent {
  scroller: Scroller = new Scroller();
  // @Prop detailListBreakpoint: string = '';
  @State subTitle:string[] = ['分类1子标题1','分类2子标题2','分类3子标题3','分类4子标题4','分类5子标题5','分类6子标题6'];
  @Link sideListIndex: number;
  @Link sideClickFlag: boolean;
  build() {
    List({ scroller: this.scroller }) {
      ForEach(this.subTitle, (item: string) => {
        ListItem() {
          Column() {
            Text(item)
              .height("22vp")
              .fontSize("16fp")
              .fontColor("#FF182431")
              .fontWeight(500)
              .align(Alignment.Start)
            Text('内容')
              .width('100%')
              .height("72vp")
              .textAlign(TextAlign.Center)
              .fontSize("14fp")
              .fontWeight(500)
              .align(Alignment.Center)
              .borderRadius("16vp")
              .backgroundColor("#0C000000")
              .margin({top:10})
            Text('内容')
              .width('100%')
              .height("72vp")
              .textAlign(TextAlign.Center)
              .fontSize("14fp")
              .fontWeight(500)
              .align(Alignment.Center)
              .borderRadius("16vp")
              .backgroundColor("#0C000000")
              .margin({top:10})
            Text('内容')
              .width('100%')
              .height("72vp")
              .textAlign(TextAlign.Center)
              .fontSize("14fp")
              .fontWeight(500)
              .align(Alignment.Center)
              .borderRadius("16vp")
              .backgroundColor("#0C000000")
              .margin({top:10})
          }
          .height(300)
          .alignItems(HorizontalAlign.Start)
        }
        .height(300)
      }, (item: string, index?: number) => index + JSON.stringify(item))
    }
    .width('100%')
    .onScrollIndex((firstIndex: number) => {
      if(!this.sideClickFlag) {
        this.sideListIndex = firstIndex;
      }
      this.sideClickFlag = false;
      this.scroller.scrollToIndex(this.sideListIndex);
    })
  }
}
 
@Component
export struct SideListComponent {
  scroller: Scroller = new Scroller();
  @Link sideListIndex: number;
  @Link sideClickFlag: boolean;
  @State listData:string[] = ['分类1','分类2','分类3','分类4','分类5','分类6'];

  build() {
    Flex({ justifyContent: FlexAlign.SpaceBetween }) {
      List() {
        ForEach(this.listData, (item: string, index?: number) => {
          ListItem() {
            Text(item)
              .fontSize("14fp")
              .fontWeight('400')
              .fontColor(this.sideListIndex === index ? '#007DFF' : '#182431')
              .textAlign(TextAlign.Start)
              .margin({
                top: '12',
                bottom: '16',
                left: '0'
              })
              .width('100%')
              .onClick(() => {
                this.sideClickFlag = true;
                if (index != undefined) {
                  this.sideListIndex = index;
                }
                this.scroller.scrollToIndex(this.sideListIndex);
              })
          }.animation({
            duration: 1200,
            curve: Curve.Friction,
            delay: 500,
            iterations: -1, // 设置-1表示动画无限循环
            playMode: PlayMode.Alternate,
            expectedFrameRateRange: {
              min: 20,
              max: 120,
              expected: 90,
            }
          })
        })
      }

      .constraintSize({
        minWidth: (100)
      })
      .width('100%')
      .height('100%')
      .margin({ top: '12vp' })

      Divider()
        .vertical(true)
        .color("#0C000000")
        .strokeWidth(1)
        .margin({ bottom: '8vp' })
    }
  }}
  • 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.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
分享
微博
QQ
微信
回复
2024-12-20 18:49:32
相关问题
HarmonyOS 嵌套滑动问题
1082浏览 • 1回复 待解决
HarmonyOS scroll滑动问题
1115浏览 • 1回复 待解决
HarmonyOS Slider滑动问题
1240浏览 • 1回复 待解决
HarmonyOS list滑动问题
1478浏览 • 1回复 待解决
HarmonyOS onToch事件滑动问题
634浏览 • 1回复 待解决
HarmonyOS Scroll嵌套List的滑动问题
779浏览 • 1回复 待解决
TabContent内web组件滑动问题
799浏览 • 2回复 待解决
HarmonyOS Refresh跟list组件惯性滑动问题
849浏览 • 1回复 待解决
HarmonyOS Tabs和Web嵌套左右滑动问题
903浏览 • 1回复 待解决
仿射变换后列表滑动问题
974浏览 • 1回复 待解决
HarmonyOS scroll滚动问题
700浏览 • 1回复 待解决
HarmonyOS bindSheet拖动问题
493浏览 • 1回复 待解决
HarmonyOS LongPressGesture手势移动问题
1066浏览 • 1回复 待解决
HarmonyOS 悬浮按钮拖动问题
1281浏览 • 1回复 待解决
HarmonyOS Scroll组件滚动问题
1426浏览 • 1回复 待解决
HarmonyOS Grid组件子项拖动问题
1008浏览 • 1回复 待解决
HarmonyOS TextPickerDialog多级联动问题
619浏览 • 1回复 待解决
HarmonyOS 图片放大后的滚动联动问题
658浏览 • 1回复 待解决
HarmonyOS grid里面的item支持拖动问题
690浏览 • 1回复 待解决