1.首先编写main.xml文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp" > <LinearLayout android:id="@+id/left_layout" android:layout_width="wrap_content" android:layout_height="45dp" android:layout_gravity="left" android:background="@drawable/message_left" > <TextView android:id="@+id/left_msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:textColor="#fff" /> </LinearLayout> <LinearLayout android:id="@+id/right_layout" android:layout_width="wrap_content" android:layout_height="45dp" android:layout_gravity="right" android:background="@drawable/message_right" > <TextView android:id="@+id/right_msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" /> </LinearLayout> </LinearLayout>
2.然后来定义一个消息实体类Msg:
package org.lxh.demo; public class Msg { public static final int TYPE_RECEIVED = 1; public static final int TYPE_SENT = 0; private String content; private int type; public Msg(String content, int type) { this.content = content; this.type = type; } public String getContent() { return content; } public int getType() { return type; } }
3.新建msg_item.xml代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp" > <LinearLayout android:id="@+id/left_layout" android:layout_width="wrap_content" android:layout_height="45dp" android:layout_gravity="left" android:background="@drawable/message_left" > <TextView android:id="@+id/left_msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:textColor="#fff" /> </LinearLayout> <LinearLayout android:id="@+id/right_layout" android:layout_width="wrap_content" android:layout_height="45dp" android:layout_gravity="right" android:background="@drawable/message_right" > <TextView android:id="@+id/right_msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" /> </LinearLayout> </LinearLayout>
4.新建MsgAdapter继承自ArrayAdapter:
package org.lxh.demo; import java.util.List; import android.R.integer; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.TextView; public class MsgAdapter extends ArrayAdapter<Msg> { private int resourceId; public MsgAdapter(Context context, int textViewResourceId, List<Msg> objects) { super(context, textViewResourceId, objects); resourceId = textViewResourceId; } @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()).inflate(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.TYPE_RECEIVED) {// 判断消息类型,决定哪边显示 viewHolder.leftLayout.setVisibility(View.VISIBLE); viewHolder.rightLayout.setVisibility(View.GONE); viewHolder.leftMsg.setText(msg.getContent()); } else if (msg.getType() == Msg.TYPE_SENT) { viewHolder.rightLayout.setVisibility(View.VISIBLE); viewHolder.leftLayout.setVisibility(View.GONE); viewHolder.rightMsg.setText(msg.getContent()); } return view; } class ViewHolder { LinearLayout leftLayout; LinearLayout rightLayout; TextView leftMsg; TextView rightMsg; } }
5.MainActivity:
package org.lxh.demo; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnFocusChangeListener; import android.view.Window; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; public class Hello extends Activity { private ListView msgListView; private EditText inputText; private Button send; private MsgAdapter adapter; private List<Msg> msgList=new ArrayList<Msg>(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 生命周期方法 requestWindowFeature(Window.FEATURE_NO_TITLE); super.setContentView(R.layout.main); // 设置要使用的布局管理器 initMsg(); adapter=new MsgAdapter(Hello.this, R.layout.msg_item, msgList); inputText=(EditText)findViewById(R.id.input_text); send=(Button)findViewById(R.id.send); msgListView=(ListView)findViewById(R.id.msg_list_view); msgListView.setAdapter(adapter); send.setOnClickListener(new OnClickListener() { public void onClick(View v) { String content=inputText.getText().toString(); if(!"".equals(content)){ Msg msg=new Msg(content, Msg.TYPE_SENT); msgList.add(msg); adapter.notifyDataSetChanged();//当有县消息时刷新ListView中的显示 msgListView.setSelection(msgList.size());//将ListView定位到最后一行 inputText.setText(""); } } }); } private void initMsg() { Msg msg1=new Msg("Hi Peter", Msg.TYPE_RECEIVED); msgList.add(msg1); Msg msg2=new Msg("Hello Dog", Msg.TYPE_SENT); msgList.add(msg2); Msg msg3=new Msg("How are you?", Msg.TYPE_RECEIVED); msgList.add(msg3); } }
运行实例如下:
源码下载地址:http://download.csdn.net/detail/yayun0516/8736781
喜欢的朋友可以关注我!
原文地址:http://blog.csdn.net/yayun0516/article/details/45969847