HarmonyOS 鸿蒙Next基于组件嵌套的滑动

发布于 1周前 作者 bupafengyu 来自 鸿蒙OS

HarmonyOS 鸿蒙Next基于组件嵌套的滑动 场景一

基于nestedScroll实现WaterFlow与Scroll混合滑动。

方案:

外层scroll滑动,内层waterFlow滑动,外层使用嵌套属性.nestedScroll进行联动。

核心代码

Scroll() { 
  Column({ space: 2 }) { 
    Image($r('app.media.dzdp')) 
      .width('100%') 
      .height(200) 
      .objectFit(ImageFit.Cover) 
    WaterFlow() { 
      LazyForEach(this.dataSource, (item: number) => { 
        FlowItem() { 
          Column() { 
            Text("旅游景点" + item).fontSize(12).height('16') 
            Image($r(`app.media.img_${item % 10}`)) 
              .objectFit(ImageFit.Fill) 
              .width('100%') 
              .layoutWeight(1) 
          } 
        } 
        .width('100%') 
        .height(this.itemHeightArray[item % 100]) 
        .backgroundColor(this.colors[item % 5]) 
      }, (item: string) => item) 
    } 
    .columnsTemplate("1fr 1fr") 
    .columnsGap(10) 
    .rowsGap(5) 
    .backgroundColor(0xFAEEE0) 
    .width('100%') 
    .height('100%') 
    .nestedScroll( 
      { 
        scrollForward: NestedScrollMode.SELF_FIRST, // 往末尾端滚动 
        scrollBackward: NestedScrollMode.SELF_FIRST // 往起始端滚动 
      }) 
  } 
}

场景二

list中嵌套List滑动。

方案:

外层list组件和内层list组件基于.nestedScroll进行联动。

核心代码

@Component 
struct ItemCompponet { 
  build() { 
    Column() { 
      header({ title: this.title }) 
      List() { 
        LazyForEach(this.generateDataSource(), (data: string, index) => { 
          ListItem() { 
            Row(){ 
              Text(data) 
                .fontSize(14) 
                .backgroundColor(Color.White) 
                .fontColor(bgColors[index % bgColors.length]) 
                .textAlign(TextAlign.Center) 
                .onAppear(() => { 
                  console.info("appear:" + data) 
                }) 
              Image($r(this.Image)) 
                .width(40) 
                .height(20) 
                .margin({ 
                  left: 20 
                }) 
            } 
            .width('100%') 
          } 
          .height(rowHeight) 
        }, (data: string, index) => { 
          return data + ' - ' + index.toString() 
        }) 
      } 
      .nestedScroll({ 
        scrollForward:this.posType[this.currentType[0]], 
        scrollBackward: this.posType[this.currentType[1]] 
      }) 
      .scrollBar(BarState.Off) 
      .cachedCount(10) 
      .friction(1.25) 
      .edgeEffect(EdgeEffect.None) 
    } 
  } 
} 

@Component 
export struct ListToList { 
  @Builder 
  private mainListView() { 
    List({ scroller: this.scroll }) { 
      ListItem() { 
        ItemCompponet({ title: '美食', datas: generateData('美食', 20) ,currentType:[0,1],Image: 'app.media.food'}) 
      } 
      .height('100%') 
      .margin({bottom:50}) 
      ListItem() { 
        ItemCompponet({ title: '旅游景点', datas: generateData('旅游景点', 20)  ,currentType:[0,0],Image: 'app.media.travel'}) 
      } 
      .height('100%') 
      .margin({bottom:50}) 
      ListItem() { 
        ItemCompponet({ title: '健康', datas: generateData('健康', 20)  ,currentType:[0,0],Image: 'app.media.medicine'}) 
      } 
      .height('100%') 
      .margin({bottom:50}) 
 
      ListItem() { 
        ItemCompponet({ title: '运动', datas: generateData('运动', 20)  ,currentType:[1,0],Image: 'app.media.run'}) 
      } 
      .height('100%') 
    } 
    .onReachEnd(()=>{ 
      console.log('is end') 
    }) 
    .divider({ strokeWidth: 10, color: Color.Gray }) 
    .height("100%") 
    .width("100%") 
    .scrollBar(BarState.Off) 
    .edgeEffect(EdgeEffect.None) 
  } 
}

更多关于HarmonyOS 鸿蒙Next基于组件嵌套的滑动的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于HarmonyOS 鸿蒙Next基于组件嵌套的滑动的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


针对帖子标题“HarmonyOS 鸿蒙Next基于组件嵌套的滑动”的问题,以下是专业且简洁的回答:

在HarmonyOS鸿蒙Next系统中,实现基于组件嵌套的滑动功能,主要依赖于鸿蒙系统的UI框架提供的布局管理和事件处理机制。

鸿蒙系统支持多种布局方式,如DirectionalLayout、StackLayout等,这些布局可以嵌套使用以实现复杂的UI结构。在实现滑动功能时,通常会用到ScrollView或ListContainer等组件,它们能够包裹其他组件并提供滑动能力。

对于组件嵌套的情况,你需要确保外层的滑动组件(如ScrollView)能够正确地包裹内层的布局和组件。同时,内层的组件布局也需要合理设置,以避免滑动冲突或布局混乱。

在实现过程中,可能需要注意以下几点:

  • 确保滑动组件的滚动方向正确设置(水平或垂直)。
  • 检查内层组件的布局参数,避免超出外层组件的边界。
  • 如果使用了列表组件(如ListContainer),需要正确处理列表项的加载和回收。

如果遇到具体的滑动问题,如滑动不流畅、滑动冲突等,可以检查相关组件的属性设置和事件处理逻辑。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!