环信自定义消息类型——名片

(转载)https://www.jianshu.com/p/1e23164bafa3

 

名片分享.jpg

近来的需求需要对环信进行定制化,实现如图所示的名片分享功能。
环信聊天中的每一种消息类型都由一种对应的ChatRow来控制,相当于adapter里的Holder。

自定义GroupCardChatRow继承EaseChatRow,在 onBubbleClick()中重写消息的点击事件。

 
  1. public class GroupCardChatRow extends EaseChatRow {

  2.  
  3. private TextView contentView;

  4. private TextView tvGroupName;

  5. private ImageView imgGroup;

  6. private TextView tvIntroduce;

  7. private TextView tvNember;

  8. private String gid;

  9.  
  10. public GroupCardChatRow(Context context, EMMessage message, int position, BaseAdapter adapter) {

  11. super(context, message, position, adapter);

  12. }

  13.  
  14. //接收和发送名片消息的布局

  15. @Override

  16. protected void onInflateView() {

  17. inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?

  18. R.layout.ease_row_received_group_card : R.layout.ease_row_sent_group_card, this);

  19. }

  20.  
  21. @Override

  22. protected void onFindViewById() {

  23. contentView = (TextView) findViewById(R.id.tv_chatcontent);

  24. tvGroupName = (TextView) findViewById(R.id.tv_group_name);

  25. imgGroup = (ImageView) findViewById(R.id.img_group);

  26. tvIntroduce = (TextView) findViewById(tv_introduce);

  27. tvNember = (TextView) findViewById(R.id.tv_number);

  28. }

  29.  
  30. @Override

  31. public void onSetUpView() {

  32.  
  33. try {

  34. Map<String, String> map = new HashMap<>();

  35. map = new Gson().fromJson(message.getStringAttribute(EaseConstant.EXTRA_GROUP_CARD), Map.class);

  36. String name = map.get("name");

  37. String description = map.get("description");

  38. String pic = map.get("pic");

  39. String memberCount = map.get("member_count");

  40. gid = map.get("gid");

  41. tvGroupName.setText(name);

  42. ImageUtil.loadImageWithView(context, pic, imgGroup);

  43. tvIntroduce.setText(description);

  44. tvNember.setText("群成员人数:" + memberCount + "人");

  45.  
  46. } catch (HyphenateException e) {

  47. e.printStackTrace();

  48. }

  49. handleTextMessage();

  50. }

  51.  
  52. protected void handleTextMessage() {

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

  54. setMessageSendCallback();

  55. switch (message.status()) {

  56. case CREATE:

  57. progressBar.setVisibility(View.GONE);

  58. statusView.setVisibility(View.VISIBLE);

  59. break;

  60. case SUCCESS:

  61. progressBar.setVisibility(View.GONE);

  62. statusView.setVisibility(View.GONE);

  63. break;

  64. case FAIL:

  65. progressBar.setVisibility(View.GONE);

  66. statusView.setVisibility(View.VISIBLE);

  67. break;

  68. case INPROGRESS:

  69. progressBar.setVisibility(View.VISIBLE);

  70. statusView.setVisibility(View.GONE);

  71. break;

  72. default:

  73. break;

  74. }

  75. } else {

  76. if (!message.isAcked() && message.getChatType() == ChatType.Chat) {

  77. try {

  78. EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());

  79. } catch (HyphenateException e) {

  80. e.printStackTrace();

  81. }

  82. }

  83. }

  84. }

  85.  
  86. @Override

  87. protected void onUpdateView() {

  88. adapter.notifyDataSetChanged();

  89. }

  90.  
  91. @Override

  92. protected void onBubbleClick() {

  93. // TODO Auto-generated method stub

  94.  
  95. Intent intent = new Intent(context, GroupCardActivity.class);

  96. intent.putExtra("gid", gid);

  97. context.startActivity(intent);

  98.  
  99.  
  100. }

  101. }

  102.  
  103.  

