复杂的ListView显示用户程序和系统程序数据信息
核心代码:
/**
* 4、展示应用程序信息的Adapter
*
* @author hacket
*/
private class AppManageAdapter extends BaseAdapter {
@Override
public int getCount() {
int usersize = userAppInfos.size() + 1;
int syssize = sysAppInfos.size() + 1;
return usersize + syssize;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
AppInfo appInfo = null;
if (position == 0) {// 显示用户程序TextView
TextView tv = new TextView(getApplicationContext());
tv.setBackgroundColor(Color.GRAY);
tv.setTextColor(Color.WHITE);
tv.setText("用户程序:" + userAppInfos.size() + "个");
return tv;// 记得return,否则后面appInfo会报空指针访问异常
} else if (position == userAppInfos.size() + 1) {// 显示系统程序TextView
TextView tv = new TextView(getApplicationContext());
tv.setBackgroundColor(Color.GRAY);
tv.setTextColor(Color.WHITE);
tv.setText("系统程序:" + sysAppInfos.size() + "个");
return tv;// 记得return,否则后面appInfo会报空指针访问异常
} else if (position <= userAppInfos.size()) {// 显示用户程序数据
int newUserPosition = position-1;
appInfo = userAppInfos.get(newUserPosition);
} else {// 显示系统程序数据
int newSysPosition = position - 1 - userAppInfos.size() - 1;
appInfo = sysAppInfos.get(newSysPosition);
}
View view = null;
ViewHolder viewHolder;// 提升性能的,将查找到的组件存起来
if (convertView != null && view instanceof RelativeLayout) {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}else {
view = View.inflate(getApplicationContext(), R.layout.listview_appinfo_item, null);
viewHolder = new ViewHolder();
viewHolder.iv_app_icon = (ImageView) view.findViewById(R.id.iv_app_icon);
viewHolder.tv_app_name = (FocusedTextView) view.findViewById(R.id.tv_app_name);
viewHolder.tv_app_install_location = (TextView) view.findViewById(R.id.tv_app_install_location);
view.setTag(viewHolder);
}
viewHolder.iv_app_icon.setImageDrawable(appInfo.getAppIcon());
viewHolder.tv_app_name.setText(appInfo.getAppName());
boolean isInRom = appInfo.getIsInRom();
if (isInRom) {// 在手机内存
viewHolder.tv_app_install_location.setText("内部存储");
} else {// 在外部存储卡
viewHolder.tv_app_install_location.setText("外部存储");
}
return view;
}
}
private class ViewHolder {
ImageView iv_app_icon;// 应用程序图标
FocusedTextView tv_app_name;// 应用程序名
TextView tv_app_install_location;// 应用程序安装位置
}
|
结果: |