中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
嵌套滑动处理,除了。
nestedScroll({ scrollForward: NestedScrollMode.PARENT_FIRST, scrollBackward: NestedScrollMode.SELF_FIRST })
这种方式,还有其他方式吗?
目前遇到的问题是,外层是Scroll,里层是Swiper,Swiper里层是List,给List设置。
.nestedScroll({ scrollForward: NestedScrollMode.PARENT_FIRST, scrollBackward: NestedScrollMode.SELF_FIRST })
这时候嵌套滑动处理正常,需求增加下拉刷新和上拉加载,给List外层再套一层自定义的下拉刷新和上拉加载布局后,嵌套滑动处理不能工作了。
微信扫码分享
@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') }) } }