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 imitate Wechat chat Interface by Android ListView

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

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "Android ListView how to achieve imitating Wechat chat interface", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Android ListView how to achieve imitating Wechat chat interface" bar!

Effect picture:

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 ());} 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);}} so far, I believe you have a deeper understanding of "how Android ListView implements a Wechat-like chat interface". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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