HarmonyOS 判断组件滚出了屏幕

做一个视频列表播放的功能,一个列表上有一个视频,如果滚出屏幕外面,就停止播放,怎么判断一个组件是否在屏幕内呢?

HarmonyOS
2024-12-25 12:25:46
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

在视频所在列表项加个onVisibleAreaChange事件来判断状态,currentRatio为0表示完全不可见,currentRatio为1表示完全可见,其他是部分可见。

参考文档:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-component-visible-area-change-event-V5

以下示例给出判断index=2的列表项的可见状态判断,供参考:

@Entry
@Component
struct ListExample {
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  @State str:string = '完全不可见'
  build() {
    Column() {
      Text(this.str)
        .fontSize(24)
      List({ space: 20, initialIndex: 0 }) {
        ForEach(this.arr, (item: number,index:number) => {
          ListItem() {
            if(index==2){
              Text('' + item)
                .width('100%').height(100).fontSize(16)
                .textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF)
                // 组件可见区域变化事件
                .onVisibleAreaChange([0,1],(isVisible: boolean, currentRatio: number)=>{
                  if(currentRatio==0){
                    this.str = '完全不可见'
                  }else if (currentRatio==1){
                    this.str = '完全可见'
                  }else {
                    this.str = '部分可见'
                  }
                })
            }else {
              Text('' + item)
                .width('100%').height(100).fontSize(16)
                .textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF)
            }
          }
        }, (item: string) => item)
      }
      .listDirection(Axis.Vertical) // 排列方向
      .scrollBar(BarState.Off)
      .friction(0.6)
      .divider({ strokeWidth: 2, color: 0xFFFFFF, startMargin: 20, endMargin: 20 }) // 每行之间的分界线
      .edgeEffect(EdgeEffect.Spring) // 边缘效果设置为Spring
      .onScrollIndex((firstIndex: number, lastIndex: number, centerIndex: number) => {
        console.info('first' + firstIndex)
        console.info('last' + lastIndex)
        console.info('center' + centerIndex)
      })
      .width('90%')
    }
    .width('100%')
    .height('100%')
    .backgroundColor(0xDCDCDC)
    .padding({ top: 5 })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
分享
微博
QQ
微信
回复
2024-12-25 15:01:35


相关问题
HarmonyOS 如何判断控件移出了屏幕
551浏览 • 1回复 待解决
HarmonyOS 怎么判断屏幕是否打开
647浏览 • 1回复 待解决
HarmonyOS UI组件如何知道弹出了软键盘
542浏览 • 1回复 待解决
HarmonyOS 传感器判断屏幕方向
842浏览 • 1回复 待解决
HarmonyOS 如何判断手机屏幕类型
672浏览 • 1回复 待解决
HarmonyOS Web组件UserAgent判断方法
767浏览 • 1回复 待解决
HarmonyOS web组件滚动方向判断
574浏览 • 1回复 待解决
HarmonyOS Text组件无法铺满屏幕
631浏览 • 1回复 待解决
如何判断Web组件是否全屏
2717浏览 • 1回复 待解决
HarmonyOS row中的子控件超出了row的范围
1272浏览 • 1回复 待解决
HarmonyOS 屏幕锁屏和解锁屏幕
1071浏览 • 1回复 待解决