HarmonyOS 列表List相关问题

list中存在多个item,由于宽度不同导致部分item不完全可见,想知道完全可见这部分demo的下标。

1、怎么获取List中第一个完全可见的item下标。

2、怎么获取List中最后一个完全可见的item下标。

3、怎么判断List中某个item是否完全可见。

HarmonyOS
2024-09-24 10:35:40
1213浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

HarmonyOS没有可以直接获取第一个和最后一个完全可见元素的接口,需要使用onScrollIndex和OnScroll回调配合实现。onScrollIndex能获取当前窗口内所有可见的item,在onscrollstop中,通过获取可见item的坐标和父容器坐标的比较得到完全可见的item的index。

具体DEMO如下,以list中text左对齐为例:

@Entry  
@Component  
struct ListExample {  
  private arr: number[] = []  
  private scrollerForList: Scroller = new Scroller()  
  @State startStr: string = ''  
  
  startIndex = 0  
  endIndex = 0  
  initState = true  
  rowWidth = 400  
  private judgeVisible() {  
    this.startStr = ""  
    // 判断start是否遮挡  
    let rect = this.scrollerForList.getItemRect(this.startIndex)  
    // 左对齐的场景下,如果左边第一个元素完全可见,那么元素x坐标(相对row的坐标)误差在1以内  
    if (!((-1 < rect.x) && (rect.x < 1)))  
    {  
      this.startIndex = this.startIndex + 1  
    }  
    rect = this.scrollerForList.getItemRect(this.endIndex)  
    // 左对齐的场景下,如果右边第一个元素完全可见,那么元素x坐标加上元素宽度 应该在row的宽度范围之内(还需要考虑1px的误差)  
    if (!((rect.x + rect.width) > this.rowWidth - 1 && (rect.x + rect.width) < this.rowWidth + 1))  
    {  
      this.endIndex = this.endIndex - 1  
    }  
    if (this.startIndex <= this.endIndex) {  
      this.startStr = String(this.startIndex) + "," + String(this.endIndex)  
    }else {  
      this.startStr = "没有能全部显示的控件"  
    }  
  }  
  
  aboutToAppear() {  
    for (let i = 0; i < 20; i++) {  
      this.arr.push(i)  
    }  
  }  
  build() {  
    Column() {  
      Row() {  
        Text(this.startStr)  
          .fontSize(10)  
          .textAlign(TextAlign.Start)  
          .fontColor(Color.Red)  
      }  
      .height(100)  
      .backgroundColor(Color.Gray)  
      .opacity(0.3)  
      .width("100%")  
  
      Row() {  
        List({ space: 20, initialIndex: 0, scroller: this.scrollerForList }) {  
          ForEach(this.arr, (item: number) => {  
            ListItem() {  
              Text('' + item)  
                .width('100%').height(100).fontSize(16)  
                .textAlign(TextAlign.Center)  
            }  
            .borderRadius(10).backgroundColor(0xFFFFFF)  
            .width(item%4==0 ? 500 : 50)  
            .height(200)  
          }, (item: number) => JSON.stringify(item))  
        }  
        .chainAnimation(true)  
        .edgeEffect(EdgeEffect.Spring)  
        .listDirection(Axis.Horizontal)  
        .height('100%')  
        .width(this.rowWidth)  
        .scrollSnapAlign(ScrollSnapAlign.START)  
        .borderRadius(10)  
        .backgroundColor(0xDCDCDC)  
        .divider({ strokeWidth: 2, color: 0xFF00000, startMargin: 10, endMargin: 40 })  
        .onScrollIndex((start, end)=>{  
          this.startIndex = start  
          this.endIndex = end  
          // 界面初始显示的时候,不会触发onScrollStop事件,在这里判断可见性  
          if (this.initState == true) {  
            this.judgeVisible()  
            this.initState = false  
          }  
        })  
        .onScrollStop(()=>{  
          // 缓存开始结束索引: 在最后一页多次右滑时候,不会触发onScrollIndex  
          let tmpStart = this.startIndex  
          let tmpEnd = this.endIndex  
          this.judgeVisible()  
          this.startIndex = tmpStart  
          this.endIndex =tmpEnd  
        })  
      }  
      .width('100%')  
      .height('100%')  
      .backgroundColor(0xDCDCDC)  
      .padding({ top: 10 })  
    }  
  }  
}
  • 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.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
分享
微博
QQ
微信
回复
2024-09-24 15:48:17


相关问题
HarmonyOS list sticky相关问题
578浏览 • 1回复 待解决
HarmonyOS 列表展示list懒加载问题
1287浏览 • 1回复 待解决
使用LazyForEach懒加载列表相关问题
1730浏览 • 1回复 待解决
HarmonyOS list 列表项交换
600浏览 • 1回复 待解决
HarmonyOS List控制器Scroller相关
886浏览 • 1回复 待解决
HarmonyOS list嵌套tab中列表高度变化
670浏览 • 1回复 待解决
HarmonyOS List列表滚动到指定位置
1166浏览 • 1回复 待解决
HarmonyOS List嵌套ListList嵌套Grid问题
871浏览 • 1回复 待解决
HarmonyOS 滚动列表问题
594浏览 • 1回复 待解决
HarmonyOS 滚动列表问题
721浏览 • 0回复 待解决
HarmonyOS 列表选择问题
510浏览 • 1回复 待解决
Redis数据类型列表list是什么?
4117浏览 • 1回复 待解决
HarmonyOS List回弹问题
557浏览 • 1回复 待解决
HarmonyOS list组件问题
773浏览 • 1回复 待解决
List列表组件如何改为横向显示的?
1626浏览 • 1回复 待解决
HarmonyOS 列表刷新问题
1322浏览 • 1回复 待解决
HarmonyOS Grid相关问题
1240浏览 • 1回复 待解决
HarmonyOS Worker相关问题
885浏览 • 1回复 待解决
HarmonyOS string相关问题
911浏览 • 1回复 待解决
HarmonyOS 证书相关问题
1020浏览 • 1回复 待解决
HarmonyOS BindSheet相关问题
1409浏览 • 1回复 待解决