Commit 58f7b2668fe82fd47858e9a0d7abd63cb613ce65
1 parent
db523305
修改有人加入聊天室
Showing
10 changed files
with
299 additions
and
183 deletions
app/libs/libcnliveutil.jar
No preview for this file type
app/src/main/java/com/cnlive/im/adapter/ChatListAdapter.java
| 1 | 1 | package com.cnlive.im.adapter; |
| 2 | 2 | |
| 3 | +import android.annotation.TargetApi; | |
| 3 | 4 | import android.content.Context; |
| 5 | +import android.graphics.Color; | |
| 6 | +import android.os.Build; | |
| 4 | 7 | import android.view.LayoutInflater; |
| 5 | 8 | import android.view.View; |
| 6 | 9 | import android.view.ViewGroup; |
| 7 | 10 | import android.widget.BaseAdapter; |
| 11 | +import android.widget.RelativeLayout; | |
| 8 | 12 | import android.widget.TextView; |
| 9 | 13 | |
| 10 | 14 | import com.cnlive.im.R; |
| 11 | -import com.cnlive.libs.util.chat.model.CNMessage; | |
| 15 | +import com.cnlive.im.ui.ChatRoomActivity; | |
| 16 | + | |
| 17 | +import org.json.JSONException; | |
| 18 | +import org.json.JSONObject; | |
| 12 | 19 | |
| 13 | 20 | import java.util.ArrayList; |
| 14 | 21 | import java.util.List; |
| ... | ... | @@ -19,12 +26,14 @@ import java.util.List; |
| 19 | 26 | |
| 20 | 27 | public class ChatListAdapter extends BaseAdapter { |
| 21 | 28 | |
| 22 | - private ArrayList<CNMessage> msgList; | |
| 29 | + private List<ChatRoomActivity.CNMessageInfo> msgList; | |
| 23 | 30 | private LayoutInflater mInflater = null; |
| 31 | + private final RelativeLayout.LayoutParams layoutParams; | |
| 24 | 32 | |
| 25 | 33 | public ChatListAdapter(Context context) { |
| 26 | 34 | msgList = new ArrayList<>(); |
| 27 | 35 | mInflater = LayoutInflater.from(context); |
| 36 | + layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); | |
| 28 | 37 | } |
| 29 | 38 | |
| 30 | 39 | @Override |
| ... | ... | @@ -42,45 +51,65 @@ public class ChatListAdapter extends BaseAdapter { |
| 42 | 51 | return 0; |
| 43 | 52 | } |
| 44 | 53 | |
| 54 | + @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) | |
| 45 | 55 | @Override |
| 46 | 56 | public View getView(int position, View convertView, ViewGroup parent) { |
| 47 | 57 | ViewHolder viewHolder; |
| 48 | 58 | if (convertView == null) { |
| 49 | 59 | viewHolder = new ViewHolder(); |
| 50 | 60 | convertView = mInflater.inflate(R.layout.msg_text_view, null); |
| 61 | + viewHolder.mRelativeLayout = (RelativeLayout) convertView.findViewById(R.id.re_chat_room); | |
| 51 | 62 | viewHolder.userName = (TextView) convertView.findViewById(R.id.user_name); |
| 52 | 63 | viewHolder.msgContent = (TextView) convertView.findViewById(R.id.msg_text); |
| 53 | 64 | convertView.setTag(viewHolder); |
| 54 | 65 | } else { |
| 55 | 66 | viewHolder = (ViewHolder) convertView.getTag(); |
| 56 | 67 | } |
| 57 | - viewHolder.userName.setText(msgList.get(position).getUserInfo().getUserName() == null ? "" : msgList.get(position).getUserInfo().getUserName() + " : "); | |
| 58 | - viewHolder.msgContent.setText(msgList.get(position).getContent() == null ? "" : msgList.get(position).getContent()); | |
| 68 | + | |
| 69 | + if (msgList.get(position).isSendMesageInfo()) { | |
| 70 | + viewHolder.msgContent.setText(msgList.get(position).getCnMessage().getUserInfo().getUserName() == null ? "" : " : " + msgList.get(position).getCnMessage().getUserInfo().getUserName()); | |
| 71 | + try { | |
| 72 | + viewHolder.userName.setText(msgList.get(position).getCnMessage().getContent() == null ? "" : (String) new JSONObject(msgList.get(position).getCnMessage().getContent()).get("message")); | |
| 73 | + } catch (JSONException e) { | |
| 74 | + e.printStackTrace(); | |
| 75 | + } | |
| 76 | + layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_START); | |
| 77 | + layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END); | |
| 78 | + viewHolder.mRelativeLayout.setLayoutParams(layoutParams); | |
| 79 | + viewHolder.msgContent.setTextColor(Color.parseColor("#d61d0c")); | |
| 80 | + viewHolder.userName.setTextColor(Color.BLACK); | |
| 81 | + } else { | |
| 82 | + viewHolder.userName.setText(msgList.get(position).getCnMessage().getUserInfo().getUserName() == null ? "" : msgList.get(position).getCnMessage().getUserInfo().getUserName() + " : "); | |
| 83 | + try { | |
| 84 | + viewHolder.msgContent.setText(msgList.get(position).getCnMessage().getContent() == null ? "" : msgList.get(position).getCnMessage().getContent() == null ? "" : (String) new JSONObject(msgList.get(position).getCnMessage().getContent()).get("message")); | |
| 85 | + } catch (JSONException e) { | |
| 86 | + e.printStackTrace(); | |
| 87 | + } | |
| 88 | + viewHolder.mRelativeLayout.setLayoutParams(layoutParams); | |
| 89 | + layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_END); | |
| 90 | + layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START); | |
| 91 | + viewHolder.userName.setTextColor(Color.parseColor("#d61d0c")); | |
| 92 | + viewHolder.msgContent.setTextColor(Color.BLACK); | |
| 93 | + } | |
| 94 | + | |
| 95 | + | |
| 59 | 96 | return convertView; |
| 60 | 97 | |
| 61 | 98 | } |
| 62 | 99 | |
| 63 | - public void addMessage(CNMessage msg) { | |
| 64 | - if (msgList == null) return; | |
| 65 | - msgList.add(msg); | |
| 66 | - } | |
| 67 | 100 | |
| 68 | - public void updateItems(List<CNMessage> items) { | |
| 101 | + | |
| 102 | + public void updateItems(List<ChatRoomActivity.CNMessageInfo> items) { | |
| 69 | 103 | if (items == null) return; |
| 70 | 104 | msgList.clear(); |
| 71 | 105 | msgList.addAll(items); |
| 72 | 106 | } |
| 73 | 107 | |
| 74 | 108 | |
| 109 | + private class ViewHolder { | |
| 110 | + TextView userName; | |
| 111 | + TextView msgContent; | |
| 112 | + RelativeLayout mRelativeLayout; | |
| 75 | 113 | |
| 76 | - | |
| 77 | - | |
| 78 | - | |
| 79 | - | |
| 80 | - | |
| 81 | - | |
| 82 | - public class ViewHolder { | |
| 83 | - public TextView userName; | |
| 84 | - public TextView msgContent; | |
| 85 | 114 | } |
| 86 | 115 | } | ... | ... |
app/src/main/java/com/cnlive/im/mode/CNMessageModole.java
0 → 100644
| 1 | +package com.cnlive.im.mode; | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * @author 陈硕 | |
| 5 | + * @time 2017/3/28 10:27 | |
| 6 | + * @desc ${TODD} | |
| 7 | + */ | |
| 8 | +public class CNMessageModole { | |
| 9 | + | |
| 10 | + | |
| 11 | + public CNMessageModole(String message, String type) { | |
| 12 | + this.message = message; | |
| 13 | + this.type = type; | |
| 14 | + } | |
| 15 | + | |
| 16 | + /** | |
| 17 | + * message : Sass | |
| 18 | + * timestamp : 1.490085670463534E9 | |
| 19 | + * type : 2 | |
| 20 | + */ | |
| 21 | + | |
| 22 | + private String message; | |
| 23 | + private String type; | |
| 24 | + | |
| 25 | + public String getMessage() { | |
| 26 | + return message; | |
| 27 | + } | |
| 28 | + | |
| 29 | + public void setMessage(String message) { | |
| 30 | + this.message = message; | |
| 31 | + } | |
| 32 | + | |
| 33 | + | |
| 34 | + public String getType() { | |
| 35 | + return type; | |
| 36 | + } | |
| 37 | + | |
| 38 | + public void setType(String type) { | |
| 39 | + this.type = type; | |
| 40 | + } | |
| 41 | +} | ... | ... |
app/src/main/java/com/cnlive/im/ui/ChatItemActivity.java
app/src/main/java/com/cnlive/im/ui/ChatRoomActivity.java
| ... | ... | @@ -15,12 +15,20 @@ import android.widget.Toast; |
| 15 | 15 | |
| 16 | 16 | import com.cnlive.im.R; |
| 17 | 17 | import com.cnlive.im.adapter.ChatListAdapter; |
| 18 | +import com.cnlive.im.mode.CNMessageModole; | |
| 19 | +import com.cnlive.im.util.ToStringUtils; | |
| 18 | 20 | import com.cnlive.libs.util.chat.ChatUtil; |
| 19 | 21 | import com.cnlive.libs.util.chat.base.IChat; |
| 22 | +import com.cnlive.libs.util.chat.model.CNChatRoomInfo; | |
| 23 | +import com.cnlive.libs.util.chat.model.CNChatRoomMemberInfo; | |
| 20 | 24 | import com.cnlive.libs.util.chat.model.CNMessage; |
| 21 | 25 | import com.cnlive.libs.util.chat.model.CNUserInfo; |
| 22 | 26 | |
| 27 | +import org.json.JSONException; | |
| 28 | +import org.json.JSONObject; | |
| 29 | + | |
| 23 | 30 | import java.util.ArrayList; |
| 31 | +import java.util.List; | |
| 24 | 32 | |
| 25 | 33 | |
| 26 | 34 | /** |
| ... | ... | @@ -30,22 +38,20 @@ import java.util.ArrayList; |
| 30 | 38 | */ |
| 31 | 39 | public class ChatRoomActivity extends AppCompatActivity implements View.OnClickListener { |
| 32 | 40 | private static final String TAG = "ChatRoomActivity"; |
| 33 | - private ArrayList<CNMessage> groupChatOneListMy, groupChatTwoListMy; | |
| 34 | - private String roomIdOne = "ChatRoom05", roomIdTwo = "ChatRoom06"; | |
| 41 | + private ArrayList<CNMessageInfo> groupChatOneListMy; | |
| 42 | + private String roomIdOne = "ChatRoom05"; | |
| 35 | 43 | private EditText editText; |
| 36 | - private Button send_messageOneBtn, send_messageOneTwo; | |
| 44 | + private Button send_messageOneBtn; | |
| 37 | 45 | private ListView listViewOne; |
| 38 | - private ChatListAdapter chatListAdapterOne, chatListAdapterTwo; | |
| 39 | - private ListView listViewTwo; | |
| 40 | - private Button foundChatBtn; | |
| 41 | - private Button creatChat2Btn; | |
| 46 | + private ChatListAdapter chatListAdapterOne; | |
| 47 | + private Button foundChatBtn; | |
| 42 | 48 | private Handler mHandler; |
| 43 | 49 | private RelativeLayout re_chat; |
| 44 | 50 | private LinearLayout lin_user; |
| 45 | 51 | private EditText user_id, user_name; |
| 46 | - private CNUserInfo cnUser; | |
| 47 | - private Button exit_chat2; | |
| 48 | 52 | private Button exit_chat1; |
| 53 | + private Button chat_info_mationBtn; | |
| 54 | + public static String user_name_string; | |
| 49 | 55 | |
| 50 | 56 | |
| 51 | 57 | @Override |
| ... | ... | @@ -61,46 +67,38 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL |
| 61 | 67 | ChatUtil.setOnReceiveMessageListener(receiveMessageListener); |
| 62 | 68 | ChatUtil.setKickedOfflineListener(onKickedOfflineListener); |
| 63 | 69 | Intent intent = getIntent(); |
| 64 | - String user_name_string = intent.getStringExtra("user_name_string"); | |
| 70 | + user_name_string = intent.getStringExtra("user_name_string"); | |
| 65 | 71 | String user_id_string = intent.getStringExtra("user_id_string"); |
| 66 | - if(user_name_string!=null){ | |
| 67 | - ChatUtil.login(new CNUserInfo(user_id_string,user_name_string,""),connectListener); | |
| 68 | - }else { | |
| 72 | + if (user_name_string != null) { | |
| 73 | + ChatUtil.login(new CNUserInfo(user_id_string, user_name_string, ""), connectListener); | |
| 74 | + } else { | |
| 69 | 75 | ChatUtil.logOut(connectListener); |
| 70 | 76 | } |
| 77 | + | |
| 71 | 78 | } |
| 72 | 79 | |
| 73 | 80 | |
| 74 | 81 | private void initUI() { |
| 75 | - exit_chat2 = (Button) findViewById(R.id.exit_chat2); | |
| 82 | + chat_info_mationBtn = (Button) findViewById(R.id.chat_info_mation); | |
| 83 | + chat_info_mationBtn.setOnClickListener(this); | |
| 76 | 84 | exit_chat1 = (Button) findViewById(R.id.exit_chat1); |
| 77 | 85 | user_id = (EditText) findViewById(R.id.user_ID); |
| 78 | 86 | user_name = (EditText) findViewById(R.id.user_name); |
| 79 | 87 | lin_user = (LinearLayout) findViewById(R.id.lin_user); |
| 80 | 88 | re_chat = (RelativeLayout) findViewById(R.id.re_chat); |
| 81 | - creatChat2Btn = (Button) findViewById(R.id.creatChat2); | |
| 82 | 89 | foundChatBtn = (Button) findViewById(R.id.creatChat); |
| 83 | 90 | listViewOne = (ListView) findViewById(R.id.chat_list_viewOne); |
| 84 | - listViewTwo = (ListView) findViewById(R.id.chat_list_viewTwo); | |
| 85 | 91 | editText = (EditText) findViewById(R.id.message_content); |
| 86 | 92 | send_messageOneBtn = (Button) findViewById(R.id.send_messageOne); |
| 87 | - send_messageOneTwo = (Button) findViewById(R.id.send_messageTwo); | |
| 88 | 93 | exit_chat1.setOnClickListener(this); |
| 89 | - exit_chat2.setOnClickListener(this); | |
| 90 | 94 | foundChatBtn.setOnClickListener(this); |
| 91 | 95 | send_messageOneBtn.setOnClickListener(this); |
| 92 | - send_messageOneTwo.setOnClickListener(this); | |
| 93 | - creatChat2Btn.setOnClickListener(this); | |
| 94 | 96 | groupChatOneListMy = new ArrayList<>(); |
| 95 | - groupChatTwoListMy = new ArrayList<>(); | |
| 96 | 97 | chatListAdapterOne = new ChatListAdapter(ChatRoomActivity.this); |
| 97 | - chatListAdapterTwo = new ChatListAdapter(ChatRoomActivity.this); | |
| 98 | 98 | listViewOne.setAdapter(chatListAdapterOne); |
| 99 | - listViewTwo.setAdapter(chatListAdapterTwo); | |
| 100 | 99 | } |
| 101 | 100 | |
| 102 | 101 | |
| 103 | - | |
| 104 | 102 | /** |
| 105 | 103 | * 发送消息监听 |
| 106 | 104 | */ |
| ... | ... | @@ -115,25 +113,20 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL |
| 115 | 113 | |
| 116 | 114 | @Override |
| 117 | 115 | public void onSuccess(final CNMessage cnMessage) { |
| 118 | - | |
| 119 | 116 | mHandler.post(new Runnable() { |
| 120 | 117 | @Override |
| 121 | 118 | public void run() { |
| 119 | + try { | |
| 120 | + if (((String) new JSONObject(cnMessage.getContent()).get("type")).equals("2")) | |
| 121 | + return; | |
| 122 | + } catch (JSONException e) { | |
| 123 | + e.printStackTrace(); | |
| 124 | + } | |
| 122 | 125 | if ((roomIdOne).equals(cnMessage.getTargetId())) { |
| 123 | - groupChatOneListMy.add(cnMessage); | |
| 126 | + groupChatOneListMy.add(new CNMessageInfo(cnMessage, true)); | |
| 124 | 127 | chatListAdapterOne.updateItems(groupChatOneListMy); |
| 125 | 128 | chatListAdapterOne.notifyDataSetChanged(); |
| 126 | 129 | } |
| 127 | - Log.v(TAG, "SendMessageListener SUCCESSES" + cnMessage.getTargetId() + cnMessage.getUserInfo().getUserName()); | |
| 128 | - | |
| 129 | - /** | |
| 130 | - * 连接两个聊天室30015 错误 | |
| 131 | - */ | |
| 132 | - if ((roomIdTwo).equals(cnMessage.getTargetId())) { | |
| 133 | - groupChatTwoListMy.add(cnMessage); | |
| 134 | - chatListAdapterTwo.updateItems(groupChatTwoListMy); | |
| 135 | - chatListAdapterTwo.notifyDataSetChanged(); | |
| 136 | - } | |
| 137 | 130 | |
| 138 | 131 | } |
| 139 | 132 | }); |
| ... | ... | @@ -147,11 +140,8 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL |
| 147 | 140 | @Override |
| 148 | 141 | public void run() { |
| 149 | 142 | Toast.makeText(ChatRoomActivity.this, s, Toast.LENGTH_SHORT).show(); |
| 150 | - | |
| 151 | 143 | } |
| 152 | 144 | }); |
| 153 | - | |
| 154 | - | |
| 155 | 145 | } |
| 156 | 146 | }; |
| 157 | 147 | |
| ... | ... | @@ -173,22 +163,21 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL |
| 173 | 163 | mHandler.post(new Runnable() { |
| 174 | 164 | @Override |
| 175 | 165 | public void run() { |
| 176 | - if (( roomIdOne).equals(cnMessage.getTargetId())) { | |
| 177 | - groupChatOneListMy.add(cnMessage); | |
| 166 | + try { | |
| 167 | + JSONObject jsonObject = new JSONObject(cnMessage.getContent().toString()); | |
| 168 | + String type = (String) jsonObject.get("type"); | |
| 169 | + if ("2".equals(type)) { | |
| 170 | + Toast.makeText(ChatRoomActivity.this, cnMessage.getUserInfo().getUserName()+"加入聊天室", Toast.LENGTH_SHORT).show(); | |
| 171 | + return; | |
| 172 | + } | |
| 173 | + } catch (JSONException e) { | |
| 174 | + e.printStackTrace(); | |
| 175 | + } | |
| 176 | + { | |
| 177 | + groupChatOneListMy.add(new CNMessageInfo(cnMessage, false)); | |
| 178 | 178 | chatListAdapterOne.updateItems(groupChatOneListMy); |
| 179 | 179 | chatListAdapterOne.notifyDataSetChanged(); |
| 180 | 180 | } |
| 181 | - Log.v(TAG, "SendMessageListener SUCCESSES" + cnMessage.getTargetId() + cnMessage.getUserInfo().getUserName()); | |
| 182 | - | |
| 183 | - /** | |
| 184 | - * 连接两个聊天室30015 错误 | |
| 185 | - */ | |
| 186 | - if (( roomIdTwo).equals(cnMessage.getTargetId())) { | |
| 187 | - groupChatTwoListMy.add(cnMessage); | |
| 188 | - chatListAdapterTwo.updateItems(groupChatTwoListMy); | |
| 189 | - chatListAdapterTwo.notifyDataSetChanged(); | |
| 190 | - } | |
| 191 | - | |
| 192 | 181 | } |
| 193 | 182 | }); |
| 194 | 183 | |
| ... | ... | @@ -259,6 +248,11 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL |
| 259 | 248 | mHandler.post(new Runnable() { |
| 260 | 249 | @Override |
| 261 | 250 | public void run() { |
| 251 | + if(i==0){ | |
| 252 | + String objToJsonString = ToStringUtils.objToJsonString(new CNMessageModole(user_name_string + "加入聊天室", "2")); | |
| 253 | + ChatUtil.sendMessage(IChat.CHATROOM, roomIdOne, objToJsonString, sendMessageListener); | |
| 254 | + ++i; | |
| 255 | + } | |
| 262 | 256 | Toast.makeText(ChatRoomActivity.this, extra, Toast.LENGTH_SHORT).show(); |
| 263 | 257 | |
| 264 | 258 | } |
| ... | ... | @@ -282,41 +276,42 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL |
| 282 | 276 | if (editText.getText().toString().isEmpty()) { |
| 283 | 277 | Toast.makeText(this, "不能发送空消息呦~", Toast.LENGTH_SHORT).show(); |
| 284 | 278 | } else { |
| 285 | - ChatUtil.sendMessage(IChat.CHATROOM, roomIdOne, editText.getText().toString(), sendMessageListener); | |
| 286 | - editText.setText(""); | |
| 287 | - } | |
| 288 | - break; | |
| 289 | - // 向聊天室2发送消息 | |
| 290 | - case R.id.send_messageTwo: | |
| 291 | - //发送聊天室消息 | |
| 292 | - if (editText.getText().toString().isEmpty()) { | |
| 293 | - Toast.makeText(this, "不能发送空消息呦~", Toast.LENGTH_SHORT).show(); | |
| 294 | - } else { | |
| 295 | - ChatUtil.sendMessage(IChat.CHATROOM, roomIdTwo, editText.getText().toString(), sendMessageListener); | |
| 279 | + String objToJsonString = ToStringUtils.objToJsonString(new CNMessageModole(editText.getText().toString(), "1")); | |
| 280 | + Log.v("objToJsonString", objToJsonString); | |
| 281 | + | |
| 282 | + | |
| 283 | + ChatUtil.sendMessage(IChat.CHATROOM, roomIdOne, objToJsonString, sendMessageListener); | |
| 296 | 284 | editText.setText(""); |
| 297 | 285 | } |
| 298 | 286 | break; |
| 299 | - | |
| 300 | 287 | case R.id.creatChat: |
| 301 | 288 | ChatUtil.joinChatRoom(roomIdOne, -1, onOperationListener); |
| 302 | - //加入聊天室2 | |
| 303 | - case R.id.creatChat2: | |
| 304 | - // ChatUtil.joinChatRoom(roomIdTwo, -1, onOperationListener); | |
| 305 | - break; | |
| 306 | - | |
| 307 | - case R.id.exit_chat2: | |
| 308 | - ChatUtil.quitChatRoom(roomIdTwo, onOperationListener); | |
| 309 | 289 | break; |
| 310 | - | |
| 311 | 290 | case R.id.exit_chat1: |
| 312 | 291 | ChatUtil.quitChatRoom(roomIdOne, onOperationListener); |
| 313 | 292 | break; |
| 293 | + case R.id.chat_info_mation: | |
| 294 | + ChatUtil.getChatRoomInfo(roomIdOne, 30, CNChatRoomInfo.ChatRoomMemberOrder.RC_CHAT_ROOM_MEMBER_ASC, new IChat.CNResultCallback<CNChatRoomInfo>() { | |
| 295 | + @Override | |
| 296 | + public void onSuccess(CNChatRoomInfo cnChatRoomInfo) { | |
| 297 | + List<CNChatRoomMemberInfo> memberInfos = cnChatRoomInfo.getMemberInfo(); | |
| 298 | + for (CNChatRoomMemberInfo memberInfo : memberInfos) { | |
| 299 | + String userId = memberInfo.getUserId(); | |
| 300 | + Log.v("userId", userId); | |
| 301 | + } | |
| 302 | + } | |
| 314 | 303 | |
| 315 | - } | |
| 316 | - } | |
| 317 | - | |
| 304 | + @Override | |
| 305 | + public void onError(int i, String s) { | |
| 306 | + Log.v("userId", i + "" + s); | |
| 318 | 307 | |
| 308 | + } | |
| 309 | + }); | |
| 310 | + break; | |
| 319 | 311 | |
| 312 | + } | |
| 313 | + } | |
| 314 | +int i=0; | |
| 320 | 315 | |
| 321 | 316 | private IChat.OnConnectListener connectListener = new IChat.OnConnectListener() { |
| 322 | 317 | @Override |
| ... | ... | @@ -329,6 +324,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL |
| 329 | 324 | @Override |
| 330 | 325 | public void onSuccess(String s) { |
| 331 | 326 | Log.e(TAG, "ConnectListener 连接监听" + s); |
| 327 | + | |
| 332 | 328 | Toast.makeText(ChatRoomActivity.this, s, Toast.LENGTH_SHORT).show(); |
| 333 | 329 | } |
| 334 | 330 | |
| ... | ... | @@ -341,4 +337,31 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL |
| 341 | 337 | }; |
| 342 | 338 | |
| 343 | 339 | |
| 340 | + public static class CNMessageInfo { | |
| 341 | + CNMessage cnMessage; | |
| 342 | + boolean sendMesageInfo; | |
| 343 | + | |
| 344 | + public CNMessage getCnMessage() { | |
| 345 | + return cnMessage; | |
| 346 | + } | |
| 347 | + | |
| 348 | + public void setCnMessage(CNMessage cnMessage) { | |
| 349 | + this.cnMessage = cnMessage; | |
| 350 | + } | |
| 351 | + | |
| 352 | + public boolean isSendMesageInfo() { | |
| 353 | + return sendMesageInfo; | |
| 354 | + } | |
| 355 | + | |
| 356 | + public void setSendMesageInfo(boolean sendMesageInfo) { | |
| 357 | + this.sendMesageInfo = sendMesageInfo; | |
| 358 | + } | |
| 359 | + | |
| 360 | + public CNMessageInfo(CNMessage cnMessage, boolean sendMesageInfo) { | |
| 361 | + this.cnMessage = cnMessage; | |
| 362 | + this.sendMesageInfo = sendMesageInfo; | |
| 363 | + } | |
| 364 | + } | |
| 365 | + | |
| 366 | + | |
| 344 | 367 | } | ... | ... |
app/src/main/java/com/cnlive/im/ui/PrivateChatActivity.java
| ... | ... | @@ -37,7 +37,7 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli |
| 37 | 37 | private EditText messageContent; |
| 38 | 38 | private Button sendMessageBtn; |
| 39 | 39 | private ChatListAdapter chatListAdapter; |
| 40 | - private ArrayList<CNMessage> messageList = new ArrayList<>(); | |
| 40 | + private ArrayList<ChatRoomActivity.CNMessageInfo> messageList = new ArrayList<>(); | |
| 41 | 41 | private CNMessage cnMessage; |
| 42 | 42 | private LinearLayout linearLayoutUseID; |
| 43 | 43 | private EditText user_id_edit; |
| ... | ... | @@ -68,7 +68,6 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli |
| 68 | 68 | private void initView() { |
| 69 | 69 | opposite_name_edit = (EditText) findViewById(R.id.opposite_name_edit); |
| 70 | 70 | chatListView = (ListView) findViewById(R.id.private_list_view); |
| 71 | - chatListAdapter = new ChatListAdapter(this); | |
| 72 | 71 | chatListView.setAdapter(chatListAdapter); |
| 73 | 72 | messageContent = (EditText) findViewById(R.id.message_content); |
| 74 | 73 | sendMessageBtn = (Button) findViewById(R.id.send_message); |
| ... | ... | @@ -88,6 +87,8 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli |
| 88 | 87 | agree_reject_lin = (LinearLayout) findViewById(R.id.agree_reject_lin); |
| 89 | 88 | vistor_btn = (Button) findViewById(R.id.vistor_Btn); |
| 90 | 89 | vistor_btn.setOnClickListener(this); |
| 90 | + chatListAdapter = new ChatListAdapter(this); | |
| 91 | + | |
| 91 | 92 | } |
| 92 | 93 | |
| 93 | 94 | |
| ... | ... | @@ -205,7 +206,7 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli |
| 205 | 206 | } else if (messageList.size() == 0 && cnMessage.getContent().equals("拒绝")) { |
| 206 | 207 | } else if (messageList.size() == 0 && cnMessage.getContent().equals("同意")) { |
| 207 | 208 | } else { |
| 208 | - messageList.add(cnMessage); | |
| 209 | + messageList.add(new ChatRoomActivity.CNMessageInfo(cnMessage,true)); | |
| 209 | 210 | chatListAdapter.updateItems(messageList); |
| 210 | 211 | chatListAdapter.notifyDataSetChanged(); |
| 211 | 212 | } |
| ... | ... | @@ -242,7 +243,6 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli |
| 242 | 243 | |
| 243 | 244 | } |
| 244 | 245 | }; |
| 245 | - private boolean isvistor = false; | |
| 246 | 246 | //接收消息监听 |
| 247 | 247 | private IChat.OnReceiveMessageListener receiveMessageListener = new IChat.OnReceiveMessageListener() { |
| 248 | 248 | @Override |
| ... | ... | @@ -261,16 +261,17 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli |
| 261 | 261 | Toast.makeText(PrivateChatActivity.this, "对方已经同意了您", Toast.LENGTH_SHORT).show(); |
| 262 | 262 | sendInfo = 5; |
| 263 | 263 | } else { |
| 264 | - messageList.add(cnMessage); | |
| 264 | + messageList.add(new ChatRoomActivity.CNMessageInfo(cnMessage,false)); | |
| 265 | 265 | chatListAdapter.updateItems(messageList); |
| 266 | 266 | chatListAdapter.notifyDataSetChanged(); |
| 267 | 267 | } |
| 268 | 268 | |
| 269 | - | |
| 270 | 269 | } |
| 271 | 270 | }); |
| 272 | 271 | |
| 273 | 272 | } |
| 274 | 273 | }; |
| 274 | + | |
| 275 | + | |
| 275 | 276 | } |
| 276 | 277 | ... | ... |
app/src/main/java/com/cnlive/im/util/ToStringUtils.java
0 → 100644
| 1 | +package com.cnlive.im.util; | |
| 2 | + | |
| 3 | +import com.cnlive.im.mode.CNMessageModole; | |
| 4 | + | |
| 5 | +import org.json.JSONException; | |
| 6 | +import org.json.JSONObject; | |
| 7 | + | |
| 8 | +/** | |
| 9 | + * @author 陈硕 | |
| 10 | + * @time 2017/3/28 10:20 | |
| 11 | + * @desc ${TODD} | |
| 12 | + */ | |
| 13 | +public class ToStringUtils { | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + public static String objToJsonString(CNMessageModole cnMessageModole) { | |
| 18 | + | |
| 19 | + // 初始化返回值 | |
| 20 | + String json = "str_empty"; | |
| 21 | + | |
| 22 | + if (cnMessageModole == null) { | |
| 23 | + return json; | |
| 24 | + } | |
| 25 | + | |
| 26 | + StringBuilder buff = new StringBuilder(); | |
| 27 | + buff.append("{"); | |
| 28 | + | |
| 29 | + | |
| 30 | + buff.append("message"); | |
| 31 | + buff.append(":"); | |
| 32 | + buff.append("\""); | |
| 33 | + buff.append(cnMessageModole.getMessage() == null ? "" : cnMessageModole.getMessage()); | |
| 34 | + buff.append("\""); | |
| 35 | + buff.append(","); | |
| 36 | + | |
| 37 | + | |
| 38 | + buff.append("type"); | |
| 39 | + buff.append(":"); | |
| 40 | + buff.append("\""); | |
| 41 | + buff.append(cnMessageModole.getType() == null ? "" : cnMessageModole.getType()); | |
| 42 | + buff.append("\""); | |
| 43 | + | |
| 44 | + buff.append("}"); | |
| 45 | + json = buff.toString(); | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + return json; | |
| 52 | + } | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + public static CNMessageModole JsonStringToobj(String CNMessagejson) throws JSONException { | |
| 60 | + | |
| 61 | + | |
| 62 | + JSONObject jsonObj = new JSONObject(CNMessagejson); | |
| 63 | + return new CNMessageModole(jsonObj.get("message").toString(), jsonObj.get("type").toString()); | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + } | |
| 69 | + | |
| 70 | + | |
| 71 | +} | ... | ... |
app/src/main/res/layout/activity_groupchat.xml
| ... | ... | @@ -20,16 +20,10 @@ |
| 20 | 20 | android:orientation="horizontal"> |
| 21 | 21 | |
| 22 | 22 | <LinearLayout |
| 23 | - android:layout_width="0dp" | |
| 23 | + android:layout_width="match_parent" | |
| 24 | 24 | android:layout_height="match_parent" |
| 25 | - android:layout_weight="1" | |
| 26 | 25 | android:orientation="vertical"> |
| 27 | 26 | |
| 28 | - <TextView | |
| 29 | - android:layout_width="match_parent" | |
| 30 | - android:layout_height="wrap_content" | |
| 31 | - android:gravity="center" | |
| 32 | - android:text="聊天室一" /> | |
| 33 | 27 | |
| 34 | 28 | <ListView |
| 35 | 29 | android:id="@+id/chat_list_viewOne" |
| ... | ... | @@ -40,38 +34,9 @@ |
| 40 | 34 | android:divider="@null" |
| 41 | 35 | android:scrollbars="none" |
| 42 | 36 | android:transcriptMode="alwaysScroll" /> |
| 43 | - | |
| 44 | - </LinearLayout> | |
| 45 | - | |
| 46 | - <LinearLayout | |
| 47 | - android:layout_width="0dp" | |
| 48 | - android:layout_height="match_parent" | |
| 49 | - android:layout_weight="1" | |
| 50 | - android:orientation="vertical"> | |
| 51 | - | |
| 52 | - <TextView | |
| 53 | - android:layout_width="match_parent" | |
| 54 | - android:layout_height="wrap_content" | |
| 55 | - android:gravity="center" | |
| 56 | - android:text="聊天室二" /> | |
| 57 | - | |
| 58 | - <ListView | |
| 59 | - android:id="@+id/chat_list_viewTwo" | |
| 60 | - android:layout_width="match_parent" | |
| 61 | - android:layout_height="150dp" | |
| 62 | - android:layout_above="@+id/edit_layout" | |
| 63 | - android:layout_alignParentTop="true" | |
| 64 | - android:layout_toRightOf="@id/chat_list_viewOne" | |
| 65 | - android:divider="@null" | |
| 66 | - android:scrollbars="none" | |
| 67 | - android:transcriptMode="alwaysScroll" /> | |
| 68 | - | |
| 69 | 37 | </LinearLayout> |
| 70 | - | |
| 71 | - | |
| 72 | 38 | </LinearLayout> |
| 73 | 39 | |
| 74 | - | |
| 75 | 40 | <LinearLayout |
| 76 | 41 | android:id="@+id/edit_layout" |
| 77 | 42 | android:layout_width="match_parent" |
| ... | ... | @@ -92,17 +57,9 @@ |
| 92 | 57 | android:layout_height="wrap_content" |
| 93 | 58 | android:layout_gravity="center" |
| 94 | 59 | android:layout_weight="0.2" |
| 95 | - android:text="聊天室1发送" /> | |
| 60 | + android:text="发送" /> | |
| 96 | 61 | |
| 97 | - | |
| 98 | - <Button | |
| 99 | - android:id="@+id/send_messageTwo" | |
| 100 | - android:layout_width="0dp" | |
| 101 | - android:layout_height="wrap_content" | |
| 102 | - android:layout_gravity="center" | |
| 103 | - android:layout_weight="0.2" | |
| 104 | - android:text="聊天室2发送" /> | |
| 105 | - </LinearLayout> | |
| 62 | + </LinearLayout> | |
| 106 | 63 | |
| 107 | 64 | <LinearLayout |
| 108 | 65 | android:layout_width="match_parent" |
| ... | ... | @@ -119,44 +76,30 @@ |
| 119 | 76 | android:id="@+id/creatChat" |
| 120 | 77 | android:layout_width="wrap_content" |
| 121 | 78 | android:layout_height="wrap_content" |
| 122 | - android:text="加入聊天室1" /> | |
| 79 | + android:text="加入聊天室" /> | |
| 123 | 80 | |
| 124 | 81 | <Button |
| 125 | - android:id="@+id/creatChat2" | |
| 82 | + android:id="@+id/chat_info_mation" | |
| 83 | + android:layout_width="wrap_content" | |
| 84 | + android:layout_height="wrap_content" | |
| 85 | + android:layout_weight="1" | |
| 86 | + android:text="获取聊天室信息" /> | |
| 87 | + <Button | |
| 88 | + android:id="@+id/exit_chat1" | |
| 126 | 89 | android:layout_width="wrap_content" |
| 127 | 90 | android:layout_height="wrap_content" |
| 128 | - android:text="加入聊天室2" /> | |
| 91 | + android:text="退出聊天室" /> | |
| 129 | 92 | |
| 130 | 93 | </LinearLayout> |
| 131 | 94 | |
| 132 | - <LinearLayout | |
| 133 | - android:layout_width="match_parent" | |
| 134 | - android:layout_height="wrap_content" | |
| 135 | - android:orientation="horizontal"> | |
| 136 | 95 | |
| 137 | 96 | |
| 138 | - </LinearLayout> | |
| 139 | 97 | |
| 140 | - <LinearLayout | |
| 141 | - android:layout_width="match_parent" | |
| 142 | - android:layout_height="wrap_content" | |
| 143 | - android:orientation="horizontal"> | |
| 144 | 98 | |
| 145 | - <Button | |
| 146 | - android:id="@+id/exit_chat1" | |
| 147 | - android:layout_width="wrap_content" | |
| 148 | - android:layout_height="wrap_content" | |
| 149 | - android:text="退出聊天室1" /> | |
| 150 | 99 | |
| 151 | - <Button | |
| 152 | - android:id="@+id/exit_chat2" | |
| 153 | - android:layout_width="wrap_content" | |
| 154 | - android:layout_height="wrap_content" | |
| 155 | - android:text="退出聊天室2" /> | |
| 156 | 100 | |
| 157 | 101 | |
| 158 | 102 | |
| 159 | - </LinearLayout> | |
| 160 | 103 | </LinearLayout> |
| 161 | 104 | </RelativeLayout> |
| 162 | 105 | ... | ... |
app/src/main/res/layout/activity_main.xml
app/src/main/res/layout/msg_text_view.xml
| 1 | 1 | <?xml version="1.0" encoding="utf-8"?> |
| 2 | -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| 2 | +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| 3 | 3 | android:layout_width="match_parent" |
| 4 | 4 | android:layout_height="match_parent" |
| 5 | 5 | android:orientation="horizontal"> |
| 6 | 6 | |
| 7 | - <TextView | |
| 8 | - android:id="@+id/user_name" | |
| 7 | + <RelativeLayout | |
| 8 | + android:id="@+id/re_chat_room" | |
| 9 | 9 | android:layout_width="wrap_content" |
| 10 | - android:layout_height="wrap_content" | |
| 11 | - android:gravity="center_vertical" | |
| 12 | - android:textColor="#3ce1ff" | |
| 13 | - android:textSize="14sp" /> | |
| 10 | + android:layout_height="wrap_content"> | |
| 14 | 11 | |
| 15 | - <TextView | |
| 16 | - android:id="@+id/msg_text" | |
| 17 | - android:layout_width="wrap_content" | |
| 18 | - android:layout_height="wrap_content" | |
| 19 | - android:textColor="@android:color/black" | |
| 20 | - android:textSize="15sp" /> | |
| 21 | -</LinearLayout> | |
| 22 | 12 | \ No newline at end of file |
| 13 | + <TextView | |
| 14 | + android:id="@+id/user_name" | |
| 15 | + android:layout_width="wrap_content" | |
| 16 | + android:layout_height="wrap_content" | |
| 17 | + android:gravity="center_vertical" | |
| 18 | + android:textColor="#d61d0c" | |
| 19 | + android:textSize="14sp" /> | |
| 20 | + | |
| 21 | + <TextView | |
| 22 | + android:id="@+id/msg_text" | |
| 23 | + android:layout_width="wrap_content" | |
| 24 | + android:layout_height="wrap_content" | |
| 25 | + android:layout_toRightOf="@id/user_name" | |
| 26 | + android:textColor="@android:color/black" | |
| 27 | + android:textSize="15sp" /> | |
| 28 | + </RelativeLayout> | |
| 29 | + | |
| 30 | +</RelativeLayout> | |
| 23 | 31 | \ No newline at end of file | ... | ... |