在做环信聊天的时候,大家很有可能就把环信整个demo丢进自家工程里面了,的确是个非常省事的方法。但是呢,接下去看环信写的代码就要你花点时间了。本篇主要介绍了如何花最短的时间来解决头像以及昵称的问题。前提是你已经将工程导入项目,并接入了聊天界面以及会话列表。

方案就四个字:消息扩展(扩展字段,iOS & Android请保持一致,不然么法玩)

用到的地方

聊天界面里。

会话列表。

步骤

聊天列表点击进去把扩展信息传到聊天界面去

1.ConversationListFragment文件中找到setUpView方法,聊天列表的点击事件中加入以下代码

conversationListView.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView> parent, View view, int position, long id) {

EMConversation conversation = conversationListView.getItem(position);

String username = conversation.getUserName();

if (username.equals(EMClient.getInstance().getCurrentUser()))

Toast.makeText(getActivity(), R.string.Cant_chat_with_yourself, Toast.LENGTH_SHORT).show();

else {

// start chat acitivity

Intent intent = new Intent(getActivity(), ChatActivity.class);

if (conversation.isGroup()) {

if (conversation.getType() == EMConversationType.ChatRoom) {

// it's group chat

intent.putExtra(Constant.EXTRA_CHAT_TYPE, Constant.CHATTYPE_CHATROOM);

} else {

intent.putExtra(Constant.EXTRA_CHAT_TYPE, Constant.CHATTYPE_GROUP);

}

}

// it's single chat

intent.putExtra(Constant.EXTRA_USER_ID, username);

//这个人和你聊天的最后一条消息里面看是谁发的。判断方法就是取出这条消息发的人的环信id与你自己本地存储的环信id做匹配。是你发的与不是你发的,传入的值正好相反。

if (!conversation.getLastMessage().getFrom().equals(SPUtil.getHuanxinid(getContext()))) {

intent.putExtra("to_headportrait", conversation.getLastMessage().getStringAttribute("from_headportrait", ""));

intent.putExtra("to_username", conversation.getLastMessage().getStringAttribute("from_username", ""));

intent.putExtra("to_user_id", conversation.getLastMessage().getStringAttribute("from_user_id", ""));

} else {

intent.putExtra("to_headportrait", conversation.getLastMessage().getStringAttribute("to_headportrait", ""));

intent.putExtra("to_username", conversation.getLastMessage().getStringAttribute("to_username", ""));

intent.putExtra("to_user_id", conversation.getLastMessage().getStringAttribute("to_user_id", ""));

}

startActivity(intent);

}

}

});

将扩展信息加入进消息中。

2.全局搜onSetMessageAttributes方法,(在ChatFragment文件中)添加以下代码

try {

//我的信息,一般本地自己取出来

message.setAttribute("from_user_id", SPUtil.getUser(getContext()).getUser_id());

message.setAttribute("from_headportrait", SPUtil.getUser(getContext()).getHeadportrait());

message.setAttribute("from_username", SPUtil.getUser(getContext()).getUsername());

//对方的信息,一般上个界面传值传过来

message.setAttribute("to_user_id", getArguments().getString("to_user_id", ""));

message.setAttribute("to_headportrait", getArguments().getString("to_headportrait", ""));

message.setAttribute("to_username", getArguments().getString("to_username", ""));

} catch (Exception e) {

}

聊天列表的item项中根据消息中携带的信息来改变头像昵称

3.EaseConversationAdapter这个文件,找到 EaseUserUtils.setUserAvatar(getContext(), username, holder.avatar); EaseUserUtils.setUserNick(username, holder.name);,把这两行干掉,添加以下代码。还是根据这条消息是谁发的来做判断具体要加的图片与头像。

if (conversation.getLastMessage().getFrom().equals(SPUtil.getUser(getContext()).getHuanxinid())) {

Glide.with(getContext()).load(conversation.getLastMessage().getStringAttribute("to_headportrait", "")).placeholder(R.mipmap.zwf_one).transform(new GlideCircleTransform(getContext())).into(holder.avatar);

holder.name.setText(conversation.getLastMessage().getStringAttribute("to_username", ""));

} else {

Glide.with(getContext()).load(conversation.getLastMessage().getStringAttribute("from_headportrait", "")).placeholder(R.mipmap.zwf_one).transform(new GlideCircleTransform(getContext())).into(holder.avatar);

holder.name.setText(conversation.getLastMessage().getStringAttribute("from_username", ""));

}

聊天界面的头像也要处理一下

4.EaseChatRow这个文件,setUpBaseView方法里面找到以下代码,覆盖之。

//set nickname and avatar

if(message.direct() == Direct.SEND){

Glide.with(context).load(SPUtil.getHeadportrait(context)).centerCrop().transform(new GlideCircleTransform(getContext())).placeholder(R.mipmap.zwf_one).into(userAvatarView);

}else{

Glide.with(context).load(message.getStringAttribute("from_headportrait","")).centerCrop().transform(new GlideCircleTransform(getContext())).placeholder(R.mipmap.zwf_one).into(userAvatarView);

usernickView.setText(message.getStringAttribute("from_username",""));

}

到此,基本的显示头像昵称就应该可以了。这边图片加载用了Glide,头像是圆的,如果你项目中是其他图片加载框架自行替换吧。恩,五黑去了!

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