ListView的使用和特性研究(二)
转载自:http://www.android777.com/index.php/tutorial/listview-2.html
上篇文章 我们讲到如何通过扩展ArrayAdapter来将其里面所装的对象展现在自定义的View里,其中有涉及到展现ListView的效率问题。这边我们在分 析下到底ListView是如何创建整个UI,而我们要如何才能写出运行效率高的代码,也顺便了解下如何使用android内置的支持更方便的编写一个 ListView。
假设我们要显示一个有1000行数据的ListView,这样的数据量在手机中应该算很庞大了,那ListView会如何处理呢,以让系统运行时占用的空间时间最优化呢?
其实在ListView中有使用懒加载机制以提高显示的效率。在很多系统架构中我们都可以看到懒加载机制,如hibernate的懒加载机 制,spring bean也有懒加载,所谓的懒加载应该是在需要时才用到,不需要时能不创建就不创建,尽量减少系统消耗。就以ListView来说:假设ListView 总共要显示1000条数据,但是用户一次仅能看到10条左右,那就有其余的990条是看不到的,这时候系统可以不用创建这些View,等用户往下或往上拉 动数据再创建新的要显示的View,这就是ListView中的懒加载机制。所以我们在写代码时候要特别注意这点,这边用图形来解释下这个机制。
如上面实际上要显示的条目可能有几十上百条,但是因为手机一个屏幕只能看到4条,所以ListView就创建4个View来显示ListView内部的Item,当用户往下拉动的时候:
当用户往下拉动时,原本装Item1的视图就看不到了,而本来看不到的Item5就显示出来了,系统还是一样只能看到4条item。ListView就将 本来装Item1的View拖出来,放到最底端,然后更新里面的数据变成Item5,但是此时系统还是一样只创建了4个View,这样就节省了很多 View的创建时间,只需要做替换内容的操作,提高了系统的效率。因此我们可以得出一个结论:假设每个View里面有4个TextView,因为在这个 ListView中,系统一屏只能显示4个View,所以总共只需要创建16个TextView,而不管ListView里面有几百条记录,也都是由这 16个TextView替换内容来完成显示。但是每次要替换内容都需要查找4个View,为了使系统的效率最大化,我们可以对它进行优化,我们创建一个静 态类,保存对每个View中的4个TextView的跟踪,这样就不必每次都查找。具体实现如下(这边修改ListView的使用和特性研究(一) )中的例子:
package com.zhouzijing.android;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class PersonAdapter extends ArrayAdapter<Person>{
LayoutInflater mLayoutInflater;
int resourceId;
Context mContext;
public PersonAdapter(Context context, int resourceId, Person[] objects) {
super(context, resourceId, objects);
//获取LayoutInflater 服务,用来从预定义的xml布局创建view对象.
this.resourceId = resourceId;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
//创建新的view视图.
convertView = mLayoutInflater.inflate(resourceId, null);
}
ViewHolder holder = (ViewHolder)convertView.getTag();
if(holder==null){
holder = new ViewHolder();
//查找每个ViewItem中,各个子View,放进holder中
holder.name = (TextView) convertView.findViewById(R.id.person_name);
holder.age = (TextView) convertView.findViewById(R.id.person_age);
holder.email = (TextView) convertView.findViewById(R.id.person_email);
holder.address = (TextView) convertView.findViewById(R.id.person_address);
//保存对每个显示的ViewItem中, 各个子View的引用对象
convertView.setTag(holder);
}
//获取当前要显示的数据
Person person = getItem(position);
holder.name.setText(person.name);
holder.age.setText(String.valueOf(person.age));
holder.email.setText(person.email);
holder.address.setText(person.address);
return convertView;
}
static class ViewHolder {
TextView name;
TextView age;
TextView email;
TextView address;
}
}
上面代码是优化后的结果。在上面代码中我们进行了优化,使ListView没有每次要替换内容都查找内部的View,而是保存引用,总共一屏能显示N条,就查找N次 ,而不是之前的总显示N条,就查找N次。
下面我们看看如何使用android内置对ListView的支持来更好的辅助我们编写一个ListView。
这边我们通过继承ListActivity来编写一个ListView。所以首先我们先了解下ListActivity这个类。
上图可以看到ListActivity继承了Activity,然后扩展了一些针对ListView操作的方法,如设置选中第几个,点击事件,设置 adapter等。实际上ListActivity就是一个内置了一个ListView的Activity,我们在使用它时,可以不用调用 setContentView方法就能显示出ListView,那到底这个ListView放在哪里呢?
使用ListActivity,android会帮忙创建一个id是android.R.id.list的ListView,所有的对 ListActivity内置的ListView的操作也都是作用在这个id上,同时系统本身还会判断,如果这个ListView里的数据是空的,它会对 应的显示一个id是android.R.id.empty的控件,用来指示用户这个ListView的数据为空,所有这些逻辑流程转换都是由系统完成,用 户只需要关心显示的内容即可,所以大大方便了用户的开发。下面我们创建一个xml布局,简单介绍下如何使用内置的ListView。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:drawSelectorOnTop="false"
android:id="@android:id/list" android:layout_height="0dip"
android:layout_weight="1" android:layout_width="fill_parent"/>
<TextView android:id="@android:id/empty"
android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="没有数据"/>
</LinearLayout>
上面代码创建了布局文件,其中id是:@android:id/list的控件就是负责显示数据的ListView,@android:id/empty 则是当ListView为空时负责显示提示信息的控件。当然这边你可以添加任意你需要显示的东西,如你可能需要一个工具条,几个按钮之类的,尽管添加,ListView只会影响到对应它所在的区域而不会影响其他的控件。
public class MainActivity extends ListActivity {
final String[] data = new String[]{
"第一章","第二章","第三章","第四章","第五章"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_demo);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data);
setListAdapter(adapter);
}
}
显示的效果如下:
如果修改java代码(final String[] data = new String[]{};),让其显示空数据时,运行的效果如下:
我们可以通过重写ListActivity里的onListItemClick方法,来便捷的为ListView添加item click事件。
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(this, "您点击的是:" + data[position], Toast.LENGTH_LONG)
.show();
}
当用户点击ListView里面的Item时,效果如下:
综上,我们讨论了如何编写ListView中的自定义视图并优化其效率,如何使用ListActivity。在这边大家要熟悉android提供的一些预 设的id和layout,如@android:id/list ,android.R.layout.simple_list_item_1等。因为这些布局很常用,利用它们可以减少很多编码工作,而且后面要学习的 tabhost也是内置了一些预设置的layout或id来减少代码编写工作,如果不熟悉这些对未来扩展自定义UI将造成一定的难度。
我是代码派
public class ZipUtils {
/**
* 解压文件
* @param zipPath
* @param toPath
* @throws IOException
*/
public static void unCompress(String zipPath, String toPath) throws IOException {
if(!toPath.endsWith("\\"))toPath += "\\";
File destFile = new File(toPath);
if(!destFile.exists())destFile.mkdirs();
File zipfile = new File(zipPath);
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipfile));
ZipEntry entry = null;
while ((entry = zis.getNextEntry()) != null) {
if (entry.isDirectory()) {
File file = new File(toPath + entry.getName() + "\\");
file.mkdirs();
} else {
File file = new File(toPath + entry.getName());
if(!file.getParentFile().exists())file.getParentFile().mkdirs();
FileOutputStream fos = new FileOutputStream(file);
byte buf[] = new byte[1024];
int len = -1;
while ((len = zis.read(buf, 0, 1024)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
}
}
zis.close();
}
public static byte[] getData(String zipPath, String path) throws IOException{
File zipfile = new File(zipPath);
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipfile));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipEntry entry = null;
while ((entry = zis.getNextEntry()) != null) {
if(entry.getName().equals(path)){
byte buf[] = new byte[1024];
int len = -1;
while ((len = zis.read(buf, 0, 1024)) != -1) {
baos.write(buf, 0, len);
}
break;
}
}
baos.close();
zis.close();
return baos.toByteArray();
}
/**
* 压缩单个文件
* @param file
*/
public static void compress(File file) {
try {
String fileName = file.getName();
if(fileName.indexOf(".") != -1)
fileName = fileName.substring(0, fileName.indexOf("."));
FileOutputStream f = new FileOutputStream(file.getParent() + "/" + fileName+ ".zip");
CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
InputStream in = new FileInputStream(file);
out.putNextEntry(new ZipEntry(file.getName()));
int len = -1;
byte buf[] = new byte[1024];
while((len = in.read(buf, 0, 1024)) != -1)
out.write(buf, 0, len);
out.closeEntry();
in.close();
out.close();
} catch (Exception e) {
System.err.println(e);
}
}
/**
* 压缩文件夹
* @param file
* @throws IOException
*/
public static void compressDir(File file) throws IOException{
FileOutputStream f = new FileOutputStream(file.getParent() + file.getName() + ".zip");
CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
compressDir(file, out, file.getAbsolutePath());
out.flush();
out.close();
}
/**
* 压缩文件夹递归调用方法
* @param file
* @param out
* @throws IOException
*/
private static void compressDir(File srcFile, ZipOutputStream out, String destPath) throws IOException{
if(srcFile.isDirectory()){
File subfile[] = srcFile.listFiles();
for(int i=0; i< subfile.length; i++){
compressDir(subfile[i], out, destPath);
}
}else{
InputStream in = new FileInputStream(srcFile);
String name = srcFile.getAbsolutePath().replace(destPath, "");
if(name.startsWith("\\"))name = name.substring(1);
ZipEntry entry = new ZipEntry(name);
entry.setSize(srcFile.length());
entry.setTime(srcFile.lastModified());
out.putNextEntry(entry);
int len = -1;
byte buf[] = new byte[1024];
while((len = in.read(buf, 0, 1024)) != -1)
out.write(buf, 0, len);
out.closeEntry();
in.close();
}
}
public static void main(String[] args) throws Exception {
ZipUtils.compress(new File("d:/aa/a01.jpg"));
// try {
// byte buf[] = ZipDemo.getData("d:/aa.zip", "aa.txt");
// System.out.println(buf.length);
// ZipDemo.unCompress("d:/aa.zip", "d:/kk/");
// } catch (IOException e) {
// e.printStackTrace();
// }
}
default-autowire="x"
x有4个选择:byName,byType,constructor和autodetect
我感觉byName和byType用的多点
1. byName:
Service.java
public class Service
{
Source source;
public void setSource(Source source)
{
this.source = source;
}
}
applicationContext.xml
<beans
...
default-autowire="byName">
<bean id="source" scope="prototype"/>
<bean id="service" scope="prototype">
</bean>
</beans>
cn.hh.spring.DBCPSource实现了Source接口
xml中并没有给 bean service配Source属性,但在beans中设置了autowire="byName",这样配置文件会自动根据 cn.hh.spring.Service 中的setSource找bean id="Source"的bean ,然后自动配上去,如果没找到就不装配。
注意:byName的name是java中setXxxx 的Xxxx, 和上面设置的Source source中source拼写毫无关系,完全可以是
public class Service
{
Source source1;
public void setSource(Source source1)
{
this.source1 = source1;
}
}
结果相同。
2. byType:
Service.java同上
applicationContext.xml
<beans
...
default-autowire="byType">
<bean id="dbcpSource" scope="prototype"/>
<bean id="service" scope="prototype">
</bean>
</beans>
同样没有配置setSource,autowire改成 "byType",配置文件会找实现了Source接口的bean,这里 cn.hh.spring.DBCPSource 实现了Source接口,所以自动装配,如果没找到则不装配。
如果同个配制文件中两个bean实现了Source接口,则报错。
这里的 Type是指setSource(Source source)中参数的类型。
3. constructor:
试图在容器中寻找与需要自动装配的bean的构造函数参数一致的一个或多个bean,如果没找到则抛出异常。
4. autodetect:
首先尝试使用constructor来自动装配,然后再使用byType方式