跳到主要内容

自定义会话列表 provider

会话列表自定义会话展示模板

1. 编写自定义展示模版

继承 BaseConversationItemProvider 自定义会话条目展示模版。

import { BaseConversationItemProvider, BaseUiConversation } from "@rongcloud/imkit";

export class CustomPrivateConversationItemProvider extends BaseConversationItemProvider {
public getConversationWrapBuilder(): WrappedBuilder<[Context, BaseUiConversation, number]> {
return wrapBuilder(bindPrivateConversationMessageData)
}
}

@Builder
export function bindPrivateConversationMessageData(context: Context, baseConversation: BaseUiConversation, position: number) {
CustomPrivateConversationItemView({ conversation: baseConversation });
}


@Component
export struct CustomPrivateConversationItemView {
@ObjectLink conversation: BaseUiConversation;

build() {
this.CustomConversationItemComponent(this.conversation);
}

@Builder
CustomConversationItemComponent(baseConversation: BaseUiConversation) {
// 根据 baseConversation 来渲染UI,下面示范了添加会话头像和会话标题
// 会话头像
Image(this.baseConversation.getPortrait()).height(48).width(48)
// 会话标题
Text(this.baseConversation.getTitle()).fontColor("#C7CCD4").fontSize(16)
}
}

2.设置模版

设置 Kit SDK 内置的指定会话类型的展示模版,需要在会话列表展示前设置。

let conversationListService = RongIM.getInstance().conversationListService()
conversationListService.addConversationItemProvider(ConversationType.Private, new CustomPrivateConversationItemProvider());

3.移除指定会话类型展示模版

支持移除 Kit SDK 内置的指定会话类型的展示模版,需要在会话列表展示前设置。

注意:移除后SDK找不到这个会话类型的展示模版时,会默认返回私聊会话类型的展示模版。

let conversationListService = RongIM.getInstance().conversationListService()
conversationListService.removeConversationItemProvider(ConversationType.Private);