继续对实现带图标的ListView(改进版)中的ListView做增强。
修改list_item.xml文件,添加复选框控件:
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
| <?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>
<CheckBox
android:id="@+id/app_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:checkMark="?android:attr/listChoiceIndicatorMultiple" />
</LinearLayout>
|
这三行代码很重要,如果不加会出现一些奇怪的错误。
1
2
3
| android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
|
加入复选框后,在程序运行过程中会发现整个ListView无法响应onItemClick、onItemLongClick或onCreateContextMenu事件。原因在于复选框是拥有焦点的,它的优先级比ListItem的焦点优先级更高,于是ListItem的点击事件被屏蔽了。解决方法是让复选框不能获得焦点。android:focusable="false"就是做这个的。
修改整个SimpleIconAdapter类为以下内容:
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
| public class SimpleIconAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<? extends Map<String, ?>> mData;
public SimpleIconAdapter(Context context, List<? extends Map<String, ?>> data) {
mInflater = LayoutInflater.from(context);
mData = data;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView != null) {
view = convertView;
} else {
view = mInflater.inflate(R.layout.list_item, parent, false);
}
Map map = (Map)getItem(position);
TextView tv = (TextView)view.findViewById(R.id.app_title);
tv.setText((String)map.get("app_title"));
tv = (TextView)view.findViewById(R.id.app_package);
tv.setText((String)map.get("app_package"));
ImageView iv = (ImageView)view.findViewById(R.id.app_icon);
iv.setImageDrawable((Drawable)map.get("app_icon"));
CheckBox cb = (CheckBox)view.findViewById(R.id.app_select);
if ((Boolean)map.get("app_select") == null) {
map.put("app_select", false);
}
cb.setChecked((Boolean)map.get("app_select"));
return view;
}
}
|
再就是修改MainActivity类的onCreate()方法:
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
| @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView = (ListView)findViewById(R.id.list_view);
mListData = new ArrayList<HashMap<String, Object>>();
mAdapter = new SimpleIconAdapter(this, mListData);
mListView.setAdapter(mAdapter);
mListView.setItemsCanFocus(false);
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mListView.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 这里如此处理是因为当选中复选框,滚动ListView的时候,会出现一些
// 复选框选择错位的现象,所以在选择复选框时,记下其状态,然后在
// getView方法中进行设置。
//
// 原因:
// ListView中的getChildCount()并不总是等于Adapter中的数据行数。当手
// 机一屏显示不了所有数据时(需要翻页),getChildCount()就等于一屏
// 所显示的行数(一般为10),小于Adapter中的数据行数。而ListView的
// getCount()与Adapter中的数据行数相等。
// 当光标下移到屏幕最底部,新显示出来的View,在调用Adapter的getView
// 方法中,会判断convertView为null,而再有新的View显示就会发现
// convertView不为空,所以新显示的View其实使用了之前某个View的对象。
// 这就造成了状态可能混乱。比如第一行的复选框点选时,第11行的也同
// 时会被点选。
CheckBox cb = (CheckBox)view.findViewById(R.id.app_select);
cb.toggle();
((Map)mListData.get(position)).put("app_select", cb.isChecked());
}
});
mProgressDialog = ProgressDialog.show(this, "Wait", "loading...");
new Thread() {
@Override
public void run() {
mListData.addAll(getInstalledApps(false));
mHandler.sendEmptyMessage(0);
}
}.start();
}
|