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

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

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

3 回复

可以参考如下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-0000001862607449#ZH-CN_TOPIC_0000001862607449__示例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-0000001815767776

更多关于HarmonyOS 鸿蒙Next list如何展示分割线?要自己添加控件吗?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS(鸿蒙)系统中,若想在Next list(通常指的是列表控件)中展示分割线,不一定需要手动添加额外的控件。HarmonyOS的列表组件通常提供了配置选项来直接显示分割线。

具体来说,可以通过设置列表项(List Item)的布局参数或列表本身的样式属性来实现分割线的展示。例如,在XML布局文件中,可以为列表项容器设置背景为带有底部边框的Drawable资源,或者利用系统提供的样式属性直接启用分割线。

在Java或Kotlin代码中,也可以通过设置列表的适配器(Adapter)或者列表本身的属性来配置分割线。一些列表控件提供了如setDivider()setDividerHeight()等方法,用于设置分割线的Drawable资源和高度。

如果当前使用的列表控件没有直接提供这样的API,那么可能需要自定义列表项的布局,通过添加一个View(如View或LinearLayout)作为分割线,并设置其背景颜色、高度等属性来模拟分割线效果。

总之,多数情况下,通过合理配置列表控件的属性或布局文件,即可实现分割线的展示,无需手动为每个列表项添加额外的控件。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html。

回到顶部