Android Socket实现多个客户端聊天布局
本文实例为大家分享了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布局
适配器
public class MsgAdapter extends RecyclerView.Adapter{ private List 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 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(); } }
效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
X 关闭
X 关闭