最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2022年04月25日 12:20:33   作者:Frank Kong  
這篇文章主要為大家詳細介紹了Android Socket實現多個客戶端聊天布局,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android Socket實現多個客戶端聊天布局的具體代碼,供大家參考,具體內容如下

服務器Socket接受到客戶端發(fā)送的消息之后,轉發(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();
? ? }
}

效果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 關于Android添加fragment后版本不兼容問題

    關于Android添加fragment后版本不兼容問題

    這篇文章主要介紹了Android添加fragment后版本不兼容問題的解決方法,需要的朋友可以參考下
    2017-12-12
  • Android實現Tab切換界面功能詳解

    Android實現Tab切換界面功能詳解

    這篇文章主要為大家詳細介紹了Android如何實現Tab切換界面的功能,以及對Tab變化事件進行監(jiān)聽。文中示例代碼講解詳細,感興趣的可以了解一下
    2022-05-05
  • 谷歌被屏蔽后如何搭建安卓環(huán)境

    谷歌被屏蔽后如何搭建安卓環(huán)境

    從5月27日開始,谷歌(Google)在華的幾乎所有的服務都處于無法使用的狀態(tài),除了搜索引擎遭到屏蔽之外,谷歌的郵箱(Gmail)、日歷(Calendar)、翻譯(Translate)、地圖(Maps)、分析(Analytics)和Google AdSense等產品也受到了影響。同時安裝安卓環(huán)境的時候同樣容易出現問題
    2014-06-06
  • Android Flutter實現點贊效果的示例代碼

    Android Flutter實現點贊效果的示例代碼

    點贊這個動作不得不說在社交、短視頻等App中實在是太常見了。本文將利用Flutter制作出一個點贊動畫效果,感興趣的小伙伴可以學習一下
    2022-04-04
  • Android 應用啟動歡迎界面廣告的實現實例

    Android 應用啟動歡迎界面廣告的實現實例

    這篇文章主要介紹了Android 應用啟動歡迎界面廣告的相關資料,需要的朋友可以參考下
    2017-05-05
  • 基于Android ContentProvider的總結詳解

    基于Android ContentProvider的總結詳解

    本篇文章是對Android ContentProvider進行了詳細的總結與分析,需要的朋友參考下
    2013-05-05
  • Android TextView預渲染研究

    Android TextView預渲染研究

    這篇文章主要介紹了Android TextView預渲染研究的相關資料,需要的朋友可以參考下
    2016-09-09
  • Android中刪除Preference詳解

    Android中刪除Preference詳解

    這篇文章主要介紹了Android中刪除Preference詳解,很多時候刪除Preference總會失敗,本文著重分析刪除失敗的原因,需要的朋友可以參考下
    2015-01-01
  • Android開發(fā)之獲取短信驗證碼后按鈕背景變化并且出現倒計時

    Android開發(fā)之獲取短信驗證碼后按鈕背景變化并且出現倒計時

    在開發(fā)是經常會遇到獲取短信驗證碼,然后獲取驗證碼后需要等待n秒倒計時,這時是不能再次發(fā)送短信請求的,這是需要一個倒計時程序,本文給大家分享了實現此功能的代碼,需要的朋友參考下
    2016-01-01
  • Mono for Android 實現高效的導航(Effective Navigation)

    Mono for Android 實現高效的導航(Effective Navigation)

    Android 4.0 系統(tǒng)定義了一系列的高效導航方式 (Effective Navigation), 主要包括標簽、下拉列表、以及向上和返回等, 本文介紹如何用 Mono for Android 實現這些的導航方式
    2012-12-12

最新評論

平乐县| 北川| 正镶白旗| 诸城市| 呼图壁县| 福鼎市| 光山县| 句容市| 遵化市| 岐山县| 延庆县| 巴南区| 睢宁县| 开江县| 永安市| 波密县| 南皮县| 南昌市| 黑水县| 朝阳区| 竹溪县| 乌苏市| 磐石市| 慈溪市| 修武县| 洛浦县| 策勒县| 天津市| 田东县| 黑水县| 迁西县| 永宁县| 台湾省| 景泰县| 东城区| 林西县| 红桥区| 和田市| 郯城县| 扬中市| 兴义市|