环信中消息类型包括txt、音频、位置、文件、图片、视频类型,群名片也属于txt类型,我们通过message的拓展字段来区分消息类型是否为名片。

发送群名片消息的时候给message添加群名片拓展字段,发送正常txt消息时不添加拓展字段,在adapter中根据拓展字段是否为空来判断消息类型

 
  1.  
  2. protected void sendMessage(UserPageBean userPageBean) {

  3.  
  4. EMMessage message = EMMessage.createTxtSendMessage("群邀请", userPageBean.getIm_namelogin());

  5.  
  6. message.setAttribute(EaseConstant.EXTRA_GROUP_CARD, groupMessage);

  7. EMClient.getInstance().chatManager().sendMessage(message);

  8.  
  9. }

  10.  

在EaseMessageAdapter中新添加两种消息类型
MESSAGE_TYPE_SENT_GROUP_CARD 、 MESSAGE_TYPE_RECV_GROUP_CARD

修改 getItemViewType和createChatRow方法,在其中添加这两种消息类型

 
  1. public class EaseMessageAdapter extends BaseAdapter {

  2.  
  3. private final static String TAG = "msg";

  4.  
  5. private Context context;

  6. private static final int HANDLER_MESSAGE_REFRESH_LIST = 0;

  7. private static final int HANDLER_MESSAGE_SELECT_LAST = 1;

  8. private static final int HANDLER_MESSAGE_SEEK_TO = 2;

  9.  
  10. private static final int MESSAGE_TYPE_RECV_TXT = 0;

  11. private static final int MESSAGE_TYPE_SENT_TXT = 1;

  12. private static final int MESSAGE_TYPE_SENT_IMAGE = 2;

  13. private static final int MESSAGE_TYPE_SENT_LOCATION = 3;

  14. private static final int MESSAGE_TYPE_RECV_LOCATION = 4;

  15. private static final int MESSAGE_TYPE_RECV_IMAGE = 5;

  16. private static final int MESSAGE_TYPE_SENT_VOICE = 6;

  17. private static final int MESSAGE_TYPE_RECV_VOICE = 7;

  18. private static final int MESSAGE_TYPE_SENT_VIDEO = 8;

  19. private static final int MESSAGE_TYPE_RECV_VIDEO = 9;

  20. private static final int MESSAGE_TYPE_SENT_FILE = 10;

  21. private static final int MESSAGE_TYPE_RECV_FILE = 11;

  22. private static final int MESSAGE_TYPE_SENT_EXPRESSION = 12;

  23. private static final int MESSAGE_TYPE_RECV_EXPRESSION = 13;

  24. private static final int MESSAGE_TYPE_SENT_GROUP_CARD = 14;

  25. private static final int MESSAGE_TYPE_RECV_GROUP_CARD = 15;

  26.  
  27.  
  28.  
  29. public int itemTypeCount;

  30.  
  31. // reference to conversation object in chatsdk

  32. private EMConversation conversation;

  33. EMMessage[] messages = null;

  34.  
  35. private String toChatUsername;

  36. private boolean isTrueName;

  37. private boolean isBidTureName;

  38.  
  39. private MessageListItemClickListener itemClickListener;

  40. private EaseCustomChatRowProvider customRowProvider;

  41.  
  42. private boolean showUserNick;

  43. private boolean showAvatar;

  44. private Drawable myBubbleBg;

  45. private Drawable otherBuddleBg;

  46. private String orderId;

  47. private String type;

  48.  
  49. private ListView listView;

  50. private boolean isbidded;

  51. private boolean isbiddedMessage;

  52.  
  53. public EaseMessageAdapter(Context context, String username, int chatType, ListView listView, boolean isTrueName, String orderId, boolean isBidTureName, String type, boolean isBidded) {

  54. this.context = context;

  55. this.listView = listView;

  56. toChatUsername = username;

  57. this.isTrueName = isTrueName;

  58. this.orderId = orderId;

  59. this.isBidTureName = isBidTureName;

  60. this.type = type;

  61. this.conversation = EMClient.getInstance().chatManager().getConversation(username, EaseCommonUtils.getConversationType(chatType), true);

  62. this.isbidded = isBidded;

  63. }

  64.  
  65. List<EMMessage> msgs1;

  66. Handler handler = new Handler() {

  67. private void refreshList() {

  68. // you should not call getAllMessages() in UI thread

  69. // otherwise there is problem when refreshing UI and there is new message arrive

  70. List<EMMessage> msgs = conversation.getAllMessages();

  71.  
  72. List<String> friendsMessageList = new ArrayList<>();

  73. List<String> orderUnBidMessageList = new ArrayList<>();

  74. List<String> orderBidMessageList = new ArrayList<>();

  75. String msgId;

  76. for (int i = 0; i < msgs.size(); i++) {

  77. msgId = msgs.get(i).getMsgId();

  78. String msgOrderId = null;

  79.  
  80. // try {

  81. msgOrderId = (String) msgs.get(i).ext().get(EaseConstant.EXTRA_ORDER_ID);

  82. isbiddedMessage = (boolean) msgs.get(i).ext().get(EaseConstant.EXTRA_IS_BIDDED);

  83.  
  84. // msgOrderId = msgs.get(i).getStringAttribute(EaseConstant.EXTRA_ORDER_ID);

  85. // } catch (HyphenateException e) {

  86. // e.printStackTrace();

  87. // }

  88.  
  89. if (msgOrderId == null || msgOrderId.equals("")) {

  90. friendsMessageList.add(msgId);

  91. } else if (msgOrderId.equals(orderId)) {

  92. if (isbidded){

  93. if (isbiddedMessage){

  94. orderBidMessageList.add(msgId);

  95. }

  96. }else {

  97. if (!isbiddedMessage){

  98. orderUnBidMessageList.add(msgId);

  99. }

  100. }

  101.  
  102. }

  103.  
  104. }

  105.  
  106. if (orderId == null) {

  107. msgs1 = conversation.loadMessages(friendsMessageList);

  108. } else {

  109. if (isbidded){

  110. msgs1 = conversation.loadMessages(orderBidMessageList);

  111. }else {

  112. msgs1 = conversation.loadMessages(orderUnBidMessageList);

  113. }

  114. }

  115.  
  116.  
  117. messages = msgs1.toArray(new EMMessage[msgs1.size()]);

  118. conversation.markAllMessagesAsRead();

  119. notifyDataSetChanged();

  120.  
  121.  
  122. }

  123.  
  124.  
  125. @Override

  126. public void handleMessage(android.os.Message message) {

  127. switch (message.what) {

  128. case HANDLER_MESSAGE_REFRESH_LIST:

  129. refreshList();

  130. break;

  131. case HANDLER_MESSAGE_SELECT_LAST:

  132. if (messages.length > 0) {

  133. listView.setSelection(messages.length - 1);

  134. }

  135. break;

  136. case HANDLER_MESSAGE_SEEK_TO:

  137. int position = message.arg1;

  138. listView.setSelection(position);

  139. break;

  140. default:

  141. break;

  142. }

  143. }

  144. };

  145.  
  146. public void refresh() {

  147. if (handler.hasMessages(HANDLER_MESSAGE_REFRESH_LIST)) {

  148. return;

  149. }

  150. android.os.Message msg = handler.obtainMessage(HANDLER_MESSAGE_REFRESH_LIST);

  151. handler.sendMessage(msg);

  152.  
  153. }

  154.  
  155. /**

  156. * refresh and select the last

  157. */

  158. public void refreshSelectLast() {

  159. final int TIME_DELAY_REFRESH_SELECT_LAST = 100;

  160. handler.removeMessages(HANDLER_MESSAGE_REFRESH_LIST);

  161. handler.removeMessages(HANDLER_MESSAGE_SELECT_LAST);

  162. handler.sendEmptyMessageDelayed(HANDLER_MESSAGE_REFRESH_LIST, TIME_DELAY_REFRESH_SELECT_LAST);

  163. handler.sendEmptyMessageDelayed(HANDLER_MESSAGE_SELECT_LAST, TIME_DELAY_REFRESH_SELECT_LAST);

  164. }

  165.  
  166. /**

  167. * refresh and seek to the position

  168. */

  169. public void refreshSeekTo(int position) {

  170. handler.sendMessage(handler.obtainMessage(HANDLER_MESSAGE_REFRESH_LIST));

  171. android.os.Message msg = handler.obtainMessage(HANDLER_MESSAGE_SEEK_TO);

  172. msg.arg1 = position;

  173. handler.sendMessage(msg);

  174. }

  175.  
  176.  
  177. public EMMessage getItem(int position) {

  178. if (messages != null && position < messages.length) {

  179. return messages[position];

  180. }

  181. return null;

  182. }

  183.  
  184. public long getItemId(int position) {

  185. return position;

  186. }

  187.  
  188. /**

  189. * get count of messages

  190. */

  191. public int getCount() {

  192. return messages == null ? 0 : messages.length;

  193. }

  194.  
  195. /**

  196. * get number of message type, here 14 = (EMMessage.Type) * 2

  197. */

  198. public int getViewTypeCount() {

  199. if (customRowProvider != null && customRowProvider.getCustomChatRowTypeCount() > 0) {

  200. return customRowProvider.getCustomChatRowTypeCount() + 16;

  201. }

  202. return 16;

  203. }

  204.  
  205.  
  206. /**

  207. * get type of item

  208. */

  209. public int getItemViewType(int position) {

  210. EMMessage message = getItem(position);

  211. if (message == null) {

  212. return -1;

  213. }

  214.  
  215. if (customRowProvider != null && customRowProvider.getCustomChatRowType(message) > 0) {

  216. return customRowProvider.getCustomChatRowType(message) + 13;

  217. }

  218.  
  219. if (message.getType() == EMMessage.Type.TXT) {

  220. if (message.getBooleanAttribute(EaseConstant.MESSAGE_ATTR_IS_BIG_EXPRESSION, false)) {

  221. return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_EXPRESSION : MESSAGE_TYPE_SENT_EXPRESSION;

  222. }

  223.  
  224. try {

  225. if (!TextUtils.isEmpty(message.getStringAttribute(EaseConstant.EXTRA_GROUP_CARD))){

  226. return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_GROUP_CARD : MESSAGE_TYPE_SENT_GROUP_CARD;

  227. }

  228. } catch (HyphenateException e) {

  229. e.printStackTrace();

  230. }

  231. return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_TXT : MESSAGE_TYPE_SENT_TXT;

  232. }

  233. if (message.getType() == EMMessage.Type.IMAGE) {

  234. return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_IMAGE : MESSAGE_TYPE_SENT_IMAGE;

  235.  
  236. }

  237. if (message.getType() == EMMessage.Type.LOCATION) {

  238. return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_LOCATION : MESSAGE_TYPE_SENT_LOCATION;

  239. }

  240. if (message.getType() == EMMessage.Type.VOICE) {

  241. return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_VOICE : MESSAGE_TYPE_SENT_VOICE;

  242. }

  243. if (message.getType() == EMMessage.Type.VIDEO) {

  244. return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_VIDEO : MESSAGE_TYPE_SENT_VIDEO;

  245. }

  246. if (message.getType() == EMMessage.Type.FILE) {

  247. return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_FILE : MESSAGE_TYPE_SENT_FILE;

  248. }

  249.  
  250. return -1;// invalid

  251. }

  252.  
  253. protected EaseChatRow createChatRow(Context context, EMMessage message, int position) {

  254. EaseChatRow chatRow = null;

  255. if (customRowProvider != null && customRowProvider.getCustomChatRow(message, position, this) != null) {

  256. return customRowProvider.getCustomChatRow(message, position, this);

  257. }

  258. message.ext().put(EaseConstant.EXTRA_IS_TRUENAME, isTrueName);

  259. switch (message.getType()) {

  260. case TXT:

  261. if (message.getBooleanAttribute(EaseConstant.MESSAGE_ATTR_IS_BIG_EXPRESSION, false)) {

  262. chatRow = new EaseChatRowBigExpression(context, message, position, this);

  263. } else {

  264.  
  265. try {

  266. if (TextUtils.isEmpty(message.getStringAttribute(EaseConstant.EXTRA_GROUP_CARD))) {

  267. chatRow = new EaseChatRowText(context, message, position, this);

  268. }else {

  269. chatRow = new GroupCardChatRow(context, message, position, this);

  270. }

  271. } catch (HyphenateException e) {

  272. e.printStackTrace();

  273. chatRow = new EaseChatRowText(context, message, position, this);

  274. }

  275. }

  276. break;

  277. case LOCATION:

  278. chatRow = new EaseChatRowLocation(context, message, position, this);

  279. break;

  280. case FILE:

  281. chatRow = new EaseChatRowFile(context, message, position, this);

  282. break;

  283. case IMAGE:

  284. chatRow = new EaseChatRowImage(context, message, position, this);

  285. break;

  286. case VOICE:

  287. chatRow = new EaseChatRowVoice(context, message, position, this);

  288. break;

  289. case VIDEO:

  290. chatRow = new EaseChatRowVideo(context, message, position, this);

  291. break;

  292. default:

  293. break;

  294. }

  295.  
  296. return chatRow;

  297. }

  298.  
  299.  
  300. @SuppressLint("NewApi")

  301. public View getView(final int position, View convertView, ViewGroup parent) {

  302. EMMessage message = getItem(position);

  303. if (convertView == null) {

  304. convertView = createChatRow(context, message, position);

  305. }

  306.  
  307. //refresh ui with messages

  308. ((EaseChatRow) convertView).setUpView(message, position, itemClickListener, isTrueName, isBidTureName, type);

  309.  
  310. return convertView;

  311. }

  312.  
  313.  
  314. public String getToChatUsername() {

  315. return toChatUsername;

  316. }

  317.  
  318.  
  319. public void setShowUserNick(boolean showUserNick) {

  320. this.showUserNick = showUserNick;

  321. }

  322.  
  323.  
  324. public void setShowAvatar(boolean showAvatar) {

  325. this.showAvatar = showAvatar;

  326. }

  327.  
  328.  
  329. public void setMyBubbleBg(Drawable myBubbleBg) {

  330. this.myBubbleBg = myBubbleBg;

  331. }

  332.  
  333.  
  334. public void setOtherBuddleBg(Drawable otherBuddleBg) {

  335. this.otherBuddleBg = otherBuddleBg;

  336. }

  337.  
  338.  
  339. public void setItemClickListener(MessageListItemClickListener listener) {

  340. itemClickListener = listener;

  341. }

  342.  
  343. public void setCustomChatRowProvider(EaseCustomChatRowProvider rowProvider) {

  344. customRowProvider = rowProvider;

  345. }

  346.  
  347.  
  348. public boolean isShowUserNick() {

  349. return showUserNick;

  350. }

  351.  
  352.  
  353. public boolean isShowAvatar() {

  354. return showAvatar;

  355. }

  356.  
  357.  
  358. public Drawable getMyBubbleBg() {

  359. return myBubbleBg;

  360. }

  361.  
  362.  
  363. public Drawable getOtherBuddleBg() {

  364. return otherBuddleBg;

  365. }

  366.  
  367. }

  368.  
  369.  

 

//--------------------------------------------------------------------------------

新版本的sdk有些变化,但是大部分还是可以参考下。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值