HarmonyOS 置顶粘贴效果除了list的sticky 还有别的方案吗?

HarmonyOS  置顶粘贴效果除了list的sticky 还有别的方案吗?

HarmonyOS
2024-09-26 11:50:24
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

还可以使用偏移量通过代码逻辑来实现,以下是简单的demo:

enum ScrollPosition {  
  start,  
  center,  
  end  
}  
  
class ItemClass {  
  content: string = '';  
  color: Color = Color.White;  
}  
  
@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: 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)")
分享
微博
QQ
微信
回复
2024-09-26 17:08:28
相关问题
HarmonyOS listitem支持托动效果
178浏览 • 1回复 待解决
HarmonyOS list编辑移动效果
155浏览 • 1回复 待解决
HarmonyOS 效果实现方案
319浏览 • 1回复 待解决
HarmonyOS list中item交互效果处理
267浏览 • 1回复 待解决
如何实现list折叠动画效果
399浏览 • 1回复 待解决
如何将List回弹效果改为阴影效果
363浏览 • 1回复 待解决