scroll和list的嵌套滑动

scroll和list的嵌套滑动

HarmonyOS
2024-05-26 12:32:02
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
sunshine_2000

本文主要介绍关于Scroll嵌套List进行上拉加载,以及向下滑动过后,头部会有固定的区域,其他区域正常滑动。

使用的核心API

核心代码解释

1、 使用Scroll,List,容器组件,让页面能够滑动起来。

2、 设置顶部区域让滑动区域 到多少 固定起来。

3、 通过上拉加载刷新进行数据加载。

​@Entry 
@Component 
struct WeiboPersonPage { 
  @State currentTitle: string = '顶部模块' 
  @State data: Array<string> = []; 
  @State data2: Array<string> = []; 
  mainScroller: Scroller = new Scroller(); 
  tags: Array<string> = ['标签1', '标签2', '标签3', '标签4']; 
  tagScrollers: Array<Scroller> = []; //每个scroller对应一个标签 
  @State hideNavBar: boolean = true; 
  @State hideLoadingView: boolean = true; 
  @State mainListCanScroll: boolean = true; 
  @State private currentPageHeight: number = 0; 
  @State private currentPageWidth: number = 0; 
  @State private progress: number = 0; //0-10 
  @State isRefreshing: boolean = false; 
  private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Up | PanDirection.Down }) 
  @State isLoadingFromBottom: boolean = false; 
 
  aboutToAppear() { 
    for (let i = 0; i < this.tags.length; i++) { 
      let tagScroller = new Scroller(); 
      this.tagScrollers.push(tagScroller); 
    } 
    for (let i = 0; i < 2; i++) { 
      this.data.push(`自定义header内容`) 
    } 
    for (let i = 0; i < 50; i++) { 
      this.data2.push(`Hello ${i}`) 
    } 
  } 
 
  build() { 
    Column() { 
      Navigation() { 
        Stack({ alignContent: Alignment.TopStart }) { 
          this.mainListView(this.mainScroller, this.tagScrollers) 
          Progress({ value: this.progress, total: 10, type: ProgressType.Ring }) 
            .width(20) 
            .color(Color.Orange) 
            .style({ 
              strokeWidth: 20, 
              status: this.isRefreshing == true ? ProgressStatus.LOADING : ProgressStatus.PROGRESSING, 
              enableSmoothEffect: true, 
              enableScanEffect: true 
            }) 
            .backgroundColor(Color.Yellow) 
            .visibility((this.hideLoadingView == false || this.isRefreshing) ? Visibility.Visible : Visibility.Hidden) 
            .margin({ left: this.currentPageWidth - 23 - 20 }) 
          //1.下拉超过80,则进入刷新状态 
          Button({ type: ButtonType.Normal, stateEffect: true }) { 
            Image($r('app.media.icon')).width(40).height(40) 
          } 
          .margin({ left: 23 }) 
          .backgroundColor(Color.Blue) 
          .onClick(() => { 
          }) 
          .visibility(this.hideNavBar == false ? Visibility.Hidden : Visibility.Visible) 
        } 
      } 
      .backgroundColor(Color.White) 
      .title(this.NavigationTitle) 
      .margin({ 
        top: $r('app.media.icon') 
      }) 
      .width('100%') 
      .titleMode(NavigationTitleMode.Mini) 
      .hideBackButton(false) 
      .zIndex(1) 
      .mode(NavigationMode.Auto) 
      .hideTitleBar(this.hideNavBar) 
      .parallelGesture( 
        PanGesture(this.panOption) 
          .onActionStart((event?: GestureEvent) => { 
          }) 
          .onActionUpdate((event?: GestureEvent) => { 
          }) 
          .onActionEnd(() => { 
            if (this.progress >= 10) { 
              //并且手指离开,执行刷新 
              this.isRefreshing = true; 
              //正在刷新的时候 
              //需要一直显示 
              console.info('开始刷新========1') 
              //在这里进行回调函数,执行若干秒 
              //举例,执行若干秒 
              setTimeout(() => { 
                this.isRefreshing = false; 
                this.data2.unshift('1111'); 
              }, 2000); 
            } 
            else { 
              this.isRefreshing = false; 
              console.info('取消刷新========1') 
            } 
          }) 
      ) 
    } 
    .width('100%') 
    .backgroundColor($r('app.media.icon')) 
    .height('100%') 
    .onAreaChange((oldValue: Area, newValue: Area) => { 
      console.info('onAreaChange:' + newValue.height.toString()); 
      this.currentPageHeight = Math.round(newValue.height as number); 
      this.currentPageWidth = Math.round(newValue.width as number); 
    }) 
  } 
 
  //自定义导航 
  @Builder 
  NavigationTitle() { 
    Column() { 
      Text(this.currentTitle) 
        .fontColor('#182431') 
        .fontSize(18) 
        .fontWeight(600) 
        .backgroundColor(Color.Yellow) 
        .height(56) 
    } 
    .width('67%') 
    .height(56) 
    .backgroundColor(Color.Grey) 
  } 
 
  @Builder 
  listView(scroller: Scroller) { 
    List({ space: 0, scroller: scroller }) { 
      ForEach(this.data2, (item: string, index?: number) => { 
        ListItem() { 
          if (this.isLoadingFromBottom) { 
            if (index == this.data2.length - 1) { 
              Row() { 
                Text('加载中...').fontSize(50) 
                Progress({ value: 0, total: 20, type: ProgressType.Ring }) 
                  .width(20) 
                  .color(Color.Orange) 
                  .style({ 
                    strokeWidth: 20, status: ProgressStatus.LOADING, 
                    enableSmoothEffect: true, enableScanEffect: true 
                  }) 
                  .backgroundColor(Color.Red) 
              }.margin({ left: 10, right: 10 }) 
            } 
            else { 
              Row() { 
                Text(item).fontSize(50).width('100%') 
              }.margin({ left: 10, right: 10 }) 
            } 
          } 
          else { 
            Row() { 
              Text(item).fontSize(50).width('100%') 
            }.margin({ left: 10, right: 10 }) 
          } 
        } 
        .backgroundColor(Color.White) 
      }, (item: string) => item) 
    } 
    .cachedCount(5) 
    .divider({ strokeWidth: 1, color: 0x222222 }) 
    // 必须设置列表为滑动到边缘无效果 
    .edgeEffect(EdgeEffect.None) 
    .enableScrollInteraction(this.mainListCanScroll ? false : true) 
    .onScroll((scrollOffset: number, scrollState: ScrollState) => { 
      //如果下侧列表划到了最上侧,则主list可以向下滑动,不可以向上滑动; 
      //如果下侧列表划到了最上侧,非主list可以向上滑动,不可以向下滑动 
      if (scroller.currentOffset().yOffset == 0) { 
        this.mainListCanScroll = true 
      } 
      else { 
        this.mainListCanScroll = false 
      } 
    }) 
    .onReachEnd(() => { 
      //每次只允许执行一次,执行完后在执行下一个 
      if (this.isLoadingFromBottom == true) { 
        return; 
      } 
      //当划到末尾的时候,加一行数据,用来显示加载效果 
      this.isLoadingFromBottom = true; 
      this.data2.push('占位数据'); 
      //请求操作 
      //举例是一个延时操作 
      setTimeout(() => { 
        console.info('加载结束=====1') 
        this.isLoadingFromBottom = false; 
        this.data2.pop(); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
        this.data2.push('新的数据' + this.data2.length); 
      }, 3000); 
    }) 
  } 
 
  @Builder 
  mainListView(mainScroller: Scroller, tagScrollers: Array<Scroller>) { 
    List({ space: 0, scroller: mainScroller }) { 
      ForEach(this.data, (item: string, index?: number) => { 
        if (index == 0) { 
          ListItem() { 
            Row() { 
              Text(item).fontSize(50).width('100%') 
            }.margin({ left: 10, right: 10 }) 
          } 
          .height(400) // 
          .backgroundColor(Color.Red) 
        } 
        else { 
          ListItem() { 
            Tabs({ barPosition: BarPosition.Start }) { 
              TabContent() { 
                this.listView(tagScrollers[0]) 
              } 
              .tabBar('首页') 
 
              TabContent() { 
                this.listView(tagScrollers[1]) 
              } 
              .tabBar('推荐') 
 
              TabContent() { 
                this.listView(tagScrollers[2]) 
              } 
              .tabBar('发现') 
 
              TabContent() { 
                this.listView(tagScrollers[3]) 
              } 
              .tabBar("我的") 
            } 
          } 
          .height(this.currentPageHeight - 56 - 40) //这里要设置一个全屏减去导航栏56减去标签栏40的高度 
          .backgroundColor(Color.Green) 
        } 
      }, (item: string) => item) 
    } 
    .cachedCount(2) 
    .backgroundColor(Color.White) 
    .divider({ 
      strokeWidth: 1, 
      startMargin: '3.5%', 
      endMargin: '2%', 
      color: '#f2f2f2' 
    }) 
    .edgeEffect(EdgeEffect.None) // 必须设置列表为滑动到边缘无效果 
    .divider({ strokeWidth: 1, color: 0x222222 }) 
    // 必须设置列表为滑动到边缘无效果 
    .edgeEffect(EdgeEffect.Spring) 
    .scrollBar(BarState.Off) 
    .enableScrollInteraction(this.mainListCanScroll) 
    .onScroll((scrollOffset: number, scrollState: ScrollState) => { 
      console.info('offset1:' + this.mainScroller.currentOffset().yOffset); 
      let currentOffsetY = Math.round(this.mainScroller.currentOffset().yOffset); 
      //导航栏 
      if (currentOffsetY > 100) { 
        this.hideNavBar = false 
      } 
      else { 
        this.hideNavBar = true 
      } 
      //主list和分list滑动控制 
      if (currentOffsetY == 0) { 
        this.mainListCanScroll = true 
      } 
      else { 
        if (currentOffsetY >= 400) { 
          this.mainListCanScroll = false 
        } 
        else { 
          this.mainListCanScroll = true 
        } 
      } 
      //需要控制下拉和上拉后的刷新 
      //下拉超过80,松手后检测,如果松手后的偏移量超过80,则刷新 
      //如果松手后的偏移量不超过,则不执行刷新操作 
      if (currentOffsetY >= 0) { 
        this.hideLoadingView = true 
      } 
      else { 
        this.hideLoadingView = false 
      } 
      this.progress = currentOffsetY / 80 * (-10); 
    }) 
    .onReachStart(() => { 
      console.info('到最上面了========1') 
    }) 
    .onReachEnd(() => { 
      console.info('到最底部了========1') 
    }) 
  } 
}

适配版本信息

IDE:DevEco Studio 4.0.1.501

SDK:HarmoneyOS 4.0.0.8

分享
微博
QQ
微信
回复
2024-05-27 16:15:41
相关问题
Scroll与WaterFlow滑动嵌套
411浏览 • 1回复 待解决
WebList嵌套手势冲突问题
412浏览 • 1回复 待解决
Tabs组件嵌套滑动组件
595浏览 • 1回复 待解决
嵌套组件中Scroll不生效
654浏览 • 1回复 待解决
如何屏蔽List滑动事件
832浏览 • 1回复 待解决
使用List嵌套实现表格布局
429浏览 • 1回复 待解决
Scroll内Flex加宽高与滑动冲突
652浏览 • 1回复 待解决
如何监听List组件滑动距离
600浏览 • 1回复 待解决
页面列表嵌套滚动,实现列表吸顶
456浏览 • 1回复 待解决