
回复
因日常工作需要使用鸿蒙版腾讯 IM 聊天功能,但是腾讯 IM 只提供了相关接口,并没有像安卓和 iOS 一样提供配套的 UI 页面。这点不得不吐槽一下腾讯,作为互联网巨头鸿蒙开发进度怎么就这么慢,没办法只能对比着微信自己一点点去实现相关功能,本篇文章将详细介绍聊天列表页面的搭建过程,建议点赞收藏!
先看本文的最终实现效果如下:
V2TIMManager.getConversationManager()
.getConversationListByFilter(this.page, this.pageSize)
.then((result: V2TIMConversationResult) => {
})
List() {
LazyForEach(this.dateSource, (item: V2TIMConversation, index: number) => {
ListItem() {
this.ItemLayout(item)
}.swipeAction({
end: {
builder: () => {
this.DeleteView(index, () => {
this.deleteDialog.open()
})
}
}
})
}, (item: V2TIMConversation, index: number) => index + ""+ (item!=undefined&& item.lastMessage!=undefined &&item.lastMessage.timestamp !=undefined?item.lastMessage.timestamp:""))
}
lazyForEach 中的 keyGenerator 必须使用消息里面的时间戳作为唯一的键值,否则会报错。swipeAction 是侧滑删除功能。
当收新联系人发来的信息或已经在列表中的联系人发来的信息时,需要将联系人插入到消息列表头部。
/**
* 会话监听
*/
conversationListener: V2TIMConversationListener = {
onConversationChanged: (conversationList: V2TIMConversation[]) => {
//实时会话
},
};
ConversationTestInterfaces.addConversationListener(this.conversationListener)
let newList = this.dateSource.getDataAll().filter((item, index) => {
return conversationList[0].conversationID == item.conversationID
})
this.dateSource.pushData(conversationList[0])
let index = this.dateSource.getDataAll().indexOf(newList[0])
this.dateSource.deleteData(index)
this.dateSource.pushData(conversationList[0])
if (this.dateSource.getDataAll().length > 1) {
this.dateSource.getDataAll().sort((a, b) => (b!=undefined&& b.lastMessage!=undefined && b.lastMessage.timestamp!=undefined?b.lastMessage.timestamp:0 ) - (a!=undefined&& a.lastMessage!=undefined && a.lastMessage.timestamp!=undefined?a.lastMessage.timestamp:0 ))
}
会话删除就比较简单了,直接根据会话 ID 进行删除即可。
V2TIMManager.getConversationManager()
.deleteConversation(conversation.conversationID)
.then(() => {
//更新列表
})
.catch((error: Error) => {
});
腾讯 IM 功能十分复杂,本篇文章仅仅实现了会话列表功能,所有代码逻辑都需要自己动手处理,鸿蒙版的 IM SDK 只提供了接口,所以在实际实现过程中肯定会遇到很多问题需要自己克服,最终实现和微信相同的功能。下篇文章开始搭建聊天详情功能,需要接入鸿蒙 IM 的小伙伴赶紧收藏起来吧 !