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); } }
/** * 界面刷新 @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); } }
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); } }); }
/** * 刷新界面 @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(""); }