测试 ListView 的效率 2 [原创]
上面经过测试, 发现每次都要构造, 太浪费资源了. 所以进行如下改造.
java 代码如下:
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_2 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;
/**
* 测试效率(改进)
*/
if (convertView == null) {
view = mInflater.inflate(mResource, null);
System.out.println("-->" + position + " :: ");
} else {
view = convertView;
}
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;
}
}
}
布局文件没有变, 还是前面那个. 现在再看看打印出来的信息:
然后, 我们再任意上下拖动 ListView, 看打印信息:
此时, 可以发现, 只有这10个 item, 并没有每次都重新构造 item 了, 效率大为提高了.
因为核心代码:
if (convertView == null) {
view = mInflater.inflate(mResource, null);
System.out.println("-->" + position + " :: ");
} else {
view = convertView;
}
导致 item 可以复用了.