Android Socket实现多个客户端聊天布局

作者:Frank 时间:2022-10-09 08:31:19 

本文实例为大家分享了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();
? ? }
}

效果:

Android Socket实现多个客户端聊天布局

Android Socket实现多个客户端聊天布局

来源:https://blog.csdn.net/kh971024/article/details/78498886

标签:Android,Socket,聊天
0
投稿

猜你喜欢

  • java poi导出图片到excel示例代码

    2023-10-30 00:13:17
  • java 装饰模式(Decorator Pattern)详解

    2023-08-10 09:56:21
  • SpringMVC+Mybatis实现的Mysql分页数据查询的示例

    2023-11-24 20:53:33
  • Java实现微信公众号发送模版消息

    2021-07-16 17:03:35
  • Java实现一个简单的定时器代码解析

    2021-11-24 20:25:38
  • spring mvc实现文件上传并携带其他参数的示例

    2023-11-20 11:54:06
  • Java爬虫实现爬取京东上的手机搜索页面 HttpCliient+Jsoup

    2023-02-19 23:22:37
  • SpringBoot整合Shiro实现登录认证的方法

    2022-03-23 01:12:19
  • Spring组件开发模式支持SPEL表达式

    2023-09-05 11:53:31
  • Java操作FreeMarker模板引擎的基本用法示例小结

    2022-01-15 11:34:15
  • Java 设计模式中的策略模式详情

    2023-08-06 03:45:11
  • Android apk 插件启动内存释放问题

    2022-05-16 07:26:39
  • Eclipse项目有红感叹号的解决方法

    2023-02-13 10:54:15
  • Mybatis-Plus查询中如何排除标识字段

    2023-11-23 20:38:46
  • 通过JDK源码角度分析Long类详解

    2022-03-11 19:26:38
  • ERROR/AndroidRuntime(17121)的问题解决

    2023-02-10 04:13:12
  • 详解java中保持compareTo和equals同步

    2023-07-20 12:20:53
  • Java 创建PDF打印小册子案例

    2023-01-17 04:19:40
  • 基于Mybatis plus 自动代码生成器的实现代码

    2023-11-24 10:40:51
  • 深入理解Java中的克隆

    2023-03-14 02:59:14
  • asp之家 软件编程 m.aspxhome.com