2016-12-24 136 views
1

使用FirebaseRecyclerAdapter顯示所有消息時,聊天消息的背景會保持重疊。 This is the error I'm gettingAndroid Firebase:聊天消息背景重疊

**這是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

我想你也應該加入這行只是爲了確保您的CardView讓你首選背景資源:

 if (isMe != null && !isMe.equals(currentCustomerUID)) { 
      .... 
      viewHolder.messageCardView.setBackgroundResource(R.drawable.bubble_out); 

     } else { 
      ... 
     } 

不要忘了將R.drawable.bubble_out替換爲您自己的drawable資源。我也遇到了這個問題,那就是我所做的。自那時以來沒有出錯。

+0

非常感謝你,它終於正常工作了 –