Android Socket實現(xiàn)多個客戶端聊天布局
本文實例為大家分享了Android Socket實現(xiàn)多個客戶端聊天布局的具體代碼,供大家參考,具體內(nèi)容如下
服務(wù)器Socket接受到客戶端發(fā)送的消息之后,轉(zhuǎn)發(fā)給容器中的其他Socket,別的客戶端接受到顯示在左邊,自己發(fā)的顯示在右邊。
消息類
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(); ? ? } }
效果:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android不同activity之間共享數(shù)據(jù)解決方法
最近做局域網(wǎng)socket連接問題,要在多個activity之間公用一個socket連接,就在網(wǎng)上搜了下資料,感覺還是application方法好用,帖出來需要的朋友可以參考下2012-11-11Android 將 android view 的位置設(shè)為右下角的解決方法
Android 將 android view 的位置設(shè)為右下角的解決方法,需要的朋友可以參考一下2013-05-05解決Android TabLayout 在寬屏幕上tab不能平均分配的問題
這篇文章主要介紹了解決Android TabLayout 在寬屏幕上tab不能平均分配的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08Android中操作SQLite數(shù)據(jù)庫快速入門教程
這篇文章主要介紹了Android中操作SQLite數(shù)據(jù)庫快速入門教程,本文講解了數(shù)據(jù)庫基礎(chǔ)概念、Android平臺下數(shù)據(jù)庫相關(guān)類、創(chuàng)建數(shù)據(jù)庫、向表格中添加數(shù)據(jù)、從表格中查詢記錄等內(nèi)容,需要的朋友可以參考下2015-03-03Android利用Theme自定義Activity間的切換動畫
這篇文章主要為大家詳細介紹了Android利用Theme自定義Activity間的切換動畫,感興趣的小伙伴們可以參考一下2016-09-09解決Android Studio Log.v和Log.d不顯示的問題
這篇文章主要介紹了解決Android Studio Log.v和Log.d不顯示的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08Android多設(shè)備多module打包fat-aar(最新推薦)
這篇文章主要介紹了Android多設(shè)備多module打包(fat-aar),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03