android 模拟微信消息框 BaseAdapter()方法 [2]
在昨天的微信布局的基础上加内容 http://www.cnblogs.com/Seven-cjy/p/6098024.html
项目下/res/layout下创建一个 listview_layout.xml 的 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" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout>
项目下/res/layout下创建一个 listview_item_layout.xml 的 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="70dp" android:orientation="horizontal" > <ImageView android:id="@+id/imageView1" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginBottom="10dp" android:layout_marginLeft="13dp" android:layout_marginRight="13dp" android:layout_marginTop="10dp" android:src="@drawable/a" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="70dp" android:layout_weight="1" android:orientation="vertical" > <TextView android:id="@+id/tv_userName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:ellipsize="end" android:singleLine="true" android:textColor="@color/darkslategray" android:textSize="16sp" /> <TextView android:id="@+id/tv_lasMessage" android:layout_width="match_parent" android:layout_height="15dp" android:layout_marginBottom="15dp" android:layout_marginTop="8dp" android:ellipsize="end" android:gravity="bottom" android:singleLine="true" android:textColor="@color/gray" android:textSize="11sp" /> </LinearLayout> <LinearLayout android:layout_width="40dp" android:layout_height="60dp" android:layout_marginLeft="10dp" android:orientation="vertical" > <TextView android:id="@+id/tv_datetime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:layout_weight="1" android:gravity="top" android:textColor="@color/gray" android:textSize="11sp" /> </LinearLayout> </LinearLayout>
项目下 src 下 创建一个package的包 创建Message.java文件 (如:/项目/src/com/example/entity/Message.java)
package com.example.entity; public class Message { private String tou1; private String userName; private String lastMessage; private String datetime; public String getTou1() { return tou1; } public void setTou1(String tou1) { this.tou1 = tou1; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getLastMessage() { return lastMessage; } public void setLastMessage(String lastMessage) { this.lastMessage = lastMessage; } public String getDatetime() { return datetime; } public void setDatetime(String datetime) { this.datetime = datetime; } }
项目下 /src/com/example/winxinmff/MainActivity.java 文件
package com.example.winxinmff; import java.util.ArrayList; import java.util.List; import com.example.entity.Message; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { private ListView lv; private List<Message> messageList = new ArrayList<Message>(); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.weixin); // 模拟读取数据库或者互联网 for (int i = 0; i < 20; i++) { // 模拟20条数据 Message p = new Message(); p.setTou1("xxx"); p.setUserName("不对外开放 高内聚 "); p.setLastMessage("匿名内部类也就是没有名字的内部类匿名内部类也就是没有名字的内部类"); p.setDatetime("11.11"); messageList.add(p); } lv = (ListView) findViewById(R.id.listView1); // 简单理解为VC绑在一起 lv.setAdapter(new BaseAdapter() { private View view; public int getCount() { // 返回多少条记录 return messageList.size(); } // 每一个item项, 返回一次界面 public View getView(int position, View convertView, ViewGroup parent) { // 布局不变,数据变 // 如果缓存为空 ,我们生成新的布局作为1个 item if (convertView == null) { Log.i("info: ", "没有缓存,重新生成" + position); LayoutInflater inflater = MainActivity.this.getLayoutInflater(); // 因为 getView() 返回的对象, adapter 会自动赋给 List view = inflater.inflate(R.layout.listview_item_layout, null); } else { Log.i("info: ", "有缓存,不需要重新生成" + position); view = convertView; } Message m = messageList.get(position); TextView tv_userName = (TextView) view.findViewById(R.id.tv_userName); tv_userName.setText(m.getUserName() + position); TextView tv_lasMessage = (TextView) view.findViewById(R.id.tv_lasMessage); tv_lasMessage.setText(m.getLastMessage()); TextView tv_datetime = (TextView) view.findViewById(R.id.tv_datetime); tv_datetime.setText(m.getDatetime()); return view; } public long getItemId(int position) { return 0; } public Object getItem(int position) { return null; } }); } }