在HarmonyOS NEXT开发中List(A)里嵌套一个List(B)问题?

在HarmonyOS NEXT开发中List(A)里嵌套一个List(B),往下滑动里边的B,如果实现先响应B,当B到达边缘时响应A,当A到边缘时再响应B的边缘效果?

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

阅读 587
avatarAI BotBETA
### 回答

在HarmonyOS NEXT开发中,要实现List(A)里嵌套一个List(B),并且控制滑动事件的优先级,可以按照以下思路进行:

1. **事件分发机制**:
   - 在自定义的组件或页面中,重写滑动事件的处理方法(如`onTouchEvent`或`onInterceptTouchEvent`)。
   - 在这些方法中,首先判断当前滑动的目标是否为List(B)。
   - 如果List(B)可以处理滑动(即未到达边缘),则让List(B)消费该事件。
   - 如果List(B)已经到达边缘且无法继续滑动,则将事件传递给List(A),让List(A)继续处理滑动。

2. **边缘检测**:
   - 通过监听List(B)的滚动状态(如使用滚动监听器`OnScrollListener`),判断List(B)是否已经到达边缘。
   - 当List(B)到达边缘时,可以设置一个标志位,表示List(B)无法继续滑动。
   - 在事件分发机制中,根据这个标志位来决定是否将滑动事件传递给List(A)。

3. **循环边缘效果**:
   - 当List(A)也到达边缘时,如果希望List(B)能够继续响应滑动(实现循环边缘效果),可以在List(A)的边缘处理逻辑中,重新将滑动事件传递给List(B),但此时需要调整List(B)的显示位置或内容,以模拟循环滚动的效果。
   - 这通常涉及到对List(B)的数据或视图进行一定的调整,以确保用户界面的连贯性和一致性。

4. **注意事项**:
   - 在实现过程中,要注意滑动事件的冲突处理,避免因为事件分发不当导致界面卡顿或异常。
   - 同时,也要考虑性能优化,特别是在处理复杂的嵌套滚动结构时,要确保应用的流畅性和响应速度。

通过上述方法,你可以在HarmonyOS NEXT开发中实现List(A)里嵌套List(B)的滑动优先级控制,并达到先响应B、再响应A、最后循环响应B的边缘效果。
1 个回答

具体解决方案,参考代码如下:

@Entry 
@Component 
struct WeiboPersonPage1 { 
  @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') 
    }) 
  } 
} 

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进