1.首先是一个项的布局文件 -----item_layout.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">
<LinearLayout
android:id="@+id/left_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="@drawable/msg"
android:layout_marginLeft="10dp">
<TextView
android:id="@+id/tv_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/right_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/msg">
<TextView
android:id="@+id/tv_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>
2.第2步消息实体类Msg
package com.example.myapplication;3 ArrayAdapter的自定义
/**msg 消息实体类
* Created by Administrator on 2015/8/1.
*/
public class Msg {
public static final int TYPE_RECEIVER=0;
public static final int TYPE_SEND=1;
private String content;//短信的内容
private int type;
@Override
public String toString() {
return super.toString();
}
public Msg() {
super();
}
public Msg(String content, int type) {
this.content = content;
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
package com.example.myapplication;4.activity的显示
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;
import java.util.List;
/**
* Created by Administrator on 2015/8/1.
*/
public class MsgAdapter extends ArrayAdapter<Msg> {
private Context context;
private int resourceId;
private List<Msg> list;
public MsgAdapter(Context context,int resourceId,List<Msg> list){
super(context,resourceId,list);
this.context=context;
this.resourceId=resourceId;
this.list=list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//
ViewHolder viewHolder=null;
Msg msg=getItem(position);
if (convertView==null){
//加载布局
viewHolder=new ViewHolder();
convertView=LayoutInflater.from(getContext()).inflate(R.layout.item_layout,null);
//查找控件
viewHolder.left_layout=(LinearLayout)convertView.findViewById(R.id.left_layout);
viewHolder.right_layout=(LinearLayout)convertView.findViewById(R.id.right_layout);
viewHolder.tv_left=(TextView)convertView.findViewById(R.id.tv_left);
viewHolder.tv_right=(TextView)convertView.findViewById(R.id.tv_right);
convertView.setTag(viewHolder);
}else{
//直接获取
viewHolder=(ViewHolder)convertView.getTag();
}
if (msg.getType()==Msg.TYPE_RECEIVER)//如果它是接收消息 就放左边
{
//显示左边 隐藏右边
viewHolder.left_layout.setVisibility(View.VISIBLE);
viewHolder.right_layout.setVisibility(View.GONE);
//设置左边的内容
viewHolder.tv_left.setText(msg.getContent());
}else{
//显示右边 隐藏左边
viewHolder.left_layout.setVisibility(View.GONE);
viewHolder.right_layout.setVisibility(View.VISIBLE);
//设置右边的内容
viewHolder.tv_right.setText(msg.getContent());
}
return convertView;
}
static class ViewHolder{
static LinearLayout left_layout;
static LinearLayout right_layout;
static TextView tv_left;
static TextView tv_right;
}
}
package com.example.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
private ListView listView;
private MsgAdapter adapter=null;
private List<Msg> list=null;
private EditText et_content;
private Button btn_send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题
setContentView(R.layout.activity_main);
//初始化控件
findView();
list=new ArrayList<Msg>();
initData();//初始化数据 将数据添加到容器中
//将数据放入到适配器中
adapter=new MsgAdapter(MainActivity.this,R.layout.item_layout,list);
listView.setAdapter(adapter);
//按钮的监听事件
btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//
String content=et_content.getText().toString().trim();
//判空
if (TextUtils.isEmpty(content)){
Toast.makeText(getApplication(),"你输入的内容为空",Toast.LENGTH_SHORT).show();
}else {
//不为空
Msg msg=new Msg(content,Msg.TYPE_SEND);
list.add(msg);
//刷新消息
adapter.notifyDataSetChanged();
listView.setSelection(list.size());//将listview定位到最后一行
et_content.setText("");//清空
}
}
});
}
public void findView(){
listView=(ListView)findViewById(R.id.listview);
et_content=(EditText)findViewById(R.id.et_content);
btn_send=(Button)findViewById(R.id.btn_send);
}
public void initData(){
Msg msg1=new Msg("你好!",Msg.TYPE_RECEIVER);
list.add(msg1);
Msg msg2=new Msg("您也好啊!",Msg.TYPE_SEND);
list.add(msg2);
Msg msg3=new Msg("有什么事吗",Msg.TYPE_RECEIVER);
list.add(msg3);
}
}