HarmonyOS list如何展示分割线?要自己添加控件吗?

HarmonyOS
2024-12-26 13:17:43
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

可以参考如下demo:

// xxx.ets

@Entry
@Component
struct ListExample {
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

  build() {
    Column() {
      List({ space: 20, initialIndex: 0 }) {
        ForEach(this.arr, (item: number) => {
          if (item !== 0) {
            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)
      })
      .onScroll((scrollOffset: number, scrollState: ScrollState) => {
        console.info(`onScroll scrollState = ScrollState` + scrollState + `, scrollOffset = ` + scrollOffset)
      })
      .width('90%')
    }
    .width('100%')
    .height('100%')
    .backgroundColor(0xDCDCDC)
    .padding({ top: 5 })
  }
}

divider就是添加分割线的属性,参考链接:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-list-V5#示例1

如果给List加上divider(分割线)没办法隐藏个别分割线,可以单独给想要显示分割线的地址加上相应的分割线,可以参考如下demo:

@Entry
@Component
struct ListExample {
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

  build() {
    Column() {
      List({ space: 20, initialIndex: 0 }) {
        ForEach(this.arr, (item: number) => {
          if (item === 0) {
            ListItem() {
              Column() {
                Text('' + item)
                  .width('100%')
                  .height(100)
                  .fontSize(16)
                  .textAlign(TextAlign.Center)
                  .borderRadius(10)
                  .backgroundColor(0xFFFFFF)
              }
            }
          } else {
            ListItem() {
              Column() {
                Text('' + item)
                  .width('100%')
                  .height(100)
                  .fontSize(16)
                  .textAlign(TextAlign.Center)
                  .borderRadius(10)
                  .backgroundColor(0xFFFFFF)
                Divider().strokeWidth(2).color('#f00').margin({ top: 20, bottom: 10 })
              }
            }
          }
        }, (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)
      })
      .onScroll((scrollOffset: number, scrollState: ScrollState) => {
        console.info(`onScroll scrollState = ScrollState` + scrollState + `, scrollOffset = ` + scrollOffset)
      })
      .width('90%')
    }
    .width('100%')
    .height('100%')
    .backgroundColor(0xDCDCDC)
    .padding({ top: 5 })
  }
}

参考链接:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-divider-V5

分享
微博
QQ
微信
回复
2024-12-26 16:17:28
相关问题
List如何设置分割线左右的边距?
664浏览 • 1回复 待解决
HarmonyOS Grid如何设置分割线
277浏览 • 1回复 待解决
HarmonyOS 如何实现分割线拖拽?
157浏览 • 1回复 待解决
Divider组件如何设置分割线宽度
3055浏览 • 1回复 待解决
如何设置TabBar和TabContent的分割线样式
2501浏览 • 1回复 待解决
如果让分割线垂直起来
6091浏览 • 1回复 待解决
HarmonyOS list控件子组件复用
711浏览 • 1回复 待解决
HarmonyOS List展示不全的问题
501浏览 • 1回复 待解决
HarmonyOS 列表展示list懒加载问题
825浏览 • 1回复 待解决