Android实现环信修改头像和昵称
作者:风云正
这篇文章主要为大家详细介绍了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<EaseMember> memberList = new ArrayList<>(); }
2.环信自己也说了环信不保存项目的用户信息,所以这里我们需要在自己的项目里处理。
如果是一对一聊天,我们就获取好友列表(网络请求),并赋值:
List<EaseMember> 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<EaseMember> 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()方法中将
修改成:
//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); } }
三、总结
这种解决方法我认为非常的棒,有木有,扩张性高,对任意项目都适用,而且,不会影响原有功能!
今天的分享就到这里,感谢阅读!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。