环信需要发送名片的时候,首先在需要发送的地方new处这个message
EMMessage message = EMMessage.createTxtSendMessage(name, user_id);
name为名片的content,后面的user_id为要发送的对方的用户名
message.setAttribute(HxOutMessage.myExtType,"mingpianType");里面带的内容为需要发送的信息单人的不需要设置,如果是群聊或者聊天室,需要设置message.setChatType(EMMessage.ChatType.GroupChat);
发送消息EMClient.getInstance().chatManager().sendMessage(message);
发送完消息,跳转到对方的聊天页面intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE,EaseConstant.CHATTYPE_GROUP); intent.putExtra(EaseConstant.EXTRA_USER_ID, bean.getCrowd_id()); startActivity(intent);发送完消息以后,需要接受,在EaseMessageAdapter这个类里面接受
新建两个int的标记
private static final int MESSAGE_TYPE_RECV_MINGPIAN_CALL=14; private static final int MESSAGE_TYPE_SENT_MINGPIAN_CALL=15;在getviewtypecount的方法里面public int getViewTypeCount() { if(customRowProvider != null && customRowProvider.getCustomChatRowTypeCount() > 0){ return customRowProvider.getCustomChatRowTypeCount() + 16;//原来为14,加两个 } return 16; }在getitemviewtype的方法里面添加if (!TextUtils.isEmpty(message.getStringAttribute(HxOutMessage.myExtType,null))){ return message.direct()== EMMessage.Direct.SEND ?MESSAGE_TYPE_SENT_MINGPIAN_CALL :MESSAGE_TYPE_RECV_MINGPIAN_CALL; }在createChatRow的这个方法里面添加在switch方法里面选择TXT的选择里面case TXT: if(message.getBooleanAttribute(EaseConstant.MESSAGE_ATTR_IS_BIG_EXPRESSION, false)){ chatRow = new EaseChatRowBigExpression(context, message, position, this); }else{ if (!TextUtils.isEmpty(message.getStringAttribute(HxOutMessage.myExtType,null))){ chatRow=new MingPianChatRow(context,message,position,this);//MingPianChatRow为自己新建的EaseChatRow,自己想要发出的消息是什么样,自己去xml里面写页面 }else { chatRow = new EaseChatRowText(context, message, position, this);} } break;自己新建一个实体类,用来存放名片的信息public class HxOutMessage { public static final String myExtType="myExtType"; public static final String id="19"; public static final String name="小胡子"; public static final String autho="https://www.baidu.com/img/bd_logo1.png"; public static final String num="10"; }最后是自己新建的MingPianChatRow
public class MingPianChatRow extends EaseChatRow { private ImageView autho_im; private TextView name_tx; private TextView id_tx; private String message_id; private String from; private TextView type_tx; private String name; private String id; private String content; private String autho; private String active_id; private String shareid; public MingPianChatRow(Context context, EMMessage message, int position, BaseAdapter adapter) { super(context, message, position, adapter); } @Override protected void onInflateView() { if (!TextUtils.isEmpty(message.getStringAttribute(HxOutMessage.myExtType,null))){ inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ? R.layout.ease_chat_mingpian_recive : R.layout.ease_chat_mingpian_send, this); } } @Override protected void onFindViewById() { autho_im = ((ImageView) findViewById(R.id.autho_person)); name_tx = ((TextView) findViewById(R.id.tx_name_person)); id_tx = ((TextView) findViewById(R.id.id_person)); type_tx = ((TextView) findViewById(R.id.from_share)); } @Override protected void onUpdateView() { } @Override protected void onSetUpView() {//接受message,显示名片的内容 EMTextMessageBody body = (EMTextMessageBody) message.getBody(); if (message.direct()==EMMessage.Direct.RECEIVE){ name_tx.setText(body.getMessage()); } if (message.getBooleanAttribute("mingpianType",true)){ message.getStringAttribute(HxOutMessage.myExtType,null); message_id = message.getStringAttribute(HxOutMessage.id,null); name = message.getStringAttribute(HxOutMessage.name, null); autho = message.getStringAttribute(HxOutMessage.autho, null); content = message.getStringAttribute("content",null); from = message.getStringAttribute("from",null); id = message.getStringAttribute("goodid",null); active_id = message.getStringAttribute("active_id","0"); shareid = message.getStringAttribute("shareid","0"); } } @Override protected void onBubbleClick() { //名片的点击事件在这里写 } }//ease_chat_mingpian_recive这个xml页面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:padding="13dp" android:layout_height="match_parent"> <TextView android:id="@+id/timestamp" style="@style/chat_text_date_style" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/margin_chat_activity" > <ImageView android:id="@+id/iv_userhead" android:layout_width="@dimen/size_avatar" android:layout_height="@dimen/size_avatar" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="@dimen/margin_chat_activity" android:scaleType="fitXY" /> <RelativeLayout android:id="@+id/bubble" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_userid" android:layout_marginLeft="@dimen/margin_chat_activity" android:layout_toRightOf="@id/iv_userhead" android:background="@drawable/ease_chatfrom_bg" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/autho_person" android:layout_width="50dp" android:layout_height="50dp" android:layout_margin="10dp" android:scaleType="centerCrop" android:src="@mipmap/b" /> <TextView android:id="@+id/tx_name_person" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/autho_person" android:text="name" android:textColor="@color/text_color_b" android:textSize="15sp" /> <TextView android:id="@+id/id_person" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@id/autho_person" android:layout_marginLeft="10dp" android:layout_toRightOf="@id/autho_person" android:text="id" android:textColor="@color/text_color_b" android:textSize="15sp" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:id="@+id/line" android:layout_below="@id/autho_person" android:background="@color/line_color"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/line" android:textSize="12sp" android:id="@+id/from_share" android:textColor="@color/text_color_w" android:layout_margin="10dp" /> </RelativeLayout> </RelativeLayout> <TextView android:id="@+id/tv_userid" style="@style/chat_text_name_style" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/chat_nick_margin_left" android:textSize="@dimen/chat_nick_text_size" android:layout_toRightOf="@id/iv_userhead" android:visibility="gone" /> </RelativeLayout> </LinearLayout>完事,就这么写,哈哈