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')
}

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)
  }
}
分享
微博
QQ
微信
回复
2025-01-09 17:27:31
相关问题
ArkUI 如何设置组件悬停状态?
2072浏览 • 1回复 待解决