Android ListView 默认选中某一项

要使用 ListView 实现一个充值方式选择,默认想选中第二项,搞了一下午,终于搞定了。原本就没怎么用 Java 写过 Android 应用,又隔了好久没写,一切都生疏了,半吊子变成大呆瓜了……

ListView定义

说起来也很简单,下面是 Layout 文件中的 ListView 定义:

1
2
3
4
5
6
7
8
9
10
11
12
<ListView
android:id="@+id/recharge_method_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:dividerHeight="2dp"
android:divider="@color/ssq_bkgnd"
android:background="@android:color/white"
android:choiceMode="singleChoice"
android:listSelector="@null"
>
</ListView>

item 模板定义

item模板如下定义:

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:background="@drawable/option_selector"
>
<ImageView
android:id="@+id/recharge_method_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="4dp"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="@+id/recharge_method_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
/>
<TextView
android:id="@+id/recharge_method_clue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
/>
</LinearLayout>
<ImageView
android:id="@+id/recharge_method_checked"
android:layout_width="34dp"
android:layout_height="28dp"
android:layout_marginRight="16dp"
android:src="@drawable/option_checked"
android:visibility="invisible"
/>
</LinearLayout>

我为了给一个 ListView 的 item 显示一个选择图标,在定义 item 模板文件时直接加了一个 ImageView ,通过控制它的显示和隐藏来达到看起来选中的效果。偷了个懒,这是比较简单的实现,在 ListView 中 item 数量不多时对内存、性能等影响不大。

代码比较简单,分两部分来看吧,一部分是初始化列表,一部分是用户点击列表中的某项后切换选中标记。

初始化列表

initRechargeList()方法用来初始化充值方式列表,代码如下:

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
private void initRechargeList(){
actionTexts = new String[]{
getString(R.string.recharge_unionpay), getString(R.string.recharge_alipay), getString(R.string.recharge_bestpay)
};
actionClue = new String[]{
getString(R.string.recharge_unionpay_clue), getString(R.string.recharge_alipay_clue), getString(R.string.recharge_bestpay_clue)
};
actionImages = new int[]{
R.drawable.unionpay,
R.drawable.recharge_icon_alipay,
R.drawable.recharge_icon_bestpay
};
actionList = (ListView)findViewById(R.id.recharge_method_list);
actionItems = new ArrayList<HashMap<String, Object>>();
actionAdapter = new SimpleAdapter(this, actionItems, R.layout.recharge_method_list_item,
new String[]{"action_icon", "action_name", "action_clue"},
new int[]{R.id.recharge_method_icon, R.id.recharge_method_name, R.id.recharge_method_clue});

for(int i = 0; i < actionImages.length; ++i) {
HashMap<String, Object> item = new HashMap<String, Object>();
item.put("action_icon", actionImages[i]);
item.put("action_name", actionTexts[i]);
item.put("action_clue", actionClue[i]);
actionItems.add(item);
}
actionList.setAdapter(actionAdapter);
actionList.setOnItemClickListener(itemListener);
actionList.post(new Runnable() {
@Override
public void run() {
lastCheckedOption = actionList.getChildAt(1).findViewById(R.id.recharge_method_checked);
lastCheckedOption.setVisibility(View.VISIBLE);
actionList.setItemChecked(1, true);
}
});
}

上面的代码是初始化充值方式列表。 ListView 的用法也比较简单,View–Row Template–Data–Adapter,四个要素。

我遇到的问题是:如何默认选中某一项。

实际上我的列表中只有三项,不用考虑哪一项会不可见,应该在安卓手机上都是可见的。

一开始我在调用了 ListView 的 setAdapter 方法后,直接使用 getChildAt(1) 来获取第二项对应的 View ,你猜到了,没错,崩溃了: NullPointerException 。搞了半天,我才弄明白: setAdapter() 其实是异步的 ,调用了这个方法, ListView 的 item 并没有立马创建,而是在下一轮消息处理时才创建。弄明白了这个,就有了前面代码中的解决办法:使用 post() 提交一个 Runnable() 对象,在 Runnable() 内部来做默认选中这种初始化动作。

如你所见,我 new 了一个 Runnable 给 post() 方法,在 run() 内找到了第 2 项,显示了选中图标;并且我把第 2 项对应的 View 保存到 lastCheckedOption 成员变量中。后面我们会通过 lastCheckedOption 这个变量,结合 OnItemClickListener 来实现 ListView 中三个 item 的互斥选择效果。

用户点击处理

点击处理是通过 AdapterView.OnItemClickedListener 接口完成的。代码如下:

1
2
3
4
5
6
7
8
9
10
11
private AdapterView.OnItemClickListener itemListener = new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(lastCheckedOption != null){
lastCheckedOption.setVisibility(View.INVISIBLE);
}
lastCheckedOption = view.findViewById(R.id.recharge_method_checked);
lastCheckedOption.setVisibility(View.VISIBLE);
}
};

如你所见,我通过 lastCheckedOption 变量保存了上次选中的 item 中的表示选中效果的图标,用户点击某一个时,先隐藏上一个 item 的选中图标,再显示当前的,就有了貌似互斥的效果了。

一切就这么简单,搞定了。

最终的效果是酱紫的: