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

Android Socket實(shí)現(xiàn)多個(gè)客戶端聊天布局

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

本文實(shí)例為大家分享了Android Socket實(shí)現(xiàn)多個(gè)客戶端聊天布局的具體代碼,供大家參考,具體內(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

晋中市| 灵武市| 云浮市| 玛多县| 成武县| 抚松县| 农安县| 绵竹市| 大石桥市| 达孜县| 伊春市| 邵武市| 阿勒泰市| 临桂县| 霍山县| 崇州市| 南漳县| 沾化县| 荣成市| 大安市| 深泽县| 丘北县| 仲巴县| 宾阳县| 玉环县| 杨浦区| 昌宁县| 太湖县| 出国| 涟水县| 读书| 紫金县| 闸北区| 竹溪县| 庄浪县| 库伦旗| 余庆县| 珲春市| 临泽县| 怀来县| 赤城县|