测试 ListView 的效率 1 [原创]
测试 ListView 的效率
为了亲自测试 ListView 的使用效率, 做如下测试:
package cn.powerise.test01;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* 测试 ListView 的效率
* @author R9(QQ:34351088)
*/
public class List_Efficiency_1 extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<HashMap<String, Object>> data = null;
MyAdapter adapter = null;
data = getData();
adapter = new MyAdapter(this, data, R.layout.efficiency_item);
setListAdapter(adapter);
}
private List<HashMap<String, Object>> getData() {
List<HashMap<String, Object>> data = null;
HashMap<String, Object> item = null;
data = new ArrayList<HashMap<String, Object>>();
for (int i = 10; i < 40; i++) {
item = new HashMap<String, Object>();
item.put("image", R.drawable.icon);
item.put("id", i);
item.put("name", "Name" + i);
item.put("age", "Age" + i);
item.put("phone", "139073100" + i);
data.add(item);
}
return data;
}
class MyAdapter extends BaseAdapter {
private List<HashMap<String, Object>> mData;
private LayoutInflater mInflater;
private int mResource;
public MyAdapter(Context context, List<HashMap<String, Object>> data, int resource) {
mData = data;
mResource = resource;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
view = mInflater.inflate(mResource, null);
/**
* 测试效率(普通情况)
*/
System.out.println("-->" + position + " :: ");
ImageView imageView = (ImageView) view.findViewById(R.id.iv_image);
TextView idTextView = (TextView) view.findViewById(R.id.txt_id);
TextView nameTextView = (TextView) view.findViewById(R.id.txt_name);
TextView ageTextView = (TextView) view.findViewById(R.id.txt_age);
TextView phoneTextView = (TextView) view.findViewById(R.id.txt_phone);
HashMap<String, Object> dataSet = mData.get(position);
imageView.setImageResource(R.drawable.icon);
idTextView.setText(dataSet.get("id").toString());
nameTextView.setText(dataSet.get("name").toString());
ageTextView.setText(dataSet.get("age").toString());
phoneTextView.setText(dataSet.get("phone").toString());
return view;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
}
item 的布局文件: efficiency_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:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/txt_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/txt_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/txt_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
如下图所示:
在控制台打印输出:
上面是初始状态情况, 下面, 不停的往下, 或往上拖拉 ListView. 控制台打印出如下所示:
也就是说, 每一个 item 进入可视区域后, 都会调用 getView 方法, 并且会重新构造一个 view. 这样的写法最直接, 直观, 但是效率最低, 因为每次都要重新构造一次.