日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android?Socket實現多個客戶端聊天布局_Android

作者:Frank?Kong ? 更新時間: 2022-06-23 編程語言

本文實例為大家分享了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

欄目分類
最近更新