乐者为王

Do one thing, and do it well.

实现带图标的ListView

listview-screenshot

main.xml不需要修改,还是用不使用ListActivity实现ListView中的那个。

在list_item.xml中添加ImageView控件,设置高度和宽度分别为32dip,这是因为有些应用图标尺寸比较大的缘故,如果不设置的话有些行就会撑大:

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/app_icon"
        android:layout_gravity="center_vertical"
        android:layout_width="32.0dip"
        android:layout_height="32.0dip"
        android:layout_marginLeft="3.0dip"
        android:layout_marginRight="3.0dip" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0">
        <TextView
            android:id="@+id/app_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="16.0dip"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/app_package"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="13.0dip" />
    </LinearLayout>
</LinearLayout>

新建SimpleIconAdapter类,继承SimpleAdapter适配器:

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
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

public class SimpleIconAdapter extends SimpleAdapter {

    public SimpleIconAdapter(Context context, List<? extends Map<String, ?>> data,
            int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        ImageView iv = (ImageView)view.findViewById(R.id.app_icon);
        iv.setImageDrawable((Drawable)((Map)getItem(position)).get("app_icon"));
        return view;
    }
}

修改MainActivity文件,更换绑定的适配器:

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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
    private static final String APP_ICON = "app_icon";
    private static final String APP_TITLE = "app_title";
    private static final String APP_PACKAGE = "app_package";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView listView = (ListView)findViewById(R.id.list_view);
        SimpleAdapter adapter = new SimpleIconAdapter(this,
                getInstalledApps(false),
                R.layout.list_item,
                new String[] {APP_ICON, APP_TITLE, APP_PACKAGE},
                new int[] {R.id.app_icon, R.id.app_title, R.id.app_package});
        listView.setAdapter(adapter);
    }

    private List<HashMap<String, Object>> getInstalledApps(boolean getSysPackages) {
        List<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();

        List<PackageInfo> pkgs = getPackageManager().getInstalledPackages(0);
        for (int i = 0; i< pkgs.size(); i++) {
            PackageInfo pkg = pkgs.get(i);
            if (!getSysPackages
                    && (pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
                continue;
            }
            Drawable icon = pkg.applicationInfo.loadIcon(getPackageManager());
            String label = pkg.applicationInfo.loadLabel(getPackageManager()).toString();
            String version = pkg.versionName;
            String pkgName = pkg.packageName;

            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put(APP_ICON, icon);
            map.put(APP_TITLE, label + " " + version);
            map.put(APP_PACKAGE, pkgName);
            listItem.add(map);
        }
        return listItem;
    }
}

Comments