HarmonyOS 鸿蒙Next Swiper嵌套时无法定位

HarmonyOS 鸿蒙Next Swiper嵌套时无法定位 一级Swiper中包含10个item,每个item里又有一个Swiper其中也是包含10个item。先需要点击某个按钮后定位,比如需要父级切换到第8个item并且当前子Swiper要切换到第4个item。现在遇到的问题是打印的子级Swiper需要定位的index是正常的,但是定位不成功。以下是核心逻辑:

父级:

// 当前选中的试题id(每次滑动时不会更新的)
@Provider() selectQuesId: number = 0 
// 当前选中的index(与试题id一一对应)
@Local selectIndex: number = 0 

/** 
* 选中的试题更改后同步选中的index 
*/ 
@Monitor('selectQuesId') watchSelectQuesId() {
  this.selectIndex = this.viewModel.getParentIndexByQuesId(this.selectQuesId) ?? this.selectIndex
  error('------------定位1:' + this.selectIndex + '---------' + this.selectQuesId)
}

Swiper(this.parentSwiperController) { 
  LazyForEach(this.viewModel.quesList, (item: Ques) => { 
    QuesExamPaper({
      quesId: item.quesId,
      nextQues: () => { 
        this.showNextQues()
      }
    })
  }, (item: Ques) => item.quesId?.toString())
}
.layoutWeight(1)
.loop(false)
.index($$this.selectIndex)
.cachedCount(1)
.indicator(false)

子级:

@Local selectIndex: number = 0 // 当前选中的试题id
@Consumer() selectQuesId: number = 0 

@Monitor('selectQuesId') watchSelectQuesId() { 
  this.selectIndex = this.quesDetails?.childList?.findIndex((item) => item.id === this.selectQuesId) ?? this.selectIndex
}

Swiper(this.swiperController) { 
  ForEach(this.quesDetails?.childList, (item: QuesDetailEntity) => { 
    QuesChoicePaperV2({
      quesDetails: item,
      nextQues: () => { 
        if (this.selectIndex === (this.quesDetails?.childList?.length ?? 0) - 1) { 
          this.nextQues?.()
        } else { 
          this.swiperController.showNext()
        }
      }
    })
  }, (item: QuesDetailEntity) => item.id?.toString())
}
.nestedScroll(SwiperNestedScrollMode.SELF_FIRST)
.cachedCount(1)
.loop(false)
.indicator(false)
.index($$this.selectIndex)

更多关于HarmonyOS 鸿蒙Next Swiper嵌套时无法定位的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

试下双向绑定写法,设置变量,子组件同步给父组件在获取,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-new-binding-V5

更多关于HarmonyOS 鸿蒙Next Swiper嵌套时无法定位的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


针对HarmonyOS鸿蒙系统中Next Swiper组件嵌套时无法定位的问题,这通常涉及到组件的层级管理和布局参数设置。在鸿蒙开发中,组件的嵌套和定位依赖于其布局容器和自身的布局属性。

首先,检查每个Swiper组件的父容器布局类型,确保它们支持嵌套结构。例如,使用DirectionalLayout或StackLayout等容器,这些容器通常能更好地管理嵌套组件的布局。

其次,确认每个Swiper组件的布局参数,特别是宽度、高度和边距设置。确保这些参数没有导致组件超出其父容器的边界或被其他组件遮挡。

此外,检查Swiper组件的滚动方向和分页设置,确保它们与嵌套结构兼容。例如,如果外层Swiper是水平滚动,内层Swiper也设置为水平滚动,可能会导致定位问题。

最后,如果使用了自定义布局或样式,请检查相关代码,确保没有逻辑错误或属性冲突。

如果以上步骤无法解决问题,可能是由于鸿蒙系统的特定版本或组件库存在bug。此时,建议查阅鸿蒙官方的更新日志和开发者社区,看是否有相关的问题报告和解决方案。

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

回到顶部