复杂的ListView

需求:实现通过金山卫士的软件管理的复杂ListView,实现:ListView分别有系统应用和用户应用

界面如下:

复杂的ListView_第1张图片                        复杂的ListView_第2张图片                           复杂的ListView_第3张图片

分析代码:

AppManagerActivity.java

public class AppManagerActivity extends Activity {
	private TextView tv_avail_rom;
	private TextView tv_avail_sd;
	private ListView lv_app_manager;
	private LinearLayout ll_loading;
	/*
	 * 所有的应用程序包信息
	 */
	private List<AppInfo> appInfos;
	/*
	 * 用户应用程序的集合
	 */
	private List<AppInfo> userAppInfos;
	
	/*
	 * 系统应用程序的集合
	 */
	private List<AppInfo> systemAppInfos;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.a)
		setContentView(R.layout.activity_app_manager);
		tv_avail_rom=(TextView) findViewById(R.id.tv_avail_rom);
		tv_avail_sd=(TextView) findViewById(R.id.tv_avail_sd);
		<strong>//获得内存可用空间和SD卡可用空间
		long sdsize=getAvailSpace(Environment.getExternalStorageDirectory().getAbsolutePath());
		long romsize=getAvailSpace(Environment.getDataDirectory().getAbsolutePath());
		
		tv_avail_sd.setText("SD卡可用空间:"+Formatter.formatFileSize(this, sdsize));
		tv_avail_rom.setText("内存可用空间:"+Formatter.formatFileSize(this, romsize));
		//布局
		lv_app_manager=(ListView) findViewById(R.id.lv_app_manager);
		ll_loading=(LinearLayout) findViewById(R.id.ll_loading);</strong>
		
		ll_loading.setVisibility(View.VISIBLE);
		new Thread(){
			public void run(){
				//得到所有数据
				appInfos=AppInfoProvider.getAppInfos(AppManagerActivity.this);
				systemAppInfos=new ArrayList<AppInfo>();
				userAppInfos=new ArrayList<AppInfo>();
				<strong>for(AppInfo info:appInfos){
					if(info.isUserApp()){
						userAppInfos.add(info);
					}else{
						systemAppInfos.add(info);
					}
				}
				//加载listview的数据适配器【通过runOnUiThread来实现在线程里面更新主界面的UI】
				runOnUiThread(new Runnable(){
					@Override
					public void run() {
						lv_app_manager.setAdapter(new AppManagerAdapter());
						ll_loading.setVisibility(View.INVISIBLE);
					}
				});</strong>
			}
		}.start();
	}
	
	private class AppManagerAdapter extends BaseAdapter{
		@Override<strong>//getCount()就是多少个项
		public int getCount() {
			return userAppInfos.size()+1+systemAppInfos.size()+1;
		} </strong>
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			AppInfo appinfo;//appInfos.get(position);
			<strong>if(position==0){/</strong>/显示的是用户程序有多少个小标签
            	TextView tv=new TextView(getApplicationContext());
            	tv.setTextColor(Color.WHITE);
            	tv.setBackgroundColor(Color.GRAY);
            	tv.setText("用户程序: "+userAppInfos.size()+"个");
            	return tv;
            }<strong>else if(position==(userAppInfos.size()+1)){//显示的是系统程序有多少个小标签</strong>
            	TextView tv=new TextView(getApplicationContext());
            	tv.setTextColor(Color.WHITE);
            	tv.setBackgroundColor(Color.GRAY);
            	tv.setText("系统程序: "+systemAppInfos.size()+"个");
            	return tv;
            }<strong>else if(position<=userAppInfos.size()){//用户程序</strong>
            	int newposition=position-1;
            	appinfo=userAppInfos.get(newposition);
            }<strong>else{//系统程序</strong>
            	int newposition=position-1-userAppInfos.size()-1;
            	appinfo=systemAppInfos.get(newposition);
            }
           
			View view;
			viewHolder holder;
			<strong>//不仅需要检查不为空,还要看是合适的类型来复用【主要是多了两个TextView】
			if(convertView!=null&&convertView instanceof RelativeLayout){</strong>
				view=convertView;
				holder=(viewHolder) view.getTag();
			}else{
				<strong>//将view.inflate方法对View进行了填充
				view=View.inflate(getApplicationContext(), R.layout.list_item_appinfo, null);</strong>
				holder=new viewHolder();
				holder.iv_icon=(ImageView) view.findViewById(R.id.iv_app_icon);
				holder.tv_name=(TextView) view.findViewById(R.id.tv_app_name);
				holder.tv_location=(TextView) view.findViewById(R.id.tv_app_location);
				view.setTag(holder);
			}
			
			holder.iv_icon.setImageDrawable(appinfo.getIcon());
			holder.tv_name.setText(appinfo.getName());
			if(appinfo.isInRom()){
				holder.tv_location.setText("手机内存");
			}else{
				holder.tv_location.setText("外部内存储");
			}
			return view;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}
		@Override
		public long getItemId(int position) {
			return 0;
		}
	}
	static class viewHolder{
		TextView tv_name;
		TextView tv_location;
		ImageView iv_icon;
	}

	<strong>private long getAvailSpace(String path){
		StatFs statf=new StatFs(path);
		statf.getBlockCount();//获取分区的个数
		long size=statf.getBlockSize();  //获取分区的大小
		long count=statf.getAvailableBlocks();   //获取可用的区块个数
		return size*count;
	}</strong>
}


