
listview的优化。
1.复用历史缓存的view对象 converview
2.减少子孩子id查询的次数
3.分批的加载数据
4.分页的加载数据
优化的原则: 拆东墙补西墙。
1.时间换时间(listview的分批加载数据)
2.时间换空间 文件拷贝
3.空间换时间 文件快速查找的索引。
4.空间换空间 虚拟内存 RAMdisk
listview 有一个刷新控件,和开启子线程 更新到界面的代码
private void fillData() {
ll_loading.setVisibility(View.VISIBLE);
new Thread() {
public void run() {
appInfos = AppInfoProvider.getAppInfo(AppManagerActivity.this);
userAppInfos = new ArrayList<AppInfo>();
systemAppInfos = new ArrayList<AppInfo>();
for (AppInfo info : appInfos) {
if (info.isUserApp()) {
userAppInfos.add(info);
} else {
systemAppInfos.add(info);
}
}
// 加载listview的数据适配器
runOnUiThread(new Runnable() {
@Override
public void run() {
if (adapter == null) {
adapter = new AppManagerAdapter();
lv_app_manager.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
ll_loading.setVisibility(View.INVISIBLE);
}
});
};
}.start();
}
自定义适配器
private class AppManagerAdapter extends BaseAdapter {
// 控制listview有多少个条目
@Override
public int getCount() {
// return appInfos.size();
return userAppInfos.size() + 1 + systemAppInfos.size() + 1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
AppInfo appInfo;
if (position == 0) {// 显示的是用程序有多少个的小标签
TextView tv = new TextView(getApplicationContext());
tv.setTextColor(Color.WHITE);
tv.setBackgroundColor(Color.GRAY);
tv.setText("用户程序:" + userAppInfos.size() + "个");
return tv;
} else if (position == (userAppInfos.size() + 1)) {
TextView tv = new TextView(getApplicationContext());
tv.setTextColor(Color.WHITE);
tv.setBackgroundColor(Color.GRAY);
tv.setText("系统程序:" + systemAppInfos.size() + "个");
return tv;
} else if (position <= userAppInfos.size()) {// 用户程序
int newposition = position - 1;// 因为多了一个textview的文本占用了位置
appInfo = userAppInfos.get(newposition);
} else {// 系统程序
int newposition = position - 1 - userAppInfos.size() - 1;
appInfo = systemAppInfos.get(newposition);
}
View view;
ViewHolder holder;
// if(position<userAppInfos.size()){//这些位置是留个用户程序显示的。
// appInfo = userAppInfos.get(position);
// }else{//这些位置是留个系统程序的。
// int newposition = position - userAppInfos.size();
// appInfo = systemAppInfos.get(newposition);
// }
if (convertView != null && convertView instanceof RelativeLayout) {
// 不仅需要检查是否为空,还要判断是否是合适的类型去复用
view = convertView;
holder = (ViewHolder) view.getTag();
} else {
view = View.inflate(getApplicationContext(),
R.layout.list_item_appinfo, null);
holder = new ViewHolder();
holder.iv_icon = (ImageView) view
.findViewById(R.id.iv_app_icon);
holder.tv_location = (TextView) view
.findViewById(R.id.tv_app_location);
holder.tv_name = (TextView) view.findViewById(R.id.tv_app_name);
holder.iv_status=(ImageView) view.findViewById(R.id.iv_app_status);
view.setTag(holder);
}
holder.iv_icon.setImageDrawable(appInfo.getIcon());
holder.tv_name.setText(appInfo.getName());
if (appInfo.isInRom()) {
holder.tv_location.setText("手机内存"+appInfo.getUid());
} else {
holder.tv_location.setText("外部存储"+appInfo.getUid());
}
if(dao.find(appInfo.getPackname())){
holder.iv_status.setImageResource(R.drawable.lock);
}else{
holder.iv_status.setImageResource(R.drawable.unlock);
}
return view;
}
用的中间类
static class ViewHolder {
TextView tv_name;
TextView tv_location;
ImageView iv_icon;
ImageView iv_status;
}