2024/6/11

所学时间:2小时

代码行数:200

博客园数:1篇

所学知识:

<?xml version="1.0" encoding="utf-8"?>
<!-- activity_main.xml -->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- 消息列表 -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_messages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/layout_message_input"
android:padding="10dp"
android:clipToPadding="false"
android:scrollbars="vertical" />

<!-- 消息输入框布局 -->
<LinearLayout
android:id="@+id/layout_message_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:padding="10dp">

<!-- 消息输入框 -->
<EditText
android:id="@+id/edit_text_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="输入消息..."
android:inputType="textMultiLine"
android:minLines="1"
android:maxLines="5"
android:gravity="top" />

<!-- 发送按钮 -->
<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送" />

</LinearLayout>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- item_message.xml -->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_view_message"
android:layout_width="match_parent"
android:layout_height="40dp"
android:padding="8dp"
android:textSize="16sp"
android:textColor="@android:color/black"
tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.test30;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {

private List<String> messageList;

public MessageAdapter(List<String> messageList) {
this.messageList = messageList;
}

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

@Override
public void onBindViewHolder(@NonNull MessageViewHolder holder, int position) {
String message = messageList.get(position);
holder.messageTextView.setText(message);
}

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

public static class MessageViewHolder extends RecyclerView.ViewHolder {
TextView messageTextView;

public MessageViewHolder(View itemView) {
super(itemView);
messageTextView = itemView.findViewById(R.id.text_view_message);
}
}
}
package com.example.test30;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private MessageAdapter messageAdapter;
private List<String> messageList;
private EditText messageEditText;
private Button sendButton;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

recyclerView = findViewById(R.id.recycler_view_messages);
messageEditText = findViewById(R.id.edit_text_message);
sendButton = findViewById(R.id.button_send);

messageList = new ArrayList<>();
messageAdapter = new MessageAdapter(messageList);
recyclerView.setAdapter(messageAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = messageEditText.getText().toString();
sendMessage(message);
messageEditText.setText(""); // 清空输入框
}
});

// 模拟接收消息
simulateIncomingMessage();
}

// 发送消息的方法
private void sendMessage(String message) {
// 发送消息到服务器
// 这里可以使用网络请求或其他技术来发送消息到后端服务器
// 在这里假设直接添加到消息列表
messageList.add("我:" + message);
messageAdapter.notifyDataSetChanged(); // 更新RecyclerView
}

// 模拟接收消息的方法,实际应用中应该从后端服务器接收消息
private void simulateIncomingMessage() {
// 模拟从服务器接收到的消息
String message = "你好,世界!";
receiveMessage(message);
}

// 接收消息的方法
private void receiveMessage(String message) {
// 更新UI以显示新消息
// 这里可以将收到的消息添加到消息列表中,并更新RecyclerView的数据源
// 在这里假设直接添加到消息列表
messageList.add("对方:" + message);
messageAdapter.notifyDataSetChanged(); // 更新RecyclerView
}
}
posted @ 2024-06-11 21:19  为20岁努力  阅读(4)  评论(0编辑  收藏  举报