HarmonyOS 获取List中当前屏幕可见的ListItem

需要获取List中当前可见的ListItem,并获取其坐标位置,然后锚定到对应的tab

咨询场景描述: 现在是在onAreaChange处理相关逻辑,可能导致卡顿,是否有其他方案

HarmonyOS
2024-08-08 15:59:01
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

可参考如下示例代码:

@Entry 
@Component 
struct ListExample { 
  private arr: (number | string)[] = [0, '概览', '周边', 3, 4, '房源', 6, 7, '小区', 9, 10, 11, 12, 13, 14, 15] 
  private tabArr: string[] = ['概览', '周边', '房源', '小区'] 
  @State currentIndex: number = 0 
 
  build() { 
    Column() { 
      Row() { 
        ForEach(this.tabArr, (item: string, index: number) => { 
          Text(item) 
            .fontColor(this.currentIndex === index ? Color.Blue : '') 
        }) 
      } 
      .width('100%') 
      .padding({ left: 20 }) 
      .alignItems(VerticalAlign.Top) 
 
      List({ space: 20, initialIndex: 0 }) { 
        ForEach(this.arr, (item: number) => { 
          ListItem() { 
            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) 
        if (firstIndex == 1) { 
          this.currentIndex = 0 
        } else if (firstIndex == 2) { 
          this.currentIndex = 1 
        } else if (firstIndex == 5) { 
          this.currentIndex = 2 
        } else if (firstIndex == 8) { 
          this.currentIndex = 3 
        } 
      }) 
      .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.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
分享
微博
QQ
微信
回复
2024-08-08 20:21:20
相关问题
HarmonyOS List第一个可见ListItem
1104浏览 • 1回复 待解决
获取当前设备屏幕方向
1032浏览 • 1回复 待解决
获取当前设备屏幕密度。
1034浏览 • 1回复 待解决
HarmonyOS Listlistitem较少时无法拖拽
384浏览 • 1回复 待解决
HarmonyOS 怎么获取当前屏幕亮度值
777浏览 • 1回复 待解决
ListListItem组件使用
2583浏览 • 1回复 待解决
是否有获取当前屏幕亮度API
2909浏览 • 1回复 待解决
HarmonyOS如何实现list listitem拖拽
1821浏览 • 1回复 待解决
HarmonyOS 代码如何获取当前线程
909浏览 • 1回复 待解决