2017-08-17 75 views
-2

我正在開發聊天應用程序。我所做的是我能順利拿到API響應,並連續顯示他們喜歡這個在android中雙方的消息視圖

This is id of sender and reciver

This is the messages

這樣做我創建了一個適配器,它的代碼是這樣的。

public class Single_chat_adapter extends RecyclerView.Adapter<Single_chat_adapter.Single_chat_adapterViewHolder>{ 


private List<Datum2> data; 
private int rowLayout; 
private Context context; 

public Single_chat_adapter(List<Datum2> data, int rowLayout, Context context) { 
    this.data = data; 
    this.rowLayout = rowLayout; 
    this.context = context; 
} 

@Override 
public Single_chat_adapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card2, parent, false); 
    return new Single_chat_adapterViewHolder(view); } 

@Override 
public void onBindViewHolder(Single_chat_adapterViewHolder holder, int position) { 
    holder.single_msg.setText(data.get(position).getMsg()); 
} 

@Override 
public int getItemCount() { 
    return data.size(); 
} 

public class Single_chat_adapterViewHolder extends RecyclerView.ViewHolder { 

    TextView single_msg; 

    public Single_chat_adapterViewHolder(View itemView) { 
     super(itemView); 
     single_msg =itemView.findViewById(R.id.userNameTV); 
    } 
} 
} 

這裏我使用的是單個視圖,它是card2.xml。但我所需要做的是在左側設置發件人消息,在另一側設置收件人消息。 該怎麼辦?

回答

0

1)card2.xml

2)一個左,另一個用於右

3)把條件在onBindViewHolder創建2次,如果消息是來自發件人使左視圖中顯示和隱藏右視同樣的事情也爲正確的看法。

+0

您能否給我建議我可以做些什麼? – user7310707

+0

或者請提出我的詳細資料? – user7310707

+0

這是你的整個代碼中最簡單的東西,如果senderid或receiverid是你的,那麼你的消息。 –

0

要實現您已解釋的內容,請在card2.xml中創建兩個視圖(一個位於左側,另一個位於右側)。我爲你創造了一個。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:paddingStart="5dp" 
android:paddingEnd="5dp" 
android:layout_marginBottom="6dp" 
android:layout_marginTop="4dp" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
<ImageView 
    android:layout_width="40dp" 
    android:id="@+id/avatar" 
    android:background="@drawable/circle_stroke" 
    android:layout_marginStart="5dp" 
    android:layout_marginEnd="5dp" 
    android:src="@drawable/useric" 
    android:layout_height="40dp" /> 
<RelativeLayout 
    android:layout_toEndOf="@+id/avatar" 
    android:id="@+id/msg_back" 
    android:layout_marginBottom="5dp" 
    android:layout_gravity="center_vertical" 
    android:background="@drawable/message_bubble_accent" 
    android:layout_width="match_parent" 
    android:padding="10dp" 
    android:orientation="vertical" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:textSize="17sp" 
     android:id="@+id/user_text" 
     android:layout_width="wrap_content" 
     android:textColor="@color/white" 
     android:text="Hello world how are you?" 
     android:layout_height="wrap_content" /> 
    <TextView 
     android:textSize="12sp" 
     android:layout_below="@+id/user_text" 
     android:id="@+id/chat_time" 
     android:textColor="@color/dark" 
     android:text="3:33pm" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</RelativeLayout> 

    <ImageView 
     android:layout_width="40dp" 
     android:id="@+id/avatar2" 
     android:layout_alignParentEnd="true" 
     android:layout_below="@+id/msg_back" 
     android:background="@drawable/circle_stroke" 
     android:layout_marginStart="5dp" 
     android:layout_marginEnd="5dp" 
     android:src="@drawable/useric" 
     android:layout_height="40dp" /> 
    <RelativeLayout 
     android:layout_toStartOf="@+id/avatar2" 
     android:layout_below="@+id/msg_back" 
     android:id="@+id/msg_back2" 
     android:layout_gravity="center_vertical" 
     android:background="@drawable/message_bubble_white" 
     android:layout_width="match_parent" 
     android:padding="10dp" 
     android:orientation="vertical" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:textSize="17sp" 
      android:id="@+id/user_text2" 
      android:layout_width="wrap_content" 
      android:textColor="@color/white" 
      android:text="Hello world how are you?" 
      android:layout_height="wrap_content" /> 
     <TextView 
      android:layout_below="@+id/user_text2" 
      android:id="@+id/chat_time2" 
      android:textColor="@color/dark" 
      android:text="3:33pm" 
      android:textSize="12sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
     <TextView 
      android:layout_alignParentEnd="true" 
      android:layout_below="@+id/user_text2" 
      android:text="@string/sent" 
      android:width="20dp" 
      android:textAppearance="?android:textAppearanceSmall" 
      android:layout_width="20dp" 
      android:layout_height="20dp" 
      android:textColor="@android:color/holo_green_light"/> 
     </RelativeLayout> 
    </RelativeLayout> 
</LinearLayout> 

修改您的onBindViewHolder並添加如果消息是從其他用戶或不來,將檢查的條件。像這樣...

@Override 
public void onBindViewHolder(Single_chat_adapterViewHolder holder, int position) { 
    Datum2 datum = data.get(position); 
    holder.single_msg.setText(datum.getMsg()); 
    int msgId = datum.getMsgId(); 
    if (msgId == datum.getUserMsgId) { 
     //Do everything pertaining to this user here 
     holder.rightBubble.setText(single_msg); 
     //holder.rightAvatar.setText(single_msg) //For setting image 

    } else { 
     holder.leftBubble.setText(single_msg); 
    } 
} 

確保你從ViewHolder參考leftBubblerightBubble,並設置從使用該適配器的活動當前userMsgid

+0

** holder.rightBubble .setText(single_msg); **這一行顯示錯誤,它說不能解決方法setText – user7310707

+0

我說這個「確保你引用leftBubble和rightBubble從你的ViewHolder,並設置當前userMsgid從活動正在使用這個適配器。「在我的答案中。 – DevMike