HarmonyOS List下循环重复使用RichText时,会出现卡顿、滑动响应慢等现象,使用RichEditor组件的方案有吗?

​RichText组件比较消耗内存资源,而且有一些重复使用RichText组件的场景下,比如在List下循环重复使用RichText时,会出现卡顿、滑动响应慢等现象。在这种情况下,推荐使用RichEditor组件。能否提供一个DEMO,里面实现在List中使用RichEditor组件,并解析RichEditor解析各种html标签。

HarmonyOS
2024-10-15 11:57:52
1062浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

参考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) => {  
          ListItem() {  
            RichEditorExample()  
          }  
        }, (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 })  
  }  
}  
@Component  
struct RichEditorExample {  
  controller: RichEditorController = new RichEditorController()  
  @Builder CustomKeyboardBuilder() {  
    Column() {  
      Grid() {  
        ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#'], (item: number | string) => {  
          GridItem() {  
            Button(item + "")  
              .width(110).onClick(() => {  
              this.controller.addTextSpan(item + '', {  
                offset: this.controller.getCaretOffset(),  
                style:  
                {  
                  fontColor: Color.Orange,  
                  fontSize: 30  
                }  
              })  
              this.controller.setCaretOffset(this.controller.getCaretOffset() + item.toString().length)  
            })  
          }  
        })  
      }.maxCount(3).columnsGap(10).rowsGap(10).padding(5)  
    }.backgroundColor(Color.Gray)  
  }  
  build() {  
    Column() {  
      RichEditor({ controller: this.controller })  
        .customKeyboard(this.CustomKeyboardBuilder()).margin(10).border({ width: 1 })  
        .height(200)  
        .borderWidth(1)  
        .borderColor(Color.Red)  
        .width("100%")  
    }  
  }  
}
  • 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.

如下是富文本组件,解析并显示HTML格式文本。

@Entry  
@Component  
struct RichTextExample {  
  @State data: string =  
    '<span style="margin-right:20px;font-size:25px">这是一段文字这是一段文字这是一段文字这是一段文字这是一段文一段文字这是一段文一段文字这是段文一段文字这是段文一段文字这是段文一段文字这是段文一段文字这是段文字这是一段文字</span>'+  
      '<span style="display:inline-block;border:1px solid red;font-size:25px">标签</span>';  
  build() {  
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center,  
      justifyContent: FlexAlign.Center }) {  
      RichText(this.data)  
        .onStart(() => {  
          console.info('RichText onStart');  
        })  
        .onComplete(() => {  
          console.info('RichText onComplete');  
        })  
        .width(500)  
        .height(500)  
        .backgroundColor(0XBDDB69)  
    }  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
分享
微博
QQ
微信
回复
2024-10-15 18:01:59


相关问题
HarmonyOS List嵌套waterflow滑动
847浏览 • 1回复 待解决
HarmonyOS 页面嵌套滑动
624浏览 • 1回复 待解决
HarmonyOS 页面滑动
695浏览 • 1回复 待解决
关于启动问题首帧分析
1114浏览 • 1回复 待解决
优化还有哪些方案
135浏览 • 0回复 待解决
恭喜您,今日已阅读两篇内容,特奖励+2声望, 快来领取吧。