由于工作原因,一直没时间更新博客,上个礼拜项目上线以后,今天终于可以有时间来总结下上个礼拜做的东西了:环信自定义消息类型,我们先看下效果图
要实现这样的效果,首先我们想到的是要把这些提示消息当做一个消息类型发送给对方,接下来就一步一步的去实现
第一步:自定义消息chatrow,加载的布局根据自己的需求去实现
package com.hyphenate.easeui.widget.chatrow;
import android.content.Context;
import android.text.Spannable;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.R;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.exceptions.HyphenateException;
import android.widget.TextView.BufferType;
//自定义系统消息类型
public class EaseChatRowHint extends EaseChatRow{
private TextView tv_chatcontent;
private ImageView iv_userhead;
public EaseChatRowHint(Context context, EMMessage message, int position, BaseAdapter adapter) {
super(context, message, position, adapter);
}
@Override
protected void onInflateView() {
inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
R.layout.ease_row_received_hint : R.layout.ease_row_sent_hint,this);
}
@Override
protected void onFindViewById() {
tv_chatcontent=findViewById(R.id.tv_chatcontent);
iv_userhead=findViewById(R.id.iv_userhead);
}
@Override
protected void onUpdateView() {
adapter.notifyDataSetChanged();
}
@Override
protected void onSetUpView() {
EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
// 设置内容
tv_chatcontent.setText(span, BufferType.SPANNABLE);
handleTextMessage();
}
protected void handleTextMessage() {
if (message.direct() == EMMessage.Direct.SEND) {
setMessageSendCallback();
switch (message.status()) {
case CREATE:
iv_userhead.setVisibility(GONE);
break;
case SUCCESS:
iv_userhead.setVisibility(GONE);
break;
case FAIL:
iv_userhead.setVisibility(GONE);
break;
case INPROGRESS:
iv_userhead.setVisibility(GONE);
break;
default:
break;
}
}else if (message.direct() == EMMessage.Direct.RECEIVE){
switch (message.status()) {
case CREATE:
iv_userhead.setVisibility(GONE);
break;
case SUCCESS:
iv_userhead.setVisibility(GONE);
break;
case FAIL:
iv_userhead.setVisibility(GONE);
break;
case INPROGRESS:
iv_userhead.setVisibility(GONE);
break;
default:
break;
}
}
}
@Override
protected void onBubbleClick() {
}
}
第二步:我们需要在EaseConstant定义一个常量来区分自己的自定义消息类型
第三步:因为这个自定义消息类型是文字类型的,所以我们要在EaseMessageAdapter中去操作:
(1).定义两个类型
(2)getViewTypeCount中
(3)getItemViewType中因为我们要发送的消息类型为文字消息,所以需要做个判断,如下图所示
(4)createChatRow方法中也需要做判断的
接下来就是要在聊天界面做操作了
到这儿就是所有环信自定义类型的所有步骤了,如果还有不懂的小伙伴可以加我QQ:2298617591