今天在項目中需要實現一個支持多選的ListView,以前的做法是通過給每個列表項添加CheckBox來解決,但是總覺得這樣破壞了列表整體的美觀。
還是自己摸索下吧。先看看ListView的API,木有發現能用的,在往上看看父類AbsListView,找到一個貌似能用的
public void setItemChecked (int position, boolean value)
Sets the checked state of the specified position. The is only valid if the choice mode has been set to CHOICE_MODE_SINGLE
or CHOICE_MODE_MULTIPLE
.
Parameters
position | The item whose checked state is to be checked |
---|---|
value | The new checked state for the item |
public boolean isItemChecked (int position)
Returns the checked state of the specified position. The result is only valid if the choice mode has been set to CHOICE_MODE_SINGLE
or CHOICE_MODE_MULTIPLE
.
Parameters
position | The item whose checked state to return |
---|
Returns
- The item's checked state or
false
if choice mode is invalid
See Also
private ListView lv_contacts;
lv_contacts.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
lv_contacts = (ListView) findViewById(R.id.lv_contacts);
/**
* 中間省略設置Adapter等初始化代碼
*/
lv_contacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
boolean isChecked = isItemChecked(position);
Log.i(TAG, "checked state= "+isChecked);
lv_contacts.setItemChecked(position, !isChecked);
}
});
然後開始測試,看Log輸出。public class ContactsChooserActivity extends Activity {
private static final String TAG = "ContactsChooserActivity";
private ListView lv_contacts;
private List<Map<String, String>> list_data;
private boolean[] bCheckStatesArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts_chooser);
lv_contacts = (ListView) findViewById(R.id.lv_contacts);
// 初始化列表數據
list_data = initListData();
bCheckStatesArray = new boolean[list_data.size()];
ContactsAdapter adapter = new ContactsAdapter(this, list_data, R.layout.item_contacts_list,
new String[]{"NAME", "NUMBER"}, new int[]{R.id.tv_item_name, R.id.tv_item_number});
lv_contacts.setAdapter(adapter);
lv_contacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
boolean isChecked = bCheckStatesArray[position];
if (isChecked) {
view.setBackgroundColor(Color.TRANSPARENT);
bCheckStatesArray[position] = false;
} else {
view.setBackgroundColor(Color.BLUE);
bCheckStatesArray[position] = true;
}
}
});
// 把選中狀態記錄數組初始化一下
for (int i = 0; i < bCheckStatesArray.length; i++) {
bCheckStatesArray[i] = false;
}
}
/**
* 在getView方法中,如果不對view進行處理,就會導致上下拉動列表之後,列表項被選中的顏色狀態混亂。
*/
private class ContactsAdapter extends SimpleAdapter {
public ContactsAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//return super.getView(position, convertView, parent);
View v = super.getView(position, convertView, parent);
//根據選中狀態設置好顏色,防止拖動後出現混亂。
if (bCheckStatesArray[position]) {
v.setBackgroundColor(Color.BLUE);
} else {
v.setBackgroundColor(Color.TRANSPARENT);
}
return v;
}
}
}
列表項的佈局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:paddingTop="@dimen/item_vertical_margin"
android:paddingBottom="@dimen/item_vertical_margin"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="NAME"
android:layout_weight="1"
android:id="@+id/tv_item_name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="NUMBER"
android:id="@+id/tv_item_number" />
</LinearLayout>
主要的代碼就是這些,關於實現列表多選的其它更好方法,還希望大家多多指教和交流。