商品展示筛选页面的嵌套滚动及回滑效果

场景是商品展示、筛选页面,页面的嵌套滚动层级较深,由于有回滑效果,所以不能使用nestedScroll嵌套滚动选项。

HarmonyOS
2024-05-26 11:08:19
994浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
智体美劳全发展

使用的核心API

ScrollList

核心代码解释

@Entry 
@Component 
struct StickyNestedScroll { 
  
  @Styles 
  listCard() { 
    .backgroundColor(Color.White) 
    .height(72) 
    .width("100%") 
    .borderRadius(12) 
  } 
  
  @State textOffset: number = 0 
  @State textMargin: number = 0 
  scroller: Scroller = new Scroller() 
  @State swiperOffset: number = 0 
  @State top: number = 100 
  @State visible: boolean = false 
  @State visible1: Visibility = Visibility.Visible 
  @State number1: number = 0 
  @State XiDing: boolean = false 
  private swiperController: SwiperController = new SwiperController() 
  private data: MyDataSource = new MyDataSource([]) 
  private data1: MyDataSource = new MyDataSource([]) 
  private scrollerForScroll: Scroller = new Scroller() 
  
  @Builder 
  SwiperItem(item: string) { 
    Column(){ 
      Text(item.toString()) 
        .width('100%') 
        .height("100%") 
        .backgroundColor(0xAFEEEE) 
        .textAlign(TextAlign.Center) 
        .fontSize(30) 
    }.width('100%') 
    .height(80) 
    .margin({ left: 5, right: 5 }) 
    .backgroundColor(Color.White) 
  } 
  
  @Builder 
  SwiperItem1(item: string) { 
    Column(){ 
      Text(item.toString()) 
        .width('100%') 
        .height("100%") 
        .backgroundColor('#fff') 
        .textAlign(TextAlign.Center) 
        .fontSize(30) 
    }.width('100%') 
    .height(50) 
    .margin({ left: 5, right: 5 }) 
    .backgroundColor(Color.White) 
  } 
  
