我正在使用带有聊天消息列表的RcyclerView适配器。
每次添加到firebase的消息我都使用'onChildAdded'监听器来更新适配器列表。我还想通过添加更改颜色动画的背景来更新某些消息。 问题是我不知道如何访问我的适配器中的特定消息的位置。
这是我的chatListadapter:
class ChatListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<ChatMessage> msgList;
private static final int VIEW_TYPE_MESSAGE_LEFT = 1;
private static final int VIEW_TYPE_MESSAGE_RIGHT = 2;
private DataReferences DB;
ChatListAdapter(List<ChatMessage> msgList) {
this.msgList = msgList;
}
@Override
public int getItemCount() {
return msgList.size();
}
@Override
public int getItemViewType(int position) {
DB = DataReferences.getInstance();
ChatMessage msg = msgList.get(position);
if (!msg.uid.equals(DB.uid)) {
return VIEW_TYPE_MESSAGE_LEFT;
}
} else
return VIEW_TYPE_MESSAGE_RIGHT;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (viewType == VIEW_TYPE_MESSAGE_LEFT) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_received, parent, false);
return new LeftViewHolder(view);
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_sent, parent, false);
return new RightViewHolder(view);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int
position) {
ChatMessage msg = msgList.get(position);
if (msg.critic)
switch (holder.getItemViewType()) {
case VIEW_TYPE_MESSAGE_LEFT:
((LeftViewHolder) holder).bind(msg);
break;
case VIEW_TYPE_MESSAGE_RIGHT:
((RightViewHolder) holder).bind(msg);
break;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
private class LeftViewHolder extends RecyclerView.ViewHolder {
TextView messageText, timeText, nameText;
public LeftViewHolder(View itemView) {
super(itemView);
timeText = (TextView) itemView.findViewById(R.id.text_message_time);
nameText = (TextView) itemView.findViewById(R.id.text_message_name);
messageText = (TextView)
itemView.findViewById(R.id.text_message_body);
}
void bind(ChatMessage message) {
messageText.setText(message.getMsg());
timeText.setText(message.getTime());
nameText.setText(message.UserName);
}
}
private class RightViewHolder extends RecyclerView.ViewHolder {
TextView messageText, timeText;
public RightViewHolder(View itemView) {
super(itemView);
timeText = (TextView) itemView.findViewById(R.id.text_message_time);
messageText = (TextView)
itemView.findViewById(R.id.text_message_body);
}
void bind(ChatMessage message) {
messageText.setText(message.getMsg());
timeText.setText(message.getTime());
}
}
}
答案 0 :(得分:0)
我建议你使用FirebaseRecyclerAdapter
FirebaseUI
库(https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md)中的RecyclerView
。它使populateViewHolder
中的处理数据变得更容易。
使适配器更新特定项目。在FirebaseRecyclerAdapter
的{{1}}中,您可以执行以下操作(假设您要在阅读邮件后更改邮件背景)
override fun populateViewHolder(viewHolder: FieldViewHolder?, model: Message, position: Int) {
if(model.isRead) {
viewholder.updateBackground()
}
}
如果您使用普通RecyclerView
,可能需要付出一些努力才能找到具体位置并相应地更新用户界面。
希望有所帮助。