我想用firebase db创建一个聊天应用程序。当我想根据UID消息替换聊天消息位置时,挑战就来了。
我的回收站适配器
protected RecyclerView.Adapter newAdapter() {
Query sItemQuery = mDatabase.child("chat_messages").child(chatId).limitToLast(50);
FirebaseRecyclerOptions<ChatMessage> options =
new FirebaseRecyclerOptions.Builder<ChatMessage>()
.setQuery(sItemQuery, ChatMessage.class)
.setLifecycleOwner(this)
.build();
return new FirebaseRecyclerAdapter<ChatMessage, ChatMessageViewHolder>(options) {
@NonNull
@Override
public ChatMessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ChatMessageViewHolder holder = new ChatMessageViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.single_chat_bubble, parent, false), getApplication());
return holder;
}
@Override
public int getItemViewType(int position) {
if (isSender(position)) {
return R.layout.single_chat_page_right;
} else {
return R.layout.single_chat_bubble;
}
}
@Override
public void onDataChanged() {
// If there are no items, show a view that invites the user to add a message.
//mEmptyListMessage.setVisibility(getItemCount() == 0 ? View.VISIBLE : View.GONE);
}
@Override
protected void onBindViewHolder(@NonNull ChatMessageViewHolder holder, int position, @NonNull ChatMessage model) {
holder.bind(model);
holder.setOnClickListener(new ChatMessageViewHolder.ClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(getApplicationContext(), "", Toast.LENGTH_LONG).show();
}
@Override
public void onItemLongClick(View view, int position) {
}
});
}
};
}
我要检查Uid ==当前UId。
我也尝试过这种方法,但是我在get Item View Type上找不到模型
private boolean isSender(int position) {
if(position % 2 == 0){
return true;
}
return false;
// user.getUid().equals(getItem(position).getSender());
}