listview的优化。
1.复用历史缓存的view对象 converview
2.减少子孩子id查询的次数
3.分批的加载数据
4.分页的加载数据
优化的原则: 拆东墙补西墙。
1.时间换时间(listview的分批加载数据)
2.时间换空间 文件拷贝
3.空间换时间 文件快速查找的索引。
4.空间换空间 虚拟内存 RAMdisk
listview 有一个刷新控件,和开启子线程 更新到界面的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
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(); } |
自定义适配器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
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; } |
用的中间类
1 2 3 4 5 6 7 8 9 10 11 |
static class ViewHolder { TextView tv_name; TextView tv_location; ImageView iv_icon; ImageView iv_status; } |
未经允许不得转载:Java学习 » listview的封装,复用,及自定义适配器