In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "Android ListView how to achieve Wechat chat interface". In daily operation, I believe many people have doubts about how to achieve Wechat chat interface with Android ListView. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "how to achieve Wechat chat interface with Android ListView". Next, please follow the editor to study!
The effect picture is as follows
1. First, the general layout of the page (ListView + LinearLayout (TextView+Button))
two。 Customize Adapter for ListView
Public class MsgAdapter extends ArrayAdapter {private int resourceID; public MsgAdapter (Context context, int resource, List objects) {super (context, resource, objects); resourceID = resource;} @ Override public View getView (int position, View convertView, ViewGroup parent) {Msg msg = getItem (position); View view; ViewHolder viewHolder; if (convertView = = null) {view = LayoutInflater.from (getContext ()). Mate (resourceID, null); viewHolder = new ViewHolder () ViewHolder.leftLayout = (LinearLayout) view.findViewById (R.id.left_layout); viewHolder.rightLayout = (LinearLayout) view.findViewById (R.id.right_layout); viewHolder.leftMsg = (TextView) view.findViewById (R.id.left_msg); viewHolder.rightMsg = (TextView) view.findViewById (R.id.right_msg); view.setTag (viewHolder);} else {view = convertView; viewHolder = (ViewHolder) view.getTag () } if (msg.getType () = = Msg.MSG_RECEIVE) {viewHolder.leftLayout.setVisibility (View.VISIBLE); viewHolder.rightLayout.setVisibility (View.GONE); viewHolder.leftMsg.setText (msg.getMessage ());} else {viewHolder.rightLayout.setVisibility (View.VISIBLE); viewHolder.leftLayout.setVisibility (View.GONE); viewHolder.rightMsg.setText (msg.getMessage ());} return view;} class ViewHolder {LinearLayout leftLayout; LinearLayout rightLayout; TextView leftMsg; TextView rightMsg }} public class Msg {public static final int MSG_RECEIVE = 0; public static final int MSG_SEND = 1; private int type; private String content; public Msg (String content, int type) {this.content = content; this.type = type;} public String getMessage () {return content;} public int getType () {return type;}}
3.ListView single view layout
4.ListView loads Adapter
Public class MainActivity extends Activity {private ListView listView; private MsgAdapter msgAdapter; private List msgList = new ArrayList (); private EditText input; private Button send; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); listView = (ListView) findViewById (R.id.msg_list_view); initMsg (); msgAdapter = new MsgAdapter (this, R.layout.msg_item, msgList); listView.setAdapter (msgAdapter) Input = (EditText) findViewById (R.id.input_text); send = (Button) findViewById (R.id.send); send.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {String message = input.getText () .toString (); if (! ".equals (message)) {Msg msg = new Msg (message, Msg.MSG_SEND); msgList.add (msg); msgAdapter.notifyDataSetChanged () / / refresh listView.setSelection (msgList.size ()) when there are new messages;} else {Toast.makeText (MainActivity.this, "input can" t be empty ", Toast.LENGTH_SHORT). Show ();} input.setText (");}});} private void initMsg () {Msg msg; msg = new Msg ("Hi, boy", Msg.MSG_RECEIVE); msgList.add (msg) Msg = new Msg ("Hi, girl", Msg.MSG_SEND); msgList.add (msg); msg = new Msg ("what" s up ", Msg.MSG_RECEIVE); msgList.add (msg);}} this ends the study of" how Android ListView implements Wechat chat interface ". I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.