Android环信修改头像和昵称

一、概述

最近在处理环信聊天时,需要修改聊天页面的用户昵称和头像,在网上搜索解决方案,发现没有合适,要么看不懂,要么代码太复杂,没办法,只能发挥自己的聪明才智动手解决了。
项目里的环信是基于V3.x的,项目里的聊天包括单聊和群组聊天,所以要同时兼顾这两种模式。

二、解决方法

1.首先我们在easyui库里添加两个类:

package com.hyphenate.easeui.model;

/**
 * author: chenzheng
 * created on: 2017/7/26 17:11
 * description:
 */

public class EaseMember {

    public String member_hxid;
    public String member_headphoto;
    public String member_nickname;
}
package com.hyphenate.easeui.utils;

import com.hyphenate.easeui.model.EaseMember;

import java.util.ArrayList;
import java.util.List;

/**
 * author: chenzheng
 * created on: 2017/7/26 17:04
 * description:
 */

public class EaseGlobal {

    public static List memberList = new ArrayList<>();
}

2.环信自己也说了环信不保存项目的用户信息,所以这里我们需要在自己的项目里处理。
如果是一对一聊天,我们就获取好友列表(网络请求),并赋值:

List memberList = new ArrayList<>();
                                for(HxContact hc : contactList){
                                    EaseMember em = new EaseMember();
                                    em.member_hxid = hc.username;
                                    em.member_headphoto = Api.IMAGE_DOMAIN_URL+hc.headPhoto;
                                    em.member_nickname = hc.name;
                                    memberList.add(em);
                                }
                                //添加自己的头像和昵称信息
                                EaseMember em2 = new EaseMember();
                                em2.member_hxid = Global.userData.user.hxUserName;
                                em2.member_headphoto = Api.IMAGE_DOMAIN_URL+Global.userData.user.headPhoto;
                                em2.member_nickname = Global.userData.user.loginName;
                                memberList.add(em2);
                                EaseGlobal.memberList = memberList;

如果是群组聊天,我们就获取该群组的成员列表(网络请求),并赋值:

List memberList = new ArrayList<>();
                                for(TeamMember tm : groupMemberList){
                                    EaseMember em = new EaseMember();
                                    em.member_hxid = tm.chatUsername;
                                    em.member_headphoto = Api.IMAGE_DOMAIN_URL+tm.headPhoto;
                                    em.member_nickname = tm.mgName;
                                    memberList.add(em);
                                }
                                //添加自己的头像和昵称信息
                                EaseMember em2 = new EaseMember();
                                em2.member_hxid = Global.userData.user.hxUserName;
                                em2.member_headphoto = Api.IMAGE_DOMAIN_URL+Global.userData.user.headPhoto;
                                em2.member_nickname = Global.userData.user.loginName;
                                memberList.add(em2);
                                EaseGlobal.memberList = memberList;

3.最后一步,离成功一步之遥啦!找到EaseChatRow类,在setUpBaseView()方法中将

Android环信修改头像和昵称_第1张图片

修改成:

//set nickname and avatar
        if(EaseGlobal.memberList.size()>0){
            if (message.direct() == Direct.SEND) {
                for(EaseMember em : EaseGlobal.memberList){
                    if(em.member_hxid.equals(EMClient.getInstance().getCurrentUser())){
                        Glide.with(context).load(em.member_headphoto).into(userAvatarView);
                        break;
                    }
                }
            } else {
                for(EaseMember em : EaseGlobal.memberList){
                    if(em.member_hxid.equals(message.getFrom())){
                        Glide.with(context).load(em.member_headphoto).into(userAvatarView);
                        usernickView.setText(em.member_nickname);
                        break;
                    }
                }
            }
        }else {
            if (message.direct() == Direct.SEND) {
                EaseUserUtils.setUserAvatar(context, EMClient.getInstance().getCurrentUser(), userAvatarView);
            } else {
                EaseUserUtils.setUserAvatar(context, message.getFrom(), userAvatarView);
                EaseUserUtils.setUserNick(message.getFrom(), usernickView);
            }
        }

三、总结

这种解决方法我认为非常的棒,有木有,扩张性高,对任意项目都适用,而且,不会影响原有功能!
今天的分享就到这里,感谢阅读!

你可能感兴趣的:(Android实战案例)