網站首頁 編程語言 正文
本文實例為大家分享了Android Socket實現多個客戶端聊天布局的具體代碼,供大家參考,具體內容如下
服務器Socket接受到客戶端發送的消息之后,轉發給容器中的其他Socket,別的客戶端接受到顯示在左邊,自己發的顯示在右邊。
消息類
public class Msg {
? ? private String msg;
?
? ? private int left_right;
?
? ? public Msg(String msg,int left_right){
? ? ? ? this.msg = msg;
? ? ? ? this.left_right = left_right;
? ? }
?
? ? public String getMsg(){
? ? ? ? return msg;
? ? }
?
? ? public int getLeft_right(){
? ? ? ? return left_right;
? ? }
}
item布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:orientation="vertical" ? ? android:layout_width="match_parent" ? ? android:layout_height="wrap_content"> ? ? ? <LinearLayout ? ? ? ? android:id="@+id/left" ? ? ? ? android:background="@drawable/messageleft" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_gravity="left"> ? ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/text_left" ? ? ? ? ? ? android:textSize="16dp" ? ? ? ? ? ? android:textColor="#000" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" /> ? ? ? </LinearLayout> ? ? ? <LinearLayout ? ? ? ? android:id="@+id/right" ? ? ? ? android:layout_gravity="right" ? ? ? ? android:background="@drawable/messageright" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content"> ? ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/text_right" ? ? ? ? ? ? android:textSize="16dp" ? ? ? ? ? ? android:textColor="#000" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" /> ? ? ? </LinearLayout> ? </LinearLayout>
適配器
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder>{
? ? private List<Msg> msgs;
? ? private static final int MES_LEFT = 0,MES_RIGHT = 1;
?
? ? static class ViewHolder extends RecyclerView.ViewHolder{
?
? ? ? ? TextView text_left,text_right;
? ? ? ? LinearLayout linearLayout_left,linearLayout_right;
?
? ? ? ? public ViewHolder(View view){
? ? ? ? ? ? super(view);
? ? ? ? ? ? text_left = (TextView) view.findViewById(R.id.text_left);
? ? ? ? ? ? text_right = (TextView) view.findViewById(R.id.text_right);
? ? ? ? ? ? linearLayout_left = (LinearLayout) view.findViewById(R.id.left);
? ? ? ? ? ? linearLayout_right = (LinearLayout) view.findViewById(R.id.right);
? ? ? ? }
? ? }
?
? ? public MsgAdapter(List<Msg> msgs){
? ? ? ? this.msgs = msgs;
? ? }
?
? ? @Override
? ? public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
? ? ? ? View view = LayoutInflater.from(parent.getContext())
? ? ? ? ? ? ? ? .inflate(R.layout.msg_item,parent,false);
? ? ? ? ViewHolder holder = new ViewHolder(view);
? ? ? ? return ?holder;
? ? }
?
? ? @Override
? ? public void onBindViewHolder(ViewHolder holder, int position) {
? ? ? ? Msg msg = msgs.get(position);
?
? ? ? ? //如果顯示左邊,右邊隱藏
? ? ? ? if(msg.getLeft_right()==MES_LEFT) {
? ? ? ? ? ? holder.text_left.setText(msg.getMsg());
? ? ? ? ? ? holder.linearLayout_right.setVisibility(View.GONE);
? ? ? ? ? ? holder.linearLayout_left.setVisibility(View.VISIBLE);
? ? ? ? }
? ? ? ? //如果顯示右邊,左邊隱藏
? ? ? ? else if(msg.getLeft_right()==MES_RIGHT){
? ? ? ? ? ? holder.text_right.setText(msg.getMsg());
? ? ? ? ? ? holder.linearLayout_left.setVisibility(View.GONE);
? ? ? ? ? ? holder.linearLayout_right.setVisibility(View.VISIBLE);
? ? ? ? }
?
? ? }
?
? ? @Override
? ? public int getItemCount() {
? ? ? ? return msgs.size();
? ? }
}
效果:
原文鏈接:https://blog.csdn.net/kh971024/article/details/78498886
相關推薦
- 2022-11-23 Python?hashlib模塊與subprocess模塊使用詳細介紹_python
- 2023-04-06 C++深淺拷貝及簡易string類實現方式_C 語言
- 2022-02-16 IDEA 無法彈出模態框 HTTP 錯誤: 狀態代碼 404,net::ERR_HTTP_RESPO
- 2022-06-11 Task提高異步執行效率技巧_C#教程
- 2022-05-23 Go語言映射內部實現及基礎功能實戰_Golang
- 2022-06-25 JQuery獲取對象的方式介紹_jquery
- 2022-07-09 kubernetes之資源限制
- 2022-06-14 C語言?auto和register關鍵字_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支