/** * 界面刷新 @Title: initOrRefresh * * @param * @return void * @throws */ private void initOrRefresh() { if (mAdapter != null) { if (MyMessageReceiver.mNewNum != 0) { // 用于更新当在聊天界面锁屏期间来了消息,这时再回到聊天页面的时候需要显示新来的消息 int news = MyMessageReceiver.mNewNum; // 有可能锁屏期间,来了N条消息,因此需要倒叙显示在界面上 int size = initMsgData().size(); for (int i = (news - 1); i >= 0; i--) { mAdapter.add(initMsgData().get(size - (i + 1))); // 添加最后一条消息到界面显示 } mListView.setSelection(mAdapter.getCount() - 1); } else { mAdapter.notifyDataSetChanged(); } } else { mAdapter = new MessageChatAdapter(this, initMsgData()); mListView.setAdapter(mAdapter); } }
public void handleMessage(Message msg) { if (msg.what == NEW_MESSAGE) { BmobMsg message = (BmobMsg) msg.obj; String uid = message.getBelongId(); BmobMsg m = BmobChatManager.getInstance(ChatActivity.this) .getMessage(message.getConversationId(), message.getMsgTime()); if (!uid.equals(targetId)) // 如果不是当前正在聊天对象的消息,不处理 return; mAdapter.add(m); // 定位 mListView.setSelection(mAdapter.getCount() - 1); // 取消当前聊天对象的未读标示 BmobDB.create(ChatActivity.this).resetUnread(targetId); } }
private void initXListView() { // 首先不允许加载更多 mListView.setPullLoadEnable(false); // 允许下拉 mListView.setPullRefreshEnable(true); // 设置监听器 mListView.setXListViewListener(this); mListView.pullRefreshing(); mListView.setDividerHeight(0); // 加载数据 initOrRefresh(); mListView.setSelection(mAdapter.getCount() - 1); mListView.setOnTouchListener( new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent arg1) { hideSoftInputView(); layout_more.setVisibility(View.GONE); layout_add.setVisibility(View.GONE); btn_chat_voice.setVisibility(View.VISIBLE); btn_chat_keyboard.setVisibility(View.GONE); btn_chat_send.setVisibility(View.GONE); return false; } }); // 重发按钮的点击事件 mAdapter.setOnInViewClickListener( R.id.iv_fail_resend, new MessageChatAdapter.onInternalClickListener() { @Override public void OnClickListener(View parentV, View v, Integer position, Object values) { // 重发消息 showResendDialog(parentV, v, values); } }); }
@Override public void onClick(View v) { switch (v.getId()) { case R.id.edit_user_comment: // 点击文本输入框 mListView.setSelection(mListView.getCount() - 1); if (layout_more.getVisibility() == View.VISIBLE) { layout_add.setVisibility(View.GONE); layout_emo.setVisibility(View.GONE); layout_more.setVisibility(View.GONE); } break; case R.id.btn_chat_emo: // 点击笑脸图标 if (layout_more.getVisibility() == View.GONE) { showEditState(true); } else { if (layout_add.getVisibility() == View.VISIBLE) { layout_add.setVisibility(View.GONE); layout_emo.setVisibility(View.VISIBLE); } else { layout_more.setVisibility(View.GONE); } } break; case R.id.btn_chat_add: // 添加按钮-显示图片、拍照、位置 if (layout_more.getVisibility() == View.GONE) { layout_more.setVisibility(View.VISIBLE); layout_add.setVisibility(View.VISIBLE); layout_emo.setVisibility(View.GONE); hideSoftInputView(); } else { if (layout_emo.getVisibility() == View.VISIBLE) { layout_emo.setVisibility(View.GONE); layout_add.setVisibility(View.VISIBLE); } else { layout_more.setVisibility(View.GONE); } } break; case R.id.btn_chat_voice: // 语音按钮 edit_user_comment.setVisibility(View.GONE); layout_more.setVisibility(View.GONE); btn_chat_voice.setVisibility(View.GONE); btn_chat_keyboard.setVisibility(View.VISIBLE); btn_speak.setVisibility(View.VISIBLE); hideSoftInputView(); break; case R.id.btn_chat_keyboard: // 键盘按钮,点击就弹出键盘并隐藏掉声音按钮 showEditState(false); break; case R.id.btn_chat_send: // 发送文本 final String msg = edit_user_comment.getText().toString(); if (msg.equals("")) { ShowToast("请输入发送消息!"); return; } boolean isNetConnected = NetStateUtils.isNetworkAvailable(this); if (!isNetConnected) { ShowToast(R.string.network_error); // return; } // 组装BmobMessage对象 BmobMsg message = BmobMsg.createTextSendMsg(this, targetId, msg); message.setExtra("Bmob"); // 默认发送完成,将数据保存到本地消息表和最近会话表中 manager.sendTextMessage(targetUser, message); // 刷新界面 refreshMessage(message); break; case R.id.tv_camera: // 拍照 selectImageFromCamera(); break; case R.id.tv_picture: // 图片 selectImageFromLocal(); break; case R.id.tv_location: // 位置 selectLocationFromMap(); break; default: break; } }
/** * 刷新界面 @Title: refreshMessage * * @param @param messsage * @return void * @throws */ private void refreshMessage(BmobMsg msg) { // 更新界面 mAdapter.add(msg); mListView.setSelection(mAdapter.getCount() - 1); edit_user_comment.setText(""); }