环信自定义消息置顶功能

我在开发中发现没有消息置顶这个功能,然后我公司要求开发个消息置顶的功能,所以就的手动写了。环信好多功能都要自己写,消息撤回,文件夹等等

思路

我就想能不能在每个消息里面设置个标识,来区分是否是置顶的消息,然后我找了下消息实体类EMConversation。其中就有两个方法getExtField()和setExtField(""),反正我也不管这是什么方法发,反正我就能存能取就够了。哈哈。。。

主要方法

1、getExtField();
2、setExtField("")

2017-06-30_151924.png
2017-06-30_152028.png

开始实战

1. 在你要添加的置顶方法,我的是ConversationListFragment类中setUpView()方法中定义的侧滑按钮
 //置顶消息
        EMConversation tobeDeleteCons = (EMConversation)conversationListView.getItemAtPosition(conversationListView.getPositionForView(view));
        String isTop = tobeDeleteCons.getExtField();
            //判断是否是置顶消息  
        if("toTop".equals(isTop)){
           //取消置顶,就设置为其他的字符串
            tobeDeleteCons.setExtField("false");
        }else {
            //把它设置为置顶的标识
            tobeDeleteCons.setExtField("toTop");
        }
        //刷新消息列表
        refresh();
2.找到数据源在添加消息前添加进列表中。我的是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);
        }

到此就改造好了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,144评论 25 709
  • 点击查看原文 Web SDK 开发手册 SDK 概述 网易云信 SDK 为 Web 应用提供一个完善的 IM 系统...
    layjoy阅读 14,766评论 0 15
  • 今天看到桌上闺蜜送的酒,灵感来了。 既然对生活中处处是学问,那我们今天就聊聊酒吧。 作为一个品酒的小白,度娘告诉我...
    彭露_9d7f阅读 2,425评论 0 1
  • "Some people walk in the rain, others just get wet." 天空离我...
    水槛阅读 4,492评论 8 8
  • 1、下得了厨房,上得了厅堂,床上的妖精,床下的淑女 2、无论任何时候想出游就出游说走就走不用担心钱的问题 3、上班...
    清舞飞扬2781阅读 1,720评论 0 0

友情链接更多精彩内容