From fb9192e65f638fe9f0ea0de3cafa2847bd42b71d Mon Sep 17 00:00:00 2001 From: Hny0305Lin <1553809191@qq.com> Date: Wed, 3 Jul 2024 08:32:07 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89Alpha=201.3=EF=BC=8C=E6=96=B0UI?= =?UTF-8?q?=E8=81=8A=E5=A4=A9=E7=95=8C=E9=9D=A2(BETA)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nlchat/fun/ChatCore/ChatAdapter.java | 88 +++++++++++++++++++ .../nlchat/fun/ChatCore/ChatMessage.java | 19 ++++ .../nearlink/nlchat/fun/MainActivity.java | 24 +++++ .../res/drawable/bg_chat_bubble_received.xml | 6 ++ .../main/res/drawable/bg_chat_bubble_sent.xml | 6 ++ app/src/main/res/layout/activity_main.xml | 18 ++++ .../main/res/layout/item_chat_received.xml | 17 ++++ app/src/main/res/layout/item_chat_sent.xml | 17 ++++ app/src/main/res/values/strings.xml | 2 +- 9 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatAdapter.java create mode 100644 app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatMessage.java create mode 100644 app/src/main/res/drawable/bg_chat_bubble_received.xml create mode 100644 app/src/main/res/drawable/bg_chat_bubble_sent.xml create mode 100644 app/src/main/res/layout/item_chat_received.xml create mode 100644 app/src/main/res/layout/item_chat_sent.xml diff --git a/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatAdapter.java b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatAdapter.java new file mode 100644 index 0000000..f35a778 --- /dev/null +++ b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatAdapter.java @@ -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 { + + private static final int VIEW_TYPE_MESSAGE_SENT = 1; + private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2; + + private List chatMessages; + + public ChatAdapter(List 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()); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatMessage.java b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatMessage.java new file mode 100644 index 0000000..fc81619 --- /dev/null +++ b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatMessage.java @@ -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; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/MainActivity.java b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/MainActivity.java index 1883006..9099c76 100644 --- a/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/MainActivity.java +++ b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/MainActivity.java @@ -41,11 +41,15 @@ import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import com.github.clans.fab.FloatingActionMenu; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar; import com.haohanyh.linmengjia.nearlink.nlchat.ch34x.CH34xUARTDriver; +import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatAdapter; +import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatMessage; import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatProcessor; import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatUtils; import com.haohanyh.linmengjia.nearlink.nlchat.fun.Premission.NearLinkChatGetSomePermission; @@ -61,8 +65,10 @@ import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; +import java.util.List; import java.util.Objects; public class MainActivity extends AppCompatActivity implements View.OnClickListener { @@ -101,6 +107,11 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe private final int[] dataBitIds = {id.rbData5, id.rbData6, id.rbData7, id.rbData8}; private final int[] stopBitIds = {id.rbStop1, id.rbStop2}; private final int[] parityIds = {id.rbParityNone, id.rbParityOdd, id.rbParityEven, id.rbParityMark, id.rbParitySpace}; + //聊天UI 1.3更新 + private RecyclerView recyclerView; + private ChatAdapter chatAdapter; + private List chatMessages; + //Context private Context context = MainActivity.this; @@ -267,6 +278,13 @@ private void Init() { radioButton.setOnCheckedChangeListener(createCheckedChangeListener(i, "Parity")); } + //聊天UI 1.3更新 + recyclerView = findViewById(id.recycler_view); + recyclerView.setLayoutManager(new LinearLayoutManager(this)); + chatMessages = new ArrayList<>(); + chatAdapter = new ChatAdapter(chatMessages); + recyclerView.setAdapter(chatAdapter); + //初始化完成,软件第一次启动必须提示(这里写的第一次启动是软件启动的第一次,而不是使用频率的第一次 HhandlerI.sendEmptyMessage(31); //如果SQLite有记录,可以显示在UI上 @@ -406,6 +424,7 @@ private void NearLinkChatReadData() { }); } + @SuppressLint("NotifyDataSetChanged") private void updateServerTextView() { StringBuilder allMessages = new StringBuilder(); Iterator iterator = serverMessageQueue.iterator(); @@ -420,6 +439,8 @@ private void updateServerTextView() { } } NearLinkServerText.setText(allMessages.toString()); +// chatMessages.add(new ChatMessage(allMessages.toString(), false)); +// chatAdapter.notifyDataSetChanged(); Log.v(TAG, "消息队列在User上有改动"); } @@ -509,6 +530,7 @@ public void NearLinkChatSendData(View view) { } } + @SuppressLint("NotifyDataSetChanged") private void updateClientTextView() { StringBuilder allMessages = new StringBuilder(); Iterator iterator = clientMessageQueue.iterator(); @@ -523,6 +545,8 @@ private void updateClientTextView() { } } NearLinkClientText.setText(allMessages.toString()); +// chatMessages.add(new ChatMessage(allMessages.toString(), true)); +// chatAdapter.notifyDataSetChanged(); Log.v(TAG, "消息队列在Me上有改动"); } diff --git a/app/src/main/res/drawable/bg_chat_bubble_received.xml b/app/src/main/res/drawable/bg_chat_bubble_received.xml new file mode 100644 index 0000000..c3f07c7 --- /dev/null +++ b/app/src/main/res/drawable/bg_chat_bubble_received.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_chat_bubble_sent.xml b/app/src/main/res/drawable/bg_chat_bubble_sent.xml new file mode 100644 index 0000000..ce755dd --- /dev/null +++ b/app/src/main/res/drawable/bg_chat_bubble_sent.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index ca45a01..16df58d 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -196,6 +196,24 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_chat_sent.xml b/app/src/main/res/layout/item_chat_sent.xml new file mode 100644 index 0000000..8f7e37e --- /dev/null +++ b/app/src/main/res/layout/item_chat_sent.xml @@ -0,0 +1,17 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 78d09f0..0c0cbb9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,7 +1,7 @@ NLChat com.haohanyh.linmengjia.nearlink.nlchat.fun - 1.2.93.2024.0703 + 1.3.1.2024.0703 NLChat,浩瀚银河宗旨为用爱和魔法创造Android APP。 友情感谢