获取Scroll容器组件内指定组件的坐标值

获取Scroll容器组件内指定组件的坐标值

HarmonyOS
2024-06-04 23:26:22
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
__catcher

可通过以下两种方式获取组件坐标:方案一:给组件绑定onAreaChange属性来获取组件位置信息,将组件信息用数组存起来。scrollTo的yOffset通过数组下标来获取。

方案二:导入@ohos.arkui.componentUtils,通过getRectangleById来获取组件位置信息

示例代码

demo:

...  
    scroller: Scroller = new Scroller()  
    private componentsInfo: Position[] = []  
    private currentId: number = 1 // 要滚动的子组件索引  
... 
Stack() { 
  Scroll(this.scroller) { 
    Column() { 
      Button('LazyForEach') 
        .fontSize(20) 
        .margin({ bottom: 20 }) 
        .fontWeight(FontWeight.Bold) 
        .onAreaChange((oldValue: Area, newValue: Area) => { 
          this.componentsInfo[0] = newValue.position 
        }) 
      Button('web H5') 
        .fontSize(20) 
        .margin({ bottom: 20 }) 
        .fontWeight(FontWeight.Bold) 
        .onAreaChange((oldValue: Area, newValue: Area) => { 
          this.componentsInfo[1] = newValue.position 
        }) 
    } 
  } 
  Button('WebH5').onClick(() => { 
    this.scroller.scrollTo({ xOffset: 0, yOffset: this.componentsInfo[this.currentId].y as number }) 
  }) 
}

参考链接

组件区域变化事件onAreaChage​​componentUtils.getRectangleById

分享
微博
QQ
微信
回复
2024-06-05 19:58:15
相关问题
Scroll组件显示不全问题
333浏览 • 1回复 待解决
如何可以获取组件中心点坐标
342浏览 • 1回复 待解决
如何获取Scroll组件的当前滚动偏移量
568浏览 • 1回复 待解决
Polyline组件绘制坐标不准确
593浏览 • 1回复 待解决
如何隐藏容器组件溢出内容
868浏览 • 1回复 待解决
Scroll组件展示位置如何调整
706浏览 • 1回复 待解决
容器组件onVisibleAreaChange不生效
467浏览 • 1回复 待解决
在嵌套组件Scroll不生效
634浏览 • 1回复 待解决
组件溢出父容器问题
452浏览 • 1回复 待解决
ArkTs如何自定义容器组件
1547浏览 • 1回复 待解决
docker容器MySQL、Redis无法连接
2021浏览 • 1回复 待解决
组件中如何处理子组件点击事件
1055浏览 • 1回复 待解决
ScrollFlex加宽高与滑动冲突
650浏览 • 1回复 待解决
如何获取组件属性信息
648浏览 • 1回复 待解决
应用组件截图并保存到用户文件
126浏览 • 1回复 待解决