一、实验目的
(1)掌握ListView的使用
(2)掌握如何自定义Adapter
(3)掌握制作Nine-Patch图片
二、实验内容使用ListView编写一个简单的聊天界面,可以发送消息显示在屏幕上,不管消息多长背景图片都不会因为放大而失真。
三、实验结果图
四、实验代码
Msg类:
package com.zl5.chat;
public class Msg {
public static final int RECEIVED = 0;
public static final int SENT = 1;
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;
}
}
MsgAdapter:
package com.zl5.chat;
import java.util.List;
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> {
public int tv_layout;
public MsgAdapter(Context context, int tvresource, List<Msg> objects) {
super(context, tvresource, objects);
// TODO Auto-generated constructor stub
tv_layout = tvresource;
}
@SuppressWarnings("static-access")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Msg msg = getItem(position);
View view;
ViewHolder viewholder;
if(convertView == null){
view = LayoutInflater.from(getContext()).inflate(tv_layout, null);
viewholder = new ViewHolder();
viewholder.left_layout = (LinearLayout) view.findViewById(R.id.left_layout);
viewholder.right_layout = (LinearLayout) view.findViewById(R.id.right_layout);
viewholder.left_msg = (TextView) view.findViewById(R.id.left_msg);
viewholder.right_msg = (TextView) view.findViewById(R.id.right_msg);
view.setTag(viewholder);
}
else{
view = convertView;
viewholder = (ViewHolder) view.getTag();
}
if(msg.getType() == msg.RECEIVED){
viewholder.left_layout.setVisibility(View.VISIBLE);
viewholder.right_layout.setVisibility(View.GONE);
viewholder.left_msg.setText(msg.getContent());
}
else if(msg.getType() == msg.SENT){
viewholder.right_layout.setVisibility(View.VISIBLE);
viewholder.left_layout.setVisibility(View.GONE);
viewholder.right_msg.setText(msg.getContent());
}
return view;
}
}
class ViewHolder{
LinearLayout left_layout;
LinearLayout right_layout;
TextView left_msg;
TextView right_msg;
}
MainActivity:
package com.zl5.chat;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView msg_list;
private EditText input;
private Button send;
private MsgAdapter adapter;
private List<Msg> msglist = new ArrayList<Msg>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initMsg();
input = (EditText) findViewById(R.id.input);
send = (Button) findViewById(R.id.send);
msg_list = (ListView) findViewById(R.id.msg_list);
adapter = new MsgAdapter(MainActivity.this, R.layout.msg_item, msglist);
msg_list.setAdapter(adapter);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
String content = input.getText().toString();
if (!"".equals(content)) {
Msg msg = new Msg(content, Msg.SENT);
msglist.add(msg);
adapter.notifyDataSetChanged();
msg_list.setSelection(msglist.size());
input.setText("");
}
}
});
}
public void initMsg(){
Msg msg1 = new Msg("你好~", Msg.RECEIVED);
msglist.add(msg1);
Msg msg2 = new Msg("Hello.你是谁?", Msg.SENT);
msglist.add(msg2);
Msg msg3 = new Msg("我是下铺的。 我们可以聊聊吗? ", Msg.RECEIVED);
msglist.add(msg3);
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/chutian"
android:orientation="vertical" >
<ListView
android:id="@+id/msg_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="#0000" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/hint"
android:maxLines="2" />
<Button
android:id="@+id/send"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/send" />
</LinearLayout>
</LinearLayout>
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:padding="10dp"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/left_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@drawable/left_chat" >
<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="wrap_content"
android:layout_gravity="end"
android:background="@drawable/right_chat" >
<TextView
android:id="@+id/right_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
经过Nine-Patch制作后的图片:
五、存在的问题
本文中的消息接收是自己添加的,想要实现真正的消息收发需要继续学习呀,另外注意气泡图片背景制作,画上黑线的地方是可拉伸的部分,参考书上有错误,左右上下画的黑线应对称,不然就按照长的线进行拉伸。
PS:上面的所有方法会在自学笔记中详细介绍