PrivateChatAdapter.java
6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.cnlive.goldenline.ui.adapter;
import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.cnlive.goldenline.R;
import com.cnlive.goldenline.model.ChatBean;
import com.cnlive.goldenline.ui.base.BaseRecyclerAdapter;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* Created by luxiaoman on 2017/3/18.
*/
public class PrivateChatAdapter extends BaseRecyclerAdapter<ChatBean> {
public static final int MSG_TYPE_RECEIVE=0;//接收到的消息类型
public static final int MSG_TYPE_SEND=1;//发送的消息类型
public static final int MSG_TYPE_SYSTEM=2;//系统的消息类型
public static final int MSG_TYPE_LANDSCAPE=3; //横屏的消息类型
private int tag;
public PrivateChatAdapter(Context context, List<ChatBean> msgList) {
super(context);
updateItems(msgList);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
switch (viewType) {
case MSG_TYPE_RECEIVE:
viewHolder = new ChatViewReceiverHolder(LayoutInflater.from(mContext).inflate(R.layout.lv_chat_item_receive, parent, false));
break;
case MSG_TYPE_SEND:
viewHolder = new ChatViewSendHolder(LayoutInflater.from(mContext).inflate(R.layout.lv_chat_item_send, parent, false));
break;
case MSG_TYPE_SYSTEM:
viewHolder = new ChatViewSystemHolder(LayoutInflater.from(mContext).inflate(R.layout.lv_chat_item_system, parent, false));
break;
case MSG_TYPE_LANDSCAPE:
viewHolder = new ChatViewLandscapeHolder(LayoutInflater.from(mContext).inflate(R.layout.lv_chat_item_landscape, parent, false));
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ChatBean chatBean = mItems.get(position);
switch (getItemViewType(position)) {
case MSG_TYPE_RECEIVE:
ChatViewReceiverHolder chatViewReceiverHolder = (ChatViewReceiverHolder) holder;
chatViewReceiverHolder.chat_header.setImageURI(Uri.parse(chatBean.getData().getUserInfo().getHeadUrl()));
chatViewReceiverHolder.tv_chatName.setText(chatBean.getData().getUserInfo().getUserName() == null ? "" : chatBean.getData().getUserInfo().getUserName() + " : ");
chatViewReceiverHolder.tv_chatContent.setText(chatBean.getData().getContent() == null ? "" : chatBean.getData().getContent());
break;
case MSG_TYPE_SEND:
ChatViewSendHolder chatViewSendHolder = (ChatViewSendHolder) holder;
chatViewSendHolder.chat_header.setImageURI(Uri.parse(chatBean.getData().getUserInfo().getHeadUrl()));
chatViewSendHolder.tv_chatName.setText(chatBean.getData().getUserInfo().getUserName() == null ? "" : chatBean.getData().getUserInfo().getUserName() + " : ");
chatViewSendHolder.tv_chatContent.setText(chatBean.getData().getContent() == null ? "" : chatBean.getData().getContent());
break;
case MSG_TYPE_SYSTEM:
ChatViewSystemHolder chatViewSystemHolder=(ChatViewSystemHolder) holder;
chatViewSystemHolder.chat_system.setText(chatBean.getData().getUserInfo().getUserName()+"成功加入聊天大厅");
break;
case MSG_TYPE_LANDSCAPE:
ChatViewLandscapeHolder chatViewLandscapeHolder=(ChatViewLandscapeHolder) holder;
chatViewLandscapeHolder.chat_header.setImageURI(Uri.parse(chatBean.getData().getUserInfo().getHeadUrl()));
chatViewLandscapeHolder.tv_chatContent.setText(chatBean.getData().getContent() == null ? "" : chatBean.getData().getContent());
break;
}
}
@Override
public int getItemViewType(int position) {
String type = getItem(position).getType();
if ("0".equals(type)) {
tag = MSG_TYPE_RECEIVE;
} else if ("1".equals(type)) {
tag = MSG_TYPE_SEND;
} else if ("2".equals(type)) {
tag = MSG_TYPE_SYSTEM;
}else if ("3".equals(type)) {
tag = MSG_TYPE_LANDSCAPE;
}
return tag;
}
@Override
public int getItemCount() {
return mItems == null ? 0 : mItems.size();
}
public class ChatViewSendHolder extends RecyclerView.ViewHolder {
//聊天头像
@BindView(R.id.chat_header)
SimpleDraweeView chat_header;
//聊天姓名
@BindView(R.id.tv_chatName)
TextView tv_chatName;
//聊天内容
@BindView(R.id.tv_chatContent)
TextView tv_chatContent;
public ChatViewSendHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
public class ChatViewReceiverHolder extends RecyclerView.ViewHolder {
//聊天头像
@BindView(R.id.chat_header)
SimpleDraweeView chat_header;
//聊天姓名
@BindView(R.id.tv_chatName)
TextView tv_chatName;
//聊天内容
@BindView(R.id.tv_chatContent)
TextView tv_chatContent;
public ChatViewReceiverHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
public class ChatViewSystemHolder extends RecyclerView.ViewHolder {
//聊天系统消息
@BindView(R.id.chat_system)
TextView chat_system;
public ChatViewSystemHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
public class ChatViewLandscapeHolder extends RecyclerView.ViewHolder {
//横屏聊天消息
//聊天头像
@BindView(R.id.chat_header)
SimpleDraweeView chat_header;
//聊天内容
@BindView(R.id.tv_chatContent)
TextView tv_chatContent;
public ChatViewLandscapeHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}