在我的Android火力聊天應用程序,我想用recyclerview根據傳入或傳出消息的類型即recyclerview兩個viewtypes不chaning viewtype
讓我先告訴你的火力點數據結構顯示chatlist
<ConversationId>
<MsgId>
text :"Hello"
timestamp :<timestamp>
sender :<sender's uid>
receiver :<receiver's uid>
以下是我的適配器類
public class MultiViewTypeAdapter extends RecyclerView.Adapter {
private List<SingleChatList> chatList;
int total_types;
public class incoming extends RecyclerView.ViewHolder
{
public TextView message,time;
//public CircleImageView dp;
public incoming(View view)
{
super(view);
message=(TextView)view.findViewById(R.id.message_text);
time=(TextView)view.findViewById(R.id.timestamp_text);
}
}
public class outgoing extends RecyclerView.ViewHolder
{
public TextView message,time;
//public CircleImageView dp;
public outgoing(View view)
{
super(view);
message=(TextView)view.findViewById(R.id.message_text_view);
time=(TextView)view.findViewById(R.id.timestamp_text_view);
}
}
public MultiViewTypeAdapter(List<SingleChatList> data) {
this.chatList = data;
total_types = chatList.size();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case 0:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_incoming_bubble, parent, false);
return new incoming(view);
case 1:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_outgoing_bubble, parent, false);
return new outgoing(view);
}
return null;
}
@Override
public int getItemViewType(int position) {
int swi=0;
if(chatList.get(position).getSender()== FirebaseAuth.getCurrentUser().getUid())
{
swi=1;
}
else
{
swi=0;
}
return swi;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition) {
SingleChatList chat=chatList.get(listPosition);
if (chat != null) {
int swi=0;
if(chat.getSender()== FirebaseAuth.getCurrentUser().getUid())
{
swi=1;
}
else
{
swi=0;
}
switch (swi) {
case 0:
((incoming) holder).message.setText(chat.getMsg());
((incoming) holder).time.setText(String.valueOf(chat.getTimestamp()));
break;
case 1:
((outgoing) holder).message.setText(chat.getMsg());
((incoming) holder).time.setText(String.valueOf(chat.getTimestamp()));
break;
}
}
}
@Override
public int getItemCount() {
return chatList.size();
}
}
在這裏,我使用下面的邏輯
0決定viewtypesenderuid== FirebaseAuth.getCurrentUser().getUid();
如果上述條件爲真,那麼它應該是一個傳出消息。
我運行上面的代碼,但所有的消息只能進入味精佈局應用
onBindViewHolder函數使用holder.getItemViewType()獲取視圖類型 – Nidhin