HarmonyOS 鸿蒙Next List中的item如何感知到自己的位置

HarmonyOS 鸿蒙Next List中的item如何感知到自己的位置

我有一个List组件里面存放了n个 PostItemView,我的每一个PostItemView如何在滚动时感知到自己attachToWindow出现在了屏幕上 并且能够知道出现的百分比

2 回复

如果您的postItemView高度固定的话,当前List控件滚动的距离:在交互操作中可以使用currentOffset获取。

可以参考https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-scroll-V5#currentoffset

如果您的postItemView高度不固定的话,可以给每个元素加一个onAreaChange方法,记录他目前的偏移位置,这样也可以得出。

可以参考https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-component-area-change-event-V5#onareachange

有个简易demo您参考一下

@Entry

@Component

struct testList {

  @State test: number[] = [100,200,300,400, 100,200,300,400,100,200,300,400]

  listOffset: number = 0

  @State listHeight: number = 0

  @Builder

  listItem(height: number, index: number){

    Text(index.toString()).width('100%').height(height)

      .onAreaChange((oldA,nexA)=>{

        // 顶部距list顶部距离

        const offsetT = nexA.position.y as number - this.listOffset

        console.info('打点前',  index + '', nexA.position.y + '', this.listOffset + '')

        // 底部距list顶部距离

        const offsetB = nexA.height as number + offsetT

        if(offsetB < height * .2 || this.listHeight - offsetT < height * .2){

          return

        }

        console.info('打点', index + '', nexA.position.y + '')

      })

  }

  build() {

    Column(){

      Row(){

        Text('占位')

      }.height(100)

      List(){

        ForEach(this.test, (item: number, index: number)=>{

          // this.listItem(item, index)

          listItem({ childHeight: item, index, listOffset: this.listOffset, listHeight: this.listHeight })

        })

      }

      .layoutWeight(1)

      .onAreaChange((oldA,nexA)=>{

        this.listOffset = nexA.position.y as number

        console.info(nexA.height + '')

        this.listHeight = nexA.height  as number

      })

    }

  }

}

@Component

struct listItem{

  @Prop @Require childHeight: number;

  @Prop @Require index: number

  @Prop @Require listOffset: number

  @Prop @Watch('changeHeight') listHeight: number = 0

  itemY: number = 0

  itemHeight: number = 0

  changeHeight(){

    // 顶部距list顶部距离

    const offsetT = this.itemY - this.listOffset

    console.info('打点前',  this.index + '', this.itemY + '', this.listOffset + '')

    // 底部距list顶部距离

    const offsetB = this.itemHeight + offsetT

    if(offsetB < this.itemHeight * .2 || this.listHeight - offsetT < this.itemHeight * .2){

      return

    }

    console.info('打点', this.index + '', this.itemY + '')

  }

  build() {

    Text(this.index.toString()).width('100%').height(this.childHeight)

      .onAreaChange((oldA,nexA)=>{

        this.itemY = nexA.globalPosition.y as number

        this.itemHeight = nexA.height as number

        this.changeHeight()

      })

  }

}

更多关于HarmonyOS 鸿蒙Next List中的item如何感知到自己的位置的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙系统中,Next List中的item感知自己的位置通常通过数据绑定和组件状态管理来实现。具体来说,可以通过以下方式实现:

  1. 数据传递:在创建Next List的适配器时,可以将item的位置信息(如索引)作为数据的一部分传递给每个item的视图模型(ViewModel)或数据模型(DataModel)。这样,每个item在渲染时就能获取到自己的位置信息。

  2. 事件回调:如果Next List的item组件支持自定义事件或回调,可以在item被添加到列表时注册一个回调,该回调会接收当前item的位置信息作为参数。这样,item组件内部就可以根据这个位置信息来执行相应的逻辑。

  3. 组件状态管理:利用HarmonyOS提供的状态管理机制(如组件的状态属性),可以在item被添加到列表时,通过状态更新来传递位置信息。这样,item组件在状态变化时就能感知到自己的位置。

实现方式的选择取决于具体的业务需求和系统设计。在实际开发中,可以根据Next List的使用场景和性能要求来选择最合适的方法。

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

回到顶部