Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement multiple client chat layouts with Android Socket

Shulou Source: shulou.com Published: 2022-05-31 19:37:10 09月26日 Update

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.

Tags: Customer content client layout multiple right article message knowledge article value most container namely effect more server step knowledge point industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Docker NVidia Microsoft MySQL