-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
570d087
commit fb9192e
Showing
9 changed files
with
196 additions
and
1 deletion.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore; | ||
|
||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.TextView; | ||
|
||
import androidx.recyclerview.widget.RecyclerView; | ||
|
||
import com.haohanyh.linmengjia.nearlink.nlchat.fun.R; | ||
|
||
import java.util.List; | ||
|
||
public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | ||
|
||
private static final int VIEW_TYPE_MESSAGE_SENT = 1; | ||
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2; | ||
|
||
private List<ChatMessage> chatMessages; | ||
|
||
public ChatAdapter(List<ChatMessage> chatMessages) { | ||
this.chatMessages = chatMessages; | ||
} | ||
|
||
@Override | ||
public int getItemViewType(int position) { | ||
ChatMessage message = chatMessages.get(position); | ||
if (message.isSent()) { | ||
return VIEW_TYPE_MESSAGE_SENT; | ||
} else { | ||
return VIEW_TYPE_MESSAGE_RECEIVED; | ||
} | ||
} | ||
|
||
@Override | ||
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | ||
if (viewType == VIEW_TYPE_MESSAGE_SENT) { | ||
View view = LayoutInflater.from(parent.getContext()) | ||
.inflate(R.layout.item_chat_sent, parent, false); | ||
return new SentMessageHolder(view); | ||
} else { | ||
View view = LayoutInflater.from(parent.getContext()) | ||
.inflate(R.layout.item_chat_received, parent, false); | ||
return new ReceivedMessageHolder(view); | ||
} | ||
} | ||
|
||
@Override | ||
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { | ||
ChatMessage message = chatMessages.get(position); | ||
if (holder.getItemViewType() == VIEW_TYPE_MESSAGE_SENT) { | ||
((SentMessageHolder) holder).bind(message); | ||
} else { | ||
((ReceivedMessageHolder) holder).bind(message); | ||
} | ||
} | ||
|
||
@Override | ||
public int getItemCount() { | ||
return chatMessages.size(); | ||
} | ||
|
||
private class SentMessageHolder extends RecyclerView.ViewHolder { | ||
TextView messageText; | ||
|
||
SentMessageHolder(View itemView) { | ||
super(itemView); | ||
messageText = itemView.findViewById(R.id.text_message_body); | ||
} | ||
|
||
void bind(ChatMessage message) { | ||
messageText.setText(message.getMessage()); | ||
} | ||
} | ||
|
||
private class ReceivedMessageHolder extends RecyclerView.ViewHolder { | ||
TextView messageText; | ||
|
||
ReceivedMessageHolder(View itemView) { | ||
super(itemView); | ||
messageText = itemView.findViewById(R.id.text_message_body); | ||
} | ||
|
||
void bind(ChatMessage message) { | ||
messageText.setText(message.getMessage()); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore; | ||
|
||
public class ChatMessage { | ||
private String message; | ||
private boolean isSent; | ||
|
||
public ChatMessage(String message, boolean isSent) { | ||
this.message = message; | ||
this.isSent = isSent; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public boolean isSent() { | ||
return isSent; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<shape xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<solid android:color="#E5E5EA" /> | ||
<corners android:radius="16dp" /> | ||
<padding android:left="8dp" android:top="8dp" android:right="8dp" android:bottom="8dp" /> | ||
</shape> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<shape xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<solid android:color="#0084FF" /> | ||
<corners android:radius="16dp" /> | ||
<padding android:left="8dp" android:top="8dp" android:right="8dp" android:bottom="8dp" /> | ||
</shape> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?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:orientation="horizontal" | ||
android:padding="8dp" | ||
android:gravity="start"> | ||
|
||
<TextView | ||
android:id="@+id/text_message_body" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:background="@drawable/bg_chat_bubble_received" | ||
android:padding="10dp" | ||
android:textColor="@android:color/black" | ||
android:text="Hello, this is a received message!" /> | ||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?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:orientation="horizontal" | ||
android:padding="8dp" | ||
android:gravity="end"> | ||
|
||
<TextView | ||
android:id="@+id/text_message_body" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:background="@drawable/bg_chat_bubble_sent" | ||
android:padding="10dp" | ||
android:textColor="@android:color/white" | ||
android:text="Hello, this is a sent message!" /> | ||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters