Android-ListView类

  ListView组件在应用程序中可以说是不可或缺的一部分,ListView主要是显示列表数据,同时可以滚动查看,这篇博客主要是对ListView的基本用法进行说明,后面会依次对ListView点击动态加载,上拉加载,下拉刷新,异步获取网络图片进行显示等几个在应用中常用到的功能来进行讲解。ListView的继承结构如下:

  Android-ListView类

既然ListView是用来显示数据的,那么就要往里面添加数据,怎么添加数据进入ListView就是今天的重点,下面来进行实现

系统显示列表(ListView)时,首先会实例化一个适配器,本文将实例化一个自定义的适配器。实现 
自定义适配器,必须手动映射数据,这时就需要重写getView()方法,系统在绘制列表的每一行的时候 
将调用此方法。 
ListView在开始绘制的时候,系统自动调用getCount()函数,根据函数返回值得到ListView的长度, 
然后根据这个长度,调用getView()逐一画出每一行。 
具体使用方法可以参考下面代码,只需记住Android自定义ListView三步骤: 
第一步:准备主布局文件、组件布局文件等 
第二步:获取并整理数据 
第三部:绑定数据,这里我们是通过自己编写Adapter类来完成的

拿昨天的写的微信界面来继续实现ListView

微信界面链接:http://www.cnblogs.com/liangshijie/p/6100297.html

首先,我们新建一个listview_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" >
<!-- 添加一个ListView -->
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" > </ListView> </LinearLayout>

再新建一个item布局的布局文件,让我们适配器生成按照该布局输出数据:

listview_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="horizontal" > <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:src="@drawable/tou1" /> <LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="vertical"
android:layout_margin="5dp"
>
<TextView android:id="@+id/tv_userName"
android:layout_width="match_parent"
android:layout_height="20dp"
/> <TextView
android:id="@+id/tv_lastMessage"
android:layout_width="match_parent"
android:layout_height="15dp"
/> </LinearLayout> <TextView
android:id="@+id/tv_datetime"
android:layout_width="150dp"
android:layout_height="15dp"
/>
</LinearLayout>

然后把listview_layout.xml在我们之前微信写好的weixin.xml中间LinearLayout中:

<?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" >
<!-- head -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include layout="@layout/head"/>
</LinearLayout> <!-- 中间 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<!-- 引入listview_layout.xml(也就是ListView布局) -->
<include layout="@layout/listview_layout"/>
</LinearLayout> <!-- 底部 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include layout="@layout/bottom"/>
</LinearLayout> </LinearLayout>

新建一个Message实体类:

  

 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;
}
}

接下来就是在Activity中初始化控件和添加适配器了:

MainActivity.java:

  

 package com.example.winxin2;

 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.Menu;
import android.view.MenuItem;
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>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weixin); //模拟读取数据库或者互联网
for (int i = 0; i < 100; i++) {
Message m = new Message();
m.setTou1("xxx");
m.setUserName("张三疯"+i);
m.setLastMessage("今晚有约,一起华山论剑");
m.setDatetime("11月11日");
messageList.add(m);
} lv = (ListView)findViewById(R.id.listView1);
//简单理解为VC绑在一起
lv.setAdapter( new BaseAdapter(){ //返回多少条记录
@Override
public int getCount() {
// TODO Auto-generated method stub
return messageList.size();
}
//每一个item项,返回一次界面
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null; //布局不变,数据变 //如果缓存为空,我们生成新的布局作为1个item
if(convertView==null){
Log.i("info:", "没有缓存,重新生成"+position);
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
//因为getView()返回的对象,adapter会自动赋给ListView
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() );
tv_userName.setTextSize(15); TextView tv_lastMessage = (TextView)view.findViewById(R.id.tv_lastMessage);
tv_lastMessage.setText( m.getLastMessage() );
tv_lastMessage.setTextSize(12); TextView tv_datetime = (TextView)view.findViewById(R.id.tv_datetime);
tv_datetime.setText( m.getDatetime() );
tv_datetime.setTextSize(12); return view;
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
} } ); } }

运行结果:

Android-ListView类

可以下拉:

  Android-ListView类

  

上一篇:深入浅出数据结构C语言版(14)——散列表


下一篇:APUE学习之多线程编程(一):线程的创建和销毁