Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement multiple client chat layouts with Android Socket

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article "Android Socket how to achieve multiple client chat layout" most people do not understand, so the editor summarized the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "Android Socket how to achieve multiple client chat layout" article bar.

After receiving the message sent by the client, the server Socket forwards it to other Socket in the container, which is displayed on the left by the other client and on the right by the other client.

Message class

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 layout

Adaptor

Public class MsgAdapter extends RecyclerView.Adapter {private List msgs; private static final int MES_LEFT = 0Med 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 ()) .expiate (R.layout.msgonomitem.parentma.false); ViewHolder holder = new ViewHolder (view); return holder;} @ Override public void onBindViewHolder (ViewHolder holder, int position) {Msg msg = msgs.get (position) / / if left side is shown, right side hides if (msg.getLeft_right () = = MES_LEFT) {holder.text_left.setText (msg.getMsg ()); holder.linearLayout_right.setVisibility (View.GONE); holder.linearLayout_left.setVisibility (View.VISIBLE) } / / if the right side is shown, hide 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) on the left @ Override public int getItemCount () {return msgs.size ();}}

Effect:

The above is about the content of this article on "how to achieve multiple client chat layouts in Android Socket". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report