AppInfoProvider.java:得到数据类

public class AppInfoProvider {
	/*
<strong>	 * 业务方法,提供手机里面安装的所有应用程序信息</strong>
	 * @param context 上下文
	 * @return 
	 */
	public static List<AppInfo> getAppInfos(Context context){
		<strong>PackageManager pm=context.getPackageManager();
		//所有的安装在系统上的应用程序包信息
		List<PackageInfo> packInfos=pm.getInstalledPackages(0);
		List<AppInfo> appInfos=new ArrayList<AppInfo>();
		for(PackageInfo packInfo:packInfos){
		AppInfo appInfo=new AppInfo();
		//packInfo  相当于一个应用程序apk包的清单文件
		String packname=packInfo.packageName;
		Drawable icon=packInfo.applicationInfo.loadIcon(pm);
		String name=packInfo.applicationInfo.loadLabel(pm).toString();
		int flags=packInfo.applicationInfo.flags; //应用程序的答题卡
		if((flags&ApplicationInfo.FLAG_SYSTEM)==0){
			//用户程序
			appInfo.setUserApp(true);
		}else{
			//系统程序
			appInfo.setUserApp(false);
		}
		if((flags&ApplicationInfo.FLAG_EXTERNAL_STORAGE)==0){
			//手机的内存
			appInfo.setInRom(true);
		}else{
			//手机的外存储设备
			appInfo.setInRom(false);
		}
		appInfo.setPackname(packname);
		appInfo.setIcon(icon);
		appInfo.setName(name);
		appInfos.add(appInfo);
		}
		return appInfos;</strong>
	}
}


AppInfo.java

<strong>/*
 * 应用程序信息的业务Bean
 */</strong>
public class AppInfo {
	private Drawable icon;
	private String name;
	private String packname;
	private boolean inRom;
	private boolean userApp;
	public Drawable getIcon() {
		return icon;
	}
	public void setIcon(Drawable icon) {
		this.icon = icon;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPackname() {
		return packname;
	}
	public void setPackname(String packname) {
		this.packname = packname;
	}
	public boolean isInRom() {
		return inRom;
	}
	public void setInRom(boolean inRom) {
		this.inRom = inRom;
	}
	public boolean isUserApp() {
		return userApp;
	}
	public void setUserApp(boolean userApp) {
		this.userApp = userApp;
	}
}



Layout布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
        android:background="#8866ff00"
        android:textSize="22sp"
        android:textColor="#000000"
        android:gravity="center"
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:height="55dip"
        android:text="软件管理"
        />
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         >
        <TextView 
        android:id="@+id/tv_avail_rom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="内存可用"  
            />
        <TextView 
        android:id="@+id/tv_avail_sd"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="SD卡可用"  
            />
        <FrameLayout 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >
        <LinearLayout 
            android:id="@+id/ll_loading"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:gravity="center"
            android:visibility="invisible"
            >
            <ProgressBar 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="正在加载程序信息。。。"
                />
        </LinearLayout>
        <ListView 
            android:id="@+id/lv_app_manager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            ></ListView>
        </FrameLayout>
    </RelativeLayout>
</LinearLayout>



listview:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dip">
    
    <ImageView 
        android:id="@+id/iv_app_icon"
        android:src="@drawable/ic_launcher"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:layout_marginLeft="5dip"
        android:layout_marginBottom="5dip"
        android:layout_marginTop="5dip"
        />
    
    <TextView 
        android:layout_marginTop="8dip"
        android:layout_toRightOf="@id/iv_app_icon"
        android:id="@+id/tv_app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="20sp"
        android:text="程序名称"
        />
    <TextView 
        android:layout_marginTop="3dip"
        android:layout_toRightOf="@id/iv_app_icon"
        android:layout_below="@id/tv_app_name"
        android:id="@+id/tv_app_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#88000000"
        android:textSize="16sp"
        android:text="安装位置"
        />

</RelativeLayout>

你可能感兴趣的:(复杂的listvies显示)