先看效果

????android:choiceMode="singleChoice"
????android:background="#fff"
????android:layout_marginTop="5dp"
????android:id="@+id/listview_template"
????android:layout_above="@id/ll"
????android:layout_width="match_parent"
????android:layout_height="match_parent"/>
其中choiceMode的屬性很重要 選擇模式.再沒有發(fā)現(xiàn)這個屬性的時候,我們一般會把選中的添加到一個Map中(position,true或false),然后在取出值進(jìn)行判斷是否有選中,
ListView的選擇模式有4中分別是
1,CHOICE_MODE_NONE普通模式
2,CHOICE_MODE_SINGIE單選模式
3,CHOICE_MODE_MULTIPLE多選模式
4,CHOICE_MODE_MULTIPLE_MODAL多選模式
也可以在代碼中設(shè)置這4個屬性值
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGIE);
下面是item的布局里面包含一個RadioButton
getView方法
@OverridepublicViewgetView(final intposition,View convertView,ViewGroup parent) {
????if(convertView ==null) {? ? ? ?
????????convertView = View.inflate(SelectWatermarkActivity.this,R.layout.view_template, null);
????}? ?
????TextView name = (TextView) convertView.findViewById(R.id.template_name);
????finalRadioButton radioButton = (RadioButton) convertView.findViewById(R.id.chickbutton);
????????if(selectPosition== position) {? ? ? ?
????????????radioButton.setChecked(true);
????????}else{? ? ? ?
????????????radioButton.setChecked(false);
????????}? ?
????????TemplateBean.ResponseBean bean =mResponse.get(position);
????????name.setText(bean.getTemplateName());
????????returnconvertView;
}
其中selectPosition是用戶選擇條目的變量
再用戶點(diǎn)擊條目的時候進(jìn)行初始化
@Overridepublic voidonItemClick(AdapterView parent,View view, intposition, longid) {
????????????selectPosition= position;
????????????mAdapter.notifyDataSetChanged();
}
好了使用choiceMode屬性可以很簡單的實(shí)現(xiàn)單選和多選功能