HarmonyOS scroll嵌套组件,如何实现子组件置顶

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

scroll暂时没有原生吸顶api,可以参考以下两种方案

1、使用list的sticky属性,配合listItem实现

参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-list-V5#sticky9

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-listitem-V5

2、使用偏移量属性自行实现吸顶,参考代码如下:

enum ScrollPosition {
  start,
  center,
  end
}

class ItemClass {
  content: string = '';
  color: Color = Color.White;
}

@Entry
@Component
struct NestedScrollDemo {
  @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: ItemClass[] = [];
  private colorArr: Color[] = [Color.White, Color.Blue, Color.Brown, Color.Green, Color.Gray];
  private scrollerForScroll: Scroller = new Scroller();
  private scrollerForList: Scroller = new Scroller();
  private scrollerForTitle: Scroller = new Scroller();
  @State currentIndex: number = 0;

  aboutToAppear() {
    for (let i = 0; i < 6; i++) {
      let data: ItemClass = {
        content: i.toString(),
        color: this.colorArr[i % 5]
      }
      this.arr.push(data);
    }
  }

  @Builder
  myBuilder() {
    Row() {
      List({ space: 2, initialIndex: 0, scroller: this.scrollerForTitle }) {
        ForEach(this.arr, (item: ItemClass, index) => {
          ListItem() {
            Column() {
              Text(item.content);
              Divider()
                .color('#000000')
                .strokeWidth(5)
                .visibility(index == this.currentIndex ? Visibility.Visible : Visibility.Hidden)
            }
            .width('25%')
            .height(50)
            .onClick(() => {
              this.scrollerForList.scrollToIndex(index)
              this.scrollerForScroll.scrollEdge(Edge.Bottom)
            })
          }
        })
      }
      .listDirection(Axis.Horizontal)
      .scrollBar(BarState.Off)
    }
    .backgroundColor('#ffe2d0d0')
    .alignItems(VerticalAlign.Center)

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

          List({ space: 10, scroller: this.scrollerForList }) {
            ForEach(this.arr, (item: ItemClass, index) => {
              ListItem() {
                Column() {
                  Text(item.content)
                  //添加其他内容
                }
                .width('100%')
                .height(500)
                .backgroundColor(item.color)
              }.width("100%").height(500)
              .onVisibleAreaChange([0.8], (isVisible) => {
                if (isVisible) {
                  this.currentIndex = index;
                  this.scrollerForTitle.scrollToIndex(this.currentIndex);
                }
              })
            }, (item: ItemClass) => item.content)
          }
          .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 };
            }
          })
          .width("100%")
          .height("calc(100% - 50vp)")


          .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 };
        }
      })
    }
    .width('100%')
    .height('100%')
    .backgroundColor(0xDCDCDC)
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
嵌套组件Scroll不生效
2187浏览 • 1回复 待解决
Scroll组件的展示位置如何调整
2539浏览 • 1回复 待解决
HarmonyOS 首页有滑动置顶组件吗?
21浏览 • 1回复 待解决
HarmonyOS Scroll嵌套RelativeContainer 问题
356浏览 • 1回复 待解决
HarmonyOS Scroll 嵌套 RelativeContainer 问题
522浏览 • 1回复 待解决
HarmonyOS scroll组件问题
80浏览 • 1回复 待解决
HarmonyOS Scroll嵌套web手势冲突
14浏览 • 1回复 待解决
HarmonyOS Tabs组件嵌套Tabs组件问题
849浏览 • 1回复 待解决
HarmonyOS Scroll组件无法滑动
474浏览 • 1回复 待解决
HarmonyOS Scroll组件使用问题
446浏览 • 1回复 待解决