HarmonyOS ArkUI的list组件的ListItemGroup分组中内容切换后如何置顶

代码:

List({ scroller: this.scrollers }) {
  ListItemGroup()//其他内容
  ListItemGroup({ header: this.itemHead2() }) {
    ListItem() {
      shopIndexGoodsList({ pxZvp: this.pxZvp, otherGoodsList: this.otherGoodsList, })//商品列表的自定义组件
    }
  }
}
@Builder
itemHead2() {
  Row() {
    text("1").onClick(()=>{....})
    text('2').onClick(()=>{....})
    text("3").onClick(()=>{....})
  }.width('100%').height(80 * this.pxZvp).backgroundColor('#fff')
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

itemHead2() 是一个点击切换的title,处于界面滚动到一定距离后保持吸顶状态,点击不同的text下边的shopIndexGoodsList组件展示不同的商品列表,因为商品列表是可以滑动的,当我在title==1的时候滑动到一定距离后,切换的title==2,shopIndexGoodsList的位置还是在之前title==1时滑动到的位置上。

我想知道如何实现在切换时丝滑的让shopIndexGoodsList滚动回到顶部,itemHead2()依然保持吸顶状态,有专门的api吗?如何使用

HarmonyOS
2025-01-09 14:12:11
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

参考demo:

enum ScrollPosition {
  start,
  center,
  end
}

@Entry
@Component
struct NestedScroll {
  @State listPosition: number = ScrollPosition.start; // 0代表滚动到List顶部,1代表中间值,2代表滚动到List底部。
  @State scrollPosition: number = ScrollPosition.start; // 0代表滚动到页面顶部,1代表中间值,2代表滚动到页面底部。
  @State showTitle: boolean = false;
  @State currentYOffset: number = 0;
  private arr: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  private scrollerForScroll: Scroller = new Scroller();
  private scrollerForList: Scroller = new Scroller();
  controller: TextInputController = new TextInputController()

  build() {
    Stack({ alignContent: Alignment.Top }) {
      Scroll(this.scrollerForScroll) {
        Column() {
          // Image($r('app.media.startIcon'))
          // .width("100%")
          // .height("40%")
          Column(){

          }
          .width("100%")
          .height("40%")
          .backgroundColor(Color.Pink)

          Tabs({ barPosition: BarPosition.Start }) {
            TabContent() {
              List({ space: 10, scroller: this.scrollerForList }) {
                ForEach(this.arr, (item: number) => {
                  ListItem() {
                    Text("促销商品" + item)
                      .width("100%")
                      .height("100%")
                      .borderRadius(15)
                      .fontSize(24)
                      .textAlign(TextAlign.Center)
                      .backgroundColor(Color.White)
                  }.width("100%").height(100)
                }, (item: string) => item)
              }

              .padding({ left: 10, right: 10 })
              .width("100%")
              .edgeEffect(EdgeEffect.None)
              .scrollBar(BarState.Off)
              .onReachStart(() => {
                this.listPosition = ScrollPosition.start
              })
              .onReachEnd(() => {
                this.listPosition = ScrollPosition.end
              })
              .onScrollFrameBegin((offset: number, state: ScrollState) => {
                // 滑动到列表中间时
                if (!((this.listPosition == ScrollPosition.start && offset < 0)
                  || (this.listPosition == ScrollPosition.end && offset > 0))) {
                  this.listPosition = ScrollPosition.center
                }

                // 如果页面已滚动到底部,列表不在顶部或列表有正向偏移量
                if (this.scrollPosition == ScrollPosition.end
                  && (this.listPosition != ScrollPosition.start || offset > 0)) {
                  return { offsetRemain: offset };
                } else {
                  this.scrollerForScroll.scrollBy(0, offset)
                  return { offsetRemain: 0 };
                }
              })
            }.tabBar('促销活动')

            TabContent() {
              List({ space: 10, scroller: this.scrollerForList }) {
                ForEach(this.arr, (item: number) => {
                  ListItem() {
                    Text("行程安排" + item)
                      .width("100%")
                      .height("100%")
                      .borderRadius(15)
                      .fontSize(24)
                      .textAlign(TextAlign.Center)
                      .backgroundColor(Color.White)
                  }.width("100%").height(100)
                }, (item: string) => item)
              }
              .padding({ left: 10, right: 10 })
              .width("100%")
              .edgeEffect(EdgeEffect.None)
              .scrollBar(BarState.Off)
              .onReachStart(() => {
                this.listPosition = ScrollPosition.start
              })
              .onReachEnd(() => {
                this.listPosition = ScrollPosition.end
              })
              .onScrollFrameBegin((offset: number, state: ScrollState) => {
                // 滑动到列表中间时
                if (!((this.listPosition == ScrollPosition.start && offset < 0)
                  || (this.listPosition == ScrollPosition.end && offset > 0))) {
                  this.listPosition = ScrollPosition.center
                }

                // 如果页面已滚动到底部,列表不在顶部或列表有正向偏移量
                if (this.scrollPosition == ScrollPosition.end
                  && (this.listPosition != ScrollPosition.start || offset > 0)) {
                  return { offsetRemain: offset };
                } else {
                  this.scrollerForScroll.scrollBy(0, offset)
                  return { offsetRemain: 0 };
                }
              })
            }.tabBar('行程服务')
          }

          .vertical(false)
          .barMode(BarMode.Fixed)
          .barWidth(360)
          .barHeight(56)
          .width("100%")
          .height("92%")
          .backgroundColor('#F1F3F5')
        }
      }
      .scrollBar(BarState.Off)
      .width("100%")
      .height("100%")
      .onScroll((xOffset: number, yOffset: number) => {
        this.currentYOffset = this.scrollerForScroll.currentOffset().yOffset;
        // 非(页面在顶部或页面在底部),则页面在中间
        if (!((this.scrollPosition == ScrollPosition.start && yOffset < 0)
          || (this.scrollPosition == ScrollPosition.end && yOffset > 0))) {
          this.scrollPosition = ScrollPosition.center
        }
      })
      .onScrollEdge((side: Edge) => {
        if (side == Edge.Top) {
          // 页面在顶部
          this.scrollPosition = ScrollPosition.start
        } else if (side == Edge.Bottom) {
          // 页面在底部
          this.scrollPosition = ScrollPosition.end
        }
      })
      .onScrollFrameBegin(offset => {
        if (this.scrollPosition == ScrollPosition.end) {
          return { offsetRemain: 0 };
        } else {
          return { offsetRemain: offset };
        }
      })

      if (this.currentYOffset+50 >= 0) {
        Row() {
          TextInput({ text: 'xxxxxxxx', placeholder: 'input your word...', controller: this.controller }).fontSize(24)
        }
        .justifyContent(FlexAlign.Center)
        .backgroundColor('#00ffffff')
        .width('100%')
        .height('8%')

        // .opacity(this.currentYOffset / 50 > 1 ? 1 : this.currentYOffset / 50)
      }
    }
    .width('100%')
    .height('100%')
    .backgroundColor(0xDCDCDC)
  }
}
  • 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.
分享
微博
QQ
微信
回复
2025-01-09 17:27:31


相关问题
ArkUI 如何设置组件悬停状态?
2447浏览 • 1回复 待解决