登录 立即注册
安币:

安卓巴士 - 安卓开发 - Android开发 - 安卓 - 移动互联网门户

查看: 818|回复: 5

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

[复制链接]

303

主题

635

帖子

694

安币

手工艺人

发表于 2017-11-15 15:52:50 | 显示全部楼层 |阅读模式

                  
        

        名片分享.jpg
        

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


        

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


        
[Java] 查看源文件 复制代码
public class GroupCardChatRow extends EaseChatRow {
            private TextView contentView;
            private TextView tvGroupName;
            private ImageView imgGroup;
            private TextView tvIntroduce;
            private TextView tvNember;
            private String gid;
            public GroupCardChatRow(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_group_card : R.layout.ease_row_sent_group_card, this);
            }
            @Override
            protected void onFindViewById() {
                contentView = (TextView) findViewById(R.id.tv_chatcontent);
                tvGroupName = (TextView) findViewById(R.id.tv_group_name);
                imgGroup = (ImageView) findViewById(R.id.img_group);
                tvIntroduce = (TextView) findViewById(tv_introduce);
                tvNember = (TextView) findViewById(R.id.tv_number);
            }
            @Override
            public void onSetUpView() {
                try {
                    Map<String, String> map = new HashMap<>();
                    map = new Gson().fromJson(message.getStringAttribute(EaseConstant.EXTRA_GROUP_CARD), Map.class);
                    String name = map.get("name");
                    String description = map.get("description");
                    String pic = map.get("pic");
                    String memberCount = map.get("member_count");
                    gid = map.get("gid");
                    tvGroupName.setText(name);
                    ImageUtil.loadImageWithView(context, pic, imgGroup);
                    tvIntroduce.setText(description);
                    tvNember.setText("群成员人数:" + memberCount + "人");
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
                handleTextMessage();
            }
            protected void handleTextMessage() {
                if (message.direct() == EMMessage.Direct.SEND) {
                    setMessageSendCallback();
                    switch (message.status()) {
                        case CREATE:
                            progressBar.setVisibility(View.GONE);
                            statusView.setVisibility(View.VISIBLE);
                            break;
                        case SUCCESS:
                            progressBar.setVisibility(View.GONE);
                            statusView.setVisibility(View.GONE);
                            break;
                        case FAIL:
                            progressBar.setVisibility(View.GONE);
                            statusView.setVisibility(View.VISIBLE);
                            break;
                        case INPROGRESS:
                            progressBar.setVisibility(View.VISIBLE);
                            statusView.setVisibility(View.GONE);
                            break;
                        default:
                            break;
                    }
                } else {
                    if (!message.isAcked() &amp;&amp; message.getChatType() == ChatType.Chat) {
                        try {
                            EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
                        } catch (HyphenateException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            @Override
            protected void onUpdateView() {
                adapter.notifyDataSetChanged();
            }
            @Override
            protected void onBubbleClick() {
                // TODO Auto-generated method stub
                Intent intent = new Intent(context, GroupCardActivity.class);
                intent.putExtra("gid", gid);
                context.startActivity(intent);
        
            }
        }

        

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


        

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


        
[Java] 查看源文件 复制代码
protected void sendMessage(UserPageBean userPageBean) {
                EMMessage message = EMMessage.createTxtSendMessage("群邀请", userPageBean.getIm_namelogin());
                message.setAttribute(EaseConstant.EXTRA_GROUP_CARD, groupMessage);
                EMClient.getInstance().chatManager().sendMessage(message);
            }

        

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


        

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


        
[Java] 查看源文件 复制代码
public class EaseMessageAdapter extends BaseAdapter {
            private final static String TAG = "msg";
            private Context context;
            private static final int HANDLER_MESSAGE_REFRESH_LIST = 0;
            private static final int HANDLER_MESSAGE_SELECT_LAST = 1;
            private static final int HANDLER_MESSAGE_SEEK_TO = 2;
            private static final int MESSAGE_TYPE_RECV_TXT = 0;
            private static final int MESSAGE_TYPE_SENT_TXT = 1;
            private static final int MESSAGE_TYPE_SENT_IMAGE = 2;
            private static final int MESSAGE_TYPE_SENT_LOCATION = 3;
            private static final int MESSAGE_TYPE_RECV_LOCATION = 4;
            private static final int MESSAGE_TYPE_RECV_IMAGE = 5;
            private static final int MESSAGE_TYPE_SENT_VOICE = 6;
            private static final int MESSAGE_TYPE_RECV_VOICE = 7;
            private static final int MESSAGE_TYPE_SENT_VIDEO = 8;
            private static final int MESSAGE_TYPE_RECV_VIDEO = 9;
            private static final int MESSAGE_TYPE_SENT_FILE = 10;
            private static final int MESSAGE_TYPE_RECV_FILE = 11;
            private static final int MESSAGE_TYPE_SENT_EXPRESSION = 12;
            private static final int MESSAGE_TYPE_RECV_EXPRESSION = 13;
            private static final int MESSAGE_TYPE_SENT_GROUP_CARD = 14;
            private static final int MESSAGE_TYPE_RECV_GROUP_CARD = 15;
        
            public int itemTypeCount;
            // reference to conversation object in chatsdk
            private EMConversation conversation;
            EMMessage[] messages = null;
            private String toChatUsername;
            private boolean isTrueName;
            private boolean isBidTureName;
            private MessageListItemClickListener itemClickListener;
            private EaseCustomChatRowProvider customRowProvider;
            private boolean showUserNick;
            private boolean showAvatar;
            private Drawable myBubbleBg;
            private Drawable otherBuddleBg;
            private String orderId;
            private String type;
            private ListView listView;
            private boolean isbidded;
            private boolean isbiddedMessage;
            public EaseMessageAdapter(Context context, String username, int chatType, ListView listView, boolean isTrueName, String orderId, boolean isBidTureName, String type, boolean isBidded) {
                this.context = context;
                this.listView = listView;
                toChatUsername = username;
                this.isTrueName = isTrueName;
                this.orderId = orderId;
                this.isBidTureName = isBidTureName;
                this.type = type;
                this.conversation = EMClient.getInstance().chatManager().getConversation(username, EaseCommonUtils.getConversationType(chatType), true);
                this.isbidded = isBidded;
            }
            List<EMMessage> msgs1;
            Handler handler = new Handler() {
                private void refreshList() {
                    // you should not call getAllMessages() in UI thread
                    // otherwise there is problem when refreshing UI and there is new message arrive
                    List<EMMessage> msgs = conversation.getAllMessages();
                    List<String> friendsMessageList = new ArrayList<>();
                    List<String> orderUnBidMessageList = new ArrayList<>();
                    List<String> orderBidMessageList = new ArrayList<>();
                    String msgId;
                    for (int i = 0; i < msgs.size(); i++) {
                        msgId = msgs.get(i).getMsgId();
                        String msgOrderId = null;
        //                        try {
                        msgOrderId = (String) msgs.get(i).ext().get(EaseConstant.EXTRA_ORDER_ID);
                        isbiddedMessage = (boolean) msgs.get(i).ext().get(EaseConstant.EXTRA_IS_BIDDED);
        //                            msgOrderId = msgs.get(i).getStringAttribute(EaseConstant.EXTRA_ORDER_ID);
        //                        } catch (HyphenateException e) {
        //                            e.printStackTrace();
        //                        }
                        if (msgOrderId == null || msgOrderId.equals("")) {
                            friendsMessageList.add(msgId);
                        } else if (msgOrderId.equals(orderId)) {
                            if (isbidded){
                                if (isbiddedMessage){
                                    orderBidMessageList.add(msgId);
                                }
                            }else {
                                if (!isbiddedMessage){
                                    orderUnBidMessageList.add(msgId);
                                }
                            }
                        }
                    }
                    if (orderId == null) {
                        msgs1 = conversation.loadMessages(friendsMessageList);
                    } else {
                        if (isbidded){
                            msgs1 = conversation.loadMessages(orderBidMessageList);
                        }else {
                            msgs1 = conversation.loadMessages(orderUnBidMessageList);
                        }
                    }
        
                    messages = msgs1.toArray(new EMMessage[msgs1.size()]);
                    conversation.markAllMessagesAsRead();
                    notifyDataSetChanged();
        
                }
        
                @Override
                public void handleMessage(android.os.Message message) {
                    switch (message.what) {
                        case HANDLER_MESSAGE_REFRESH_LIST:
                            refreshList();
                            break;
                        case HANDLER_MESSAGE_SELECT_LAST:
                            if (messages.length > 0) {
                                listView.setSelection(messages.length - 1);
                            }
                            break;
                        case HANDLER_MESSAGE_SEEK_TO:
                            int position = message.arg1;
                            listView.setSelection(position);
                            break;
                        default:
                            break;
                    }
                }
            };
            public void refresh() {
                if (handler.hasMessages(HANDLER_MESSAGE_REFRESH_LIST)) {
                    return;
                }
                android.os.Message msg = handler.obtainMessage(HANDLER_MESSAGE_REFRESH_LIST);
                handler.sendMessage(msg);
            }
            
            public int getItemViewType(int position) {
                EMMessage message = getItem(position);
                if (message == null) {
                    return -1;
                }
                if (customRowProvider != null &amp;&amp; customRowProvider.getCustomChatRowType(message) > 0) {
                    return customRowProvider.getCustomChatRowType(message) + 13;
                }
                if (message.getType() == EMMessage.Type.TXT) {
                    if (message.getBooleanAttribute(EaseConstant.MESSAGE_ATTR_IS_BIG_EXPRESSION, false)) {
                        return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_EXPRESSION : MESSAGE_TYPE_SENT_EXPRESSION;
                    }
                    try {
                        if (!TextUtils.isEmpty(message.getStringAttribute(EaseConstant.EXTRA_GROUP_CARD))){
                            return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_GROUP_CARD : MESSAGE_TYPE_SENT_GROUP_CARD;
                        }
                    } catch (HyphenateException e) {
                        e.printStackTrace();
                    }
                    return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_TXT : MESSAGE_TYPE_SENT_TXT;
                }
                if (message.getType() == EMMessage.Type.IMAGE) {
                    return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_IMAGE : MESSAGE_TYPE_SENT_IMAGE;
                }
                if (message.getType() == EMMessage.Type.LOCATION) {
                    return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_LOCATION : MESSAGE_TYPE_SENT_LOCATION;
                }
                if (message.getType() == EMMessage.Type.VOICE) {
                    return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_VOICE : MESSAGE_TYPE_SENT_VOICE;
                }
                if (message.getType() == EMMessage.Type.VIDEO) {
                    return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_VIDEO : MESSAGE_TYPE_SENT_VIDEO;
                }
                if (message.getType() == EMMessage.Type.FILE) {
                    return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_FILE : MESSAGE_TYPE_SENT_FILE;
                }
                return -1;// invalid
            }
            protected EaseChatRow createChatRow(Context context, EMMessage message, int position) {
                EaseChatRow chatRow = null;
                if (customRowProvider != null &amp;&amp; customRowProvider.getCustomChatRow(message, position, this) != null) {
                    return customRowProvider.getCustomChatRow(message, position, this);
                }
                message.ext().put(EaseConstant.EXTRA_IS_TRUENAME, isTrueName);
                switch (message.getType()) {
                    case TXT:
                        if (message.getBooleanAttribute(EaseConstant.MESSAGE_ATTR_IS_BIG_EXPRESSION, false)) {
                            chatRow = new EaseChatRowBigExpression(context, message, position, this);
                        } else {
                            try {
                                if (TextUtils.isEmpty(message.getStringAttribute(EaseConstant.EXTRA_GROUP_CARD))) {
                                    chatRow = new EaseChatRowText(context, message, position, this);
                                }else {
                                    chatRow = new GroupCardChatRow(context, message, position, this);
                                }
                            } catch (HyphenateException e) {
                                e.printStackTrace();
                                chatRow = new EaseChatRowText(context, message, position, this);
                            }
                        }
                        break;
                    case LOCATION:
                        chatRow = new EaseChatRowLocation(context, message, position, this);
                        break;
                    case FILE:
                        chatRow = new EaseChatRowFile(context, message, position, this);
                        break;
                    case IMAGE:
                        chatRow = new EaseChatRowImage(context, message, position, this);
                        break;
                    case VOICE:
                        chatRow = new EaseChatRowVoice(context, message, position, this);
                        break;
                    case VIDEO:
                        chatRow = new EaseChatRowVideo(context, message, position, this);
                        break;
                    default:
                        break;
                }
                return chatRow;
            }
        
            @SuppressLint("NewApi")
            public View getView(final int position, View convertView, ViewGroup parent) {
                EMMessage message = getItem(position);
                if (convertView == null) {
                    convertView = createChatRow(context, message, position);
                }
                //refresh ui with messages
                ((EaseChatRow) convertView).setUpView(message, position, itemClickListener, isTrueName, isBidTureName, type);
                return convertView;
            }
        
            public String getToChatUsername() {
                return toChatUsername;
            }
        
            public void setShowUserNick(boolean showUserNick) {
                this.showUserNick = showUserNick;
            }
        
            public void setShowAvatar(boolean showAvatar) {
                this.showAvatar = showAvatar;
            }
        
            public void setMyBubbleBg(Drawable myBubbleBg) {
                this.myBubbleBg = myBubbleBg;
            }
        
            public void setOtherBuddleBg(Drawable otherBuddleBg) {
                this.otherBuddleBg = otherBuddleBg;
            }
        
            public void setItemClickListener(MessageListItemClickListener listener) {
                itemClickListener = listener;
            }
            public void setCustomChatRowProvider(EaseCustomChatRowProvider rowProvider) {
                customRowProvider = rowProvider;
            }
        
            public boolean isShowUserNick() {
                return showUserNick;
            }
        
            public boolean isShowAvatar() {
                return showAvatar;
            }
        
            public Drawable getMyBubbleBg() {
                return myBubbleBg;
            }
        
            public Drawable getOtherBuddleBg() {
                return otherBuddleBg;
            }
        }

               
               
        
        

3

主题

9131

帖子

1813

安币

Android大神

Rank: 6Rank: 6

QQ达人

发表于 2017-11-16 06:07:02 | 显示全部楼层
感觉楼主很用心,辛苦啦~

14

主题

1万

帖子

3914

安币

码皇(巴士元老)

Rank: 8Rank: 8

发表于 2017-11-16 22:40:55 | 显示全部楼层
楼主威武,以后多发干货,多办活动~!

0

主题

9492

帖子

1622

安币

Android大神

IT

Rank: 6Rank: 6

发表于 2017-11-17 23:41:49 | 显示全部楼层
感谢分享,楼主V5~

7

主题

9259

帖子

1987

安币

Android大神

Rank: 6Rank: 6

发表于 2017-11-18 18:43:56 | 显示全部楼层
楼主是好人,回个帖会有安币吗?

0

主题

9081

帖子

2430

安币

Android大神

Rank: 6Rank: 6

发表于 2017-11-19 12:15:48 | 显示全部楼层
每次我都积极回帖的,想要安币~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

站长推荐

通过邮件订阅最新安卓weekly信息
上一条 /4 下一条

下载安卓巴士客户端

全国最大的安卓开发者社区
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| 下载客户端|申请友链|手机版|站点统计|安卓巴士 ( 粤ICP备15117877号 )

快速回复 返回顶部 返回列表