  build() { 
    Column() { 
      TextInput({ placeholder: 'input your word...' }) 
        .width('95%') 
        .height(40) 
        .margin(10) 
      // swiper组件在屏幕中完全不可见时的逻辑 
      if (this.visible) { 
        Swiper(this.swiperController) { 
          LazyForEach(this.data, (item: string) => { 
            this.SwiperItem1(item) 
          }, (item: string) => item) 
        } 
        .displayCount(5) 
        .indicator(false) 
        .autoPlay(false) 
        .cachedCount(2) 
        .index(1) 
        .itemSpace(0) 
        .curve(Curve.Linear) 
      } 
      Scroll(this.scrollerForScroll) { 
        Column() { 
          Swiper(this.swiperController) { 
            LazyForEach(this.data, (item: string) => { 
              this.SwiperItem(item) 
            }, (item: string) => item) 
          } 
          .margin({top: this.swiperOffset}) 
          .displayCount(5) 
          .indicator(false) 
          .autoPlay(false) 
          .cachedCount(2) 
          .index(1) 
          .itemSpace(0) 
          .curve(Curve.Linear) 
          Row({ space: 20 }) { 
            Text('春节不打烊(高度50)') 
          } 
          .visibility(this.visible1) 
          .width('100%') 
          .height(50) 
          .justifyContent(FlexAlign.Center) 
          .backgroundColor(Color.Orange) 
          .border({radius: { topLeft: 20, topRight: 20 }}) 
          Row({ space: 20 }) { 
            Text('男童') 
            Text('女童') 
          } 
          .width('100%') 
          .height(50) 
          .justifyContent(FlexAlign.Center) 
          .backgroundColor(Color.Gray) 
          Row({ space: 20 }) { 
            Text('¥100以下') 
            Text('¥100-¥150') 
            Text('¥150以上') 
          } 
          .width('100%') 
          .height(50) 
          .justifyContent(FlexAlign.Center) 
          Stack({ alignContent: Alignment.Top }) { 
            List({ scroller: this.scroller, space: 10 }) { 
              LazyForEach(this.data1, (item: string, index: number) => { 
                ListItem() { 
                  Text("item" + item) 
                    .fontSize(16) 
                }.listCard() 
                .onAppear(() => { 
                  console.log('新数据', index) 
                }) 
              }, (item: string) => item) 
            }.width("100%") 
            .backgroundColor(Color.Blue) 
            .edgeEffect(EdgeEffect.None) 
            .margin({ top: this.top }) 
            .onReachStart(() => { 
              console.log('start') 
              if (this.swiperOffset === -180) { 
                this.XiDing = false 
              } 
            }) 
            // 黄色区域滑到一半,手指松开后黄色区域全部弹出 
            .onScrollStop(() => { 
              if (this.swiperOffset  > -230 && this.swiperOffset && this.XiDing === true) { 
                this.swiperOffset = -180 
              } 
            }) 
            .onScrollFrameBegin((offset: number, state: ScrollState) => { 
              // console.log(`${offset}`) 
              if (this.swiperOffset < -80) { 
                this.visible = true 
                this.visible1 = Visibility.None 
              } 
              if (this.swiperOffset > -80 && offset < 0) { 
                this.visible = false 
                this.visible1 = Visibility.Visible 
              } 
              if (this.swiperOffset - offset >= 0) { 
                this.swiperOffset = 0 
              } else { 
                this.swiperOffset = this.swiperOffset - offset 
              } 
              // 第一次回滑正常后 this.XiDing 的值变成了false,会导致第二次及以后的回滑效果异常 
              if (this.swiperOffset < -280) { 
                this.swiperOffset = -280 
                this.XiDing = true 
              } 
              // 这里不设 this.swiperOffset < 0 的话,初次进入页面下拉就会出现页面布局异常 
              if (this.swiperOffset > -180 && offset < 0 && this.XiDing === true) { 
                // console.log('不准动!!!') 
                this.swiperOffset = -180 
              } 
              if (this.swiperOffset != -280 && this.swiperOffset != 0 && this.swiperOffset != -180) { 
                return { 
                  offsetRemain: 0 
                } 
              } 
              return { 
                offsetRemain: offset 
              } 
            }) 
            // 上滑需要回滑的部分 
            Column() { 
              Row({ space: 20 }) { 
                Text('销量') 
                Text('价格') 
                Text('筛选') 
              } 
              .width('100%') 
              .height(50) 
              .justifyContent(FlexAlign.Center) 
              .backgroundColor(Color.Yellow) 
              Row({ space: 20 }) { 
                // Text('筛选') 
                // Text('春节不打烊') 
                Text('swiper:' + `${this.swiperOffset}`) 
                Text('XiDing:' + `${this.XiDing}`) 
              } 
              .backgroundColor(Color.Red) 
              .width('100%') 
              .height(50) 
              .justifyContent(FlexAlign.Center) 
            } 
            .offset({ 
              y: this.textOffset 
            }) 
            .margin({top: this.textMargin}) 
          }.width("100%").height('100%') 
        } 
      } 
    } 
    .backgroundColor(Color.Pink) 
  } 
  
  aboutToAppear() { 
    let list: number[] = [] 
    for (let i = 1; i <= 10; i++) { 
      list.push(i); 
    } 
    this.data = new MyDataSource(list) 
    let list1: number[] = [] 
    for (let i = 0; i <= 30; i++) { 
      list1.push(i); 
    } 
    this.data1 = new MyDataSource(list1) 
  } 
} 
  
class MyDataSource implements IDataSource { 
  private list: number[] = [] 
  
  constructor(list: number[]) { 
    this.list = list 
  } 
  
  totalCount(): number { 
    return this.list.length 
  } 
  
  getData(index: number): number { 
    return this.list[index] 
  } 
  
  registerDataChangeListener(listener: DataChangeListener): void { 
  } 
  
  unregisterDataChangeListener() { 
  } 
}
  • 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.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.

实现效果

适配版本信息

SDK:4.1.5.6

IDE:DevEco Studio 4.1.1.500

分享
微博
QQ
微信
回复
2024-05-27 10:52:58
相关问题
商品详情页面的常规布局方式
1243浏览 • 1回复 待解决
HarmonyOS 需要一个筛选页面的demo
1125浏览 • 1回复 待解决
页面和列表嵌套滚动,实现列表吸顶
2300浏览 • 1回复 待解决
HarmonyOS 嵌套滚动冲突
893浏览 • 1回复 待解决
如何实现嵌套滚动技术
2125浏览 • 1回复 待解决
基于webView的嵌套滚动
1049浏览 • 1回复 待解决
HarmonyOS 返回返回无法拦截事件
1357浏览 • 1回复 待解决
HarmonyOS Web嵌套滚动体验差
827浏览 • 1回复 待解决