我在開發(fā)中發(fā)現(xiàn)沒有消息置頂這個功能,然后我公司要求開發(fā)個消息置頂?shù)墓δ苡尘牛跃偷氖謩訉懥恕-h(huán)信好多功能都要自己寫,消息撤回绽诚,文件夾等等
思路
我就想能不能在每個消息里面設(shè)置個標(biāo)識,來區(qū)分是否是置頂?shù)南ⅲ缓笪艺伊讼孪嶓w類EMConversation憔购。其中就有兩個方法getExtField()和setExtField("")宫峦,反正我也不管這是什么方法發(fā),反正我就能存能取就夠了玫鸟。哈哈导绷。。屎飘。
主要方法
1妥曲、getExtField();
2、setExtField("")
2017-06-30_151924.png
2017-06-30_152028.png
開始實戰(zhàn)
1. 在你要添加的置頂方法钦购,我的是ConversationListFragment類中setUpView()方法中定義的側(cè)滑按鈕
//置頂消息
EMConversation tobeDeleteCons = (EMConversation)conversationListView.getItemAtPosition(conversationListView.getPositionForView(view));
String isTop = tobeDeleteCons.getExtField();
//判斷是否是置頂消息
if("toTop".equals(isTop)){
//取消置頂檐盟,就設(shè)置為其他的字符串
tobeDeleteCons.setExtField("false");
}else {
//把它設(shè)置為置頂?shù)臉?biāo)識
tobeDeleteCons.setExtField("toTop");
}
//刷新消息列表
refresh();
2.找到數(shù)據(jù)源在添加消息前添加進(jìn)列表中。我的是EaseConversationListFragment中setUpView()方法押桃,
conversationList.addAll(loadConversationList());
conversationListView.init(conversationList);
這個是主要方法
/**
* load conversation list
*
* @return
+ */
protected List<EMConversation> loadConversationList(){
// get all conversations
Map<String, EMConversation> conversations = EMClient.getInstance().chatManager().getAllConversations();
onHandleEMConversation(conversations);
//添加置頂消息
List<Pair<Long, EMConversation>> topList = new ArrayList<Pair<Long, EMConversation>>();
synchronized (conversations) {
for (EMConversation conversation : conversations.values()) {
if (conversation.getAllMessages().size() != 0 && conversation.getExtField().equals("toTop")) {
topList.add(new Pair<Long, EMConversation>(conversation.getLastMessage().getMsgTime(), conversation));
}
}
}
List<Pair<Long, EMConversation>> sortList = new ArrayList<Pair<Long, EMConversation>>();
/**
* lastMsgTime will change if there is new message during sorting
* so use synchronized to make sure timestamp of last message won't change.
*/
synchronized (conversations) {
for (EMConversation conversation : conversations.values()) {
if (conversation.getAllMessages().size() != 0 && !conversation.getExtField().equals("toTop")) {
sortList.add(new Pair<Long, EMConversation>(conversation.getLastMessage().getMsgTime(), conversation));
}
}
}
try {
// Internal is TimSort algorithm, has bug
sortConversationByLastChatTime(sortList);
} catch (Exception e) {
e.printStackTrace();
}
List<EMConversation> list = new ArrayList<EMConversation>();
//添加置頂消息
for (Pair<Long, EMConversation> topItem : topList) {
list.add(topItem.second);
}
for (Pair<Long, EMConversation> sortItem : sortList) {
list.add(sortItem.second);
}
return list;
}
3.在EaseConversationAdapter中加上一些信息
EMConversation conversation = getItem(position);
//判斷是否置頂葵萎,置頂改變文字
String isTop = conversation.getExtField();
if("toTop".equals(isTop)){
holder.btnToTop.setText("取消置頂");
holder.list_itease_layout.setBackgroundResource(R.drawable.ease_mm_top_listitem);
}else {
holder.btnToTop.setText("置頂");
holder.list_itease_layout.setBackgroundResource(R.drawable.ease_mm_listitem);
}
到此就改造好了