HarmonyOS List第一个可见的ListItem

HarmonyOS List第一个可见的ListItem。

HarmonyOS
2024-10-17 11:40:29
833浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

请参考:

@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.
  • 60.
分享
微博
QQ
微信
回复
2024-10-17 16:12:05
相关问题
HarmonyOS 如何返回路由第一个视图
1028浏览 • 1回复 待解决
HarmonyOS 要做一个可以无限滚动list
1668浏览 • 1回复 待解决