27、微信聊天Fragment_Msg的代码实现
2016-02-24 13:25
441 查看
初始化界面时获取所有会话信息,并且创造了一条订阅号信息。
根据最后一条消息的时间排序,适配器为NewMsgAdpter,在适配器显示列表项内容时,通过getMessageDigest来获取消息的摘要,也就是不点开看具体消息时的预览消息。
继续回到Fragment_Msg的onItemClick,会判断当前点击的消息类型,如果是订阅号消息则打开PublishMsgListActivity,如果是群组或者个人聊天消息则打开ChatActivity,并在参数中有所区分。
根据最后一条消息的时间排序,适配器为NewMsgAdpter,在适配器显示列表项内容时,通过getMessageDigest来获取消息的摘要,也就是不点开看具体消息时的预览消息。
继续回到Fragment_Msg的onItemClick,会判断当前点击的消息类型,如果是订阅号消息则打开PublishMsgListActivity,如果是群组或者个人聊天消息则打开ChatActivity,并在参数中有所区分。
public class Fragment_Msg extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener { private Activity ctx; private View layout, layout_head;; public RelativeLayout errorItem; public TextView errorText; private ListView lvContact; private NewMsgAdpter adpter; private List<EMConversation> conversationList = new ArrayList<EMConversation>(); private MainActivity parentActivity; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (layout == null) { ctx = this.getActivity(); parentActivity = (MainActivity) getActivity(); layout = ctx.getLayoutInflater().inflate(R.layout.fragment_msg, null); lvContact = (ListView) layout.findViewById(R.id.listview); errorItem = (RelativeLayout) layout.findViewById(R.id.rl_error_item); errorText = (TextView) errorItem.findViewById(R.id.tv_connect_errormsg); setOnListener(); } else { ViewGroup parent = (ViewGroup) layout.getParent(); if (parent != null) { parent.removeView(layout); } } return layout; } @Override public void onResume() { super.onResume(); conversationList.clear(); initViews(); } /** * 刷新页面 */ public void refresh() { conversationList.clear(); initViews(); } private void initViews() { conversationList.addAll(loadConversationsWithRecentChat()); if (conversationList != null && conversationList.size() > 0) { layout.findViewById(R.id.txt_nochat).setVisibility(View.GONE); adpter = new NewMsgAdpter(getActivity(), conversationList); // TODO 加载订阅号信息 ,增加一个Item // if (GloableParams.isHasPulicMsg) { EMConversation nee = new EMConversation("100000"); conversationList.add(0, nee); String time = Utils.getValue(getActivity(), "Time"); String content = Utils.getValue(getActivity(), "Content"); time = "下午 02:45"; content = "[腾讯娱乐] 赵薇炒股日赚74亿"; PublicMsgInfo msgInfo = new PublicMsgInfo(); msgInfo.setContent(content); msgInfo.setMsg_ID("12"); msgInfo.setTime(time); adpter.setPublicMsg(msgInfo); // } lvContact.setAdapter(adpter); } else { layout.findViewById(R.id.txt_nochat).setVisibility(View.VISIBLE); } } /** * 获取所有会话 */ private List<EMConversation> loadConversationsWithRecentChat() { // 获取所有会话,包括陌生人 Hashtable<String, EMConversation> conversations = EMChatManager.getInstance().getAllConversations(); List<EMConversation> list = new ArrayList<EMConversation>(); // 过滤掉messages seize为0的conversation for (EMConversation conversation : conversations.values()) { if (conversation.getAllMessages().size() != 0) list.add(conversation); } // 排序 sortConversationByLastChatTime(list); return list; } /** * 根据最后一条消息的时间排序 */ private void sortConversationByLastChatTime(List<EMConversation> conversationList) { Collections.sort(conversationList, new Comparator<EMConversation>() { @Override public int compare(final EMConversation con1, final EMConversation con2) { EMMessage con2LastMessage = con2.getLastMessage(); EMMessage con1LastMessage = con1.getLastMessage(); if (con2LastMessage.getMsgTime() == con1LastMessage.getMsgTime()) { return 0; } else if (con2LastMessage.getMsgTime() > con1LastMessage.getMsgTime()) { return 1; } else { return -1; } } }); } private void setOnListener() { lvContact.setOnItemClickListener(this); errorItem.setOnClickListener(this); } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { if (adpter.PublicMsg != null && position == 0) { // 打开订阅号列表页面 Utils.start_Activity(getActivity(), PublishMsgListActivity.class); } else { ((MainActivity) getActivity()).updateUnreadLabel(); EMConversation conversation = conversationList.get(position); Intent intent = new Intent(getActivity(), ChatActivity.class); Hashtable<String, String> ChatRecord = adpter.getChatRecord(); if (ChatRecord != null) { if (conversation.isGroup()) { GroupInfo info = GloableParams.GroupInfos.get(conversation.getUserName()); if (info != null) { intent.putExtra(Constants.TYPE, ChatActivity.CHATTYPE_GROUP); intent.putExtra(Constants.GROUP_ID, info.getGroup_id()); intent.putExtra(Constants.NAME, info.getGroup_name());// 设置标题 getActivity().startActivity(intent); } else { intent.putExtra(Constants.TYPE, ChatActivity.CHATTYPE_GROUP); intent.putExtra(Constants.GROUP_ID, info.getGroup_id()); intent.putExtra(Constants.NAME, R.string.group_chats);// 设置标题 getActivity().startActivity(intent); } } else { User user = GloableParams.Users.get(conversation .getUserName()); if (user != null) { intent.putExtra(Constants.NAME, user.getUserName());// 设置昵称 intent.putExtra(Constants.TYPE,ChatActivity.CHATTYPE_SINGLE); intent.putExtra(Constants.User_ID, conversation.getUserName()); getActivity().startActivity(intent); } else { intent.putExtra(Constants.NAME, "好友"); intent.putExtra(Constants.TYPE,ChatActivity.CHATTYPE_SINGLE); intent.putExtra(Constants.User_ID, conversation.getUserName()); getActivity().startActivity(intent); } } } } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.rl_error_item: NetUtil.openSetNetWork(getActivity()); break; default: break; } } }
相关文章推荐
- 26、微信聊天Fragment_Msg的界面布局
- 24、微信-标题栏上的下拉菜单式弹窗TitlePopup
- 23、微信-温馨提示对话框WarnTipDialog
- 22、微信 - 主界面实现
- 21、微信 - 主界面布局
- 微信从无到有
- 如何使用腾讯微信公众平台示例代码…
- Java实现仿微信红包分配规则
- 传统媒体试水微信杂志 联手搭建销售新模式
- 微信红包算法
- 微信支付 系统繁忙
- IOS开发中的微信支付功能
- 微信的那个老外产品经理又写了一篇《中国移动应用设计趋势》
- Java学习:解数独的小程序
- 微信jsapi支付开发遇到的坑
- Java小程序——将浮点数转换成人民币读法
- 微信老外产品经理:《中国移动应用设计趋势》
- iOS开发支付集成之微信支付
- Java的小程序
- 微信公众号使用