具体代码
int flags = packInfo.applicationInfo.flags; if ((flags & ApplicationInfo.FLAG_SYSTEM) != 0) { //系统应用 } else { //用户应用 } if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { //安装在sd卡 } else { //安装在手机内存 }当我们在Adapter中调用方法getView的时候,如果整个列表中的Item View如果有多种类型布局,如:
我们继续使用convertView来将数据从新填充貌似不可行了,因为每次返回的convertView类型都不一样,无法重用。
Android在设计上的时候,也想到了这点。所以,在adapter中预留的两个方法。
public int getViewTypeCount(); //有多少种布局类型 public int getItemViewType(int position); //获取某个位置是哪种类型类型只需要重写这两个方法,设置一下ItemViewType的个数和判断方法,Recycler就能有选择性的给出不同的convertView了。
private static final int TYPE_LABEL = 0; private static final int TYPE_CONTENT = 1; private class AppManagerAdapter extends BaseAdapter{ @Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { if(position == 0 || position == userAappInfos.size()+1){ return TYPE_LABEL; }else{ return TYPE_CONTENT; } } /** * 返回listview里面有多少个条目 */ @Override public int getCount() { //为什么要加两个1 , 增加了两个textview的标签。整个listview条目的个数增加了。 return 1+userAappInfos.size()+1+systemAppInfos.size(); } /** * 显示每个条目的view对象 */ @Override public View getView(int position, View convertView, ViewGroup parent) { LabelViewHolder labelViewHolder = null; ContentViewHolder contentViewHolder = null; int type = getItemViewType(position); if(convertView == null){ switch (type) { case TYPE_LABEL: convertView = new TextView(AppManagerActivity.this); labelViewHolder = new LabelViewHolder(); labelViewHolder.tv_label = (TextView) convertView; labelViewHolder.tv_label.setBackgroundColor(Color.GRAY); convertView.setTag(labelViewHolder); break; case TYPE_CONTENT: convertView = View.inflate(getApplicationContext(), R.layout.item_appinfo, null); contentViewHolder = new ContentViewHolder(); contentViewHolder.iv_appIcon = (ImageView) convertView.findViewById(R.id.iv_appicon); contentViewHolder.tv_appName = (TextView) convertView.findViewById(R.id.tv_appName); contentViewHolder.tv_apkSize = (TextView) convertView.findViewById(R.id.tv_apkSize); contentViewHolder.iv_install_location = (ImageView) convertView.findViewById(R.id.iv_install_location); convertView.setTag(contentViewHolder); break; } }else{ switch (type) { case TYPE_LABEL: labelViewHolder = (LabelViewHolder) convertView.getTag(); break; case TYPE_CONTENT: contentViewHolder = (ContentViewHolder) convertView.getTag(); break; } } switch (type) { case TYPE_LABEL: if(position == 0){ labelViewHolder.tv_label.setText("用户程序:"+userAappInfos.size()); }else{ labelViewHolder.tv_label.setText("系统程序:"+systemAppInfos.size()); } break; case TYPE_CONTENT: AppInfo appInfo; if(position<=userAappInfos.size()){//用户程序 int newPosition = position - 1;//减去用户的标签textview占据的位置 appInfo = userAappInfos.get(newPosition); }else {//系统程序 int newPosition = position - 1 - userAappInfos.size() - 1; appInfo = systemAppInfos.get(newPosition); } contentViewHolder.iv_appIcon.setImageDrawable(appInfo.getAppIcon()); contentViewHolder.tv_appName.setText(appInfo.getAppName()); contentViewHolder.tv_apkSize.setText("程序大小:"+Formatter.formatFileSize(getApplicationContext(), appInfo.getAppSize())); if(appInfo.isInRom()){ contentViewHolder.iv_install_location.setImageResource(R.drawable.memory); }else{ contentViewHolder.iv_install_location.setImageResource(R.drawable.sd); } break; } return convertView; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } } /** * 存放内容孩子对象的引用 */ static class ContentViewHolder{ ImageView iv_appIcon; TextView tv_appName; TextView tv_apkSize; ImageView iv_install_location; } /** * 存放标签孩子对象的引用 */ static class LabelViewHolder{ TextView tv_label; }