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 ListmemberList = new ArrayList<>(); }
2.环信自己也说了环信不保存项目的用户信息,所以这里我们需要在自己的项目里处理。
如果是一对一聊天,我们就获取好友列表(网络请求),并赋值:
ListmemberList = 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;
如果是群组聊天,我们就获取该群组的成员列表(网络请求),并赋值:
ListmemberList = 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 avatarif(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); }}
三、总结 这种解决方法我认为非常的棒,有木有,扩张性高,对任意项目都适用,而且,不会影响原有功能!
今天的分享就到这里,感谢阅读!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
推荐阅读
- flutter实现底部导航栏
- JAVA实现发送短信
- Android Studio中的线性布局(LinearLayout)
- Android Studio 的帧布局(Framelayout)
- 程序员|Android 这座山,我一定要爬到顶
- android|Android Studio开发毕业设计
- Android|Android 12(S) 图形显示系统 - BufferQueue的工作流程(十一)
- Java多线程【三种实现方法】
- Verilog|IIC总线协议Verilog实现
- Linux从系统到网络|socket(套接字)实现udp通信