使用FirebaseRecyclerAdapter显示所有消息时,聊天消息的背景会不断重叠。 This is the error I'm getting
**这是MessageCard
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/chattttt_linearlayout"
android:gravity="end">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
card_view:cardCornerRadius="10dp"
card_view:contentPadding="10dp"
card_view:cardElevation="4dp"
card_view:cardMaxElevation="6dp"
android:layout_gravity="end"
android:id="@+id/carddddd"
android:layout_height="wrap_content"
android:layout_margin="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sender_text_right"
android:text="Hi , How are you?"
android:padding="5dp"
android:maxWidth="250dp"
android:textSize="20sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
**这是ChatActivity firebaseRecyclerAdapter
@Override
protected void onStart() {
super.onStart();
final FirebaseRecyclerAdapter<Chat, MessageViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Chat, MessageViewHolder>(
Chat.class,
R.layout.message_card_right,
MessageViewHolder.class,
messagesDatabase
) {
@Override
protected void populateViewHolder(MessageViewHolder viewHolder, Chat model, final int position) {
viewHolder.setTextt(model.getText());
isMe = model.getUid();
if (isMe != null && !isMe.equals(currentCustomerUID)) {
viewHolder.messageLinearLayout.setGravity(Gravity.START);
viewHolder.messageText.setTextColor(getResources().getColor(android.R.color.black));
} else {
viewHolder.messageLinearLayout.setGravity(Gravity.END);
viewHolder.messageText.setTextColor(getResources().getColor(android.R.color.white));
viewHolder.messageCardView.setBackgroundResource(R.drawable.bubble_in);
}
}
};
MessageList.setAdapter(firebaseRecyclerAdapter);
}
答案 0 :(得分:0)
我认为您还应该添加此行,以确保您的CardView
获得您首选的背景资源:
if (isMe != null && !isMe.equals(currentCustomerUID)) {
....
viewHolder.messageCardView.setBackgroundResource(R.drawable.bubble_out);
} else {
...
}
不要忘记将R.drawable.bubble_out
替换为您自己的可绘制资源。我也遇到了这个问题,这就是我的所作所为。从那以后没有出错。