之前项目中有用到这块的东西,现在在拿来用,觉得还是写下来比较好。
言简意赅,主界面有个listview:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/food_bottom"
android:layout_below="@id/input_1"
android:divider="@color/voice_bg"
android:cacheColorHint="@color/transparent"
android:dividerHeight="1dip" >
在adapter中有一个checkbox:(可选择状态都设为false)
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/radiobutton"
android:layout_alignParentRight="true"
android:button="@null"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:paddingRight="@dimen/margin_max"
android:layout_marginRight="@dimen/margin_max" />
在adapter中进行标记和赋值:
private LayoutInflater mInflater;
/**the the food data list */
private List
/*the context*/
Context context;
int k=0;
public static Map
public FoodChooseAdapter(Context context, List
mInflater = LayoutInflater.from(context);
this.context = context;
mData = list;
init();
}
// 初始化
private void init() {
// mData=new ArrayList
最后是activity中的代码:
fca = new FoodChooseAdapter(CalerySearchActivity.this, mListCount);
mList.setAdapter(fca);
mList.setItemsCanFocus(false);
mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
HistoryWeightHolder holder = (HistoryWeightHolder) view
.getTag();
holder.radio.toggle();
/* click of last enable all left;inverse is all the same */
for (int i = 0; i < mList.getCount(); i++) {
if (i == position) {
FoodChooseAdapter.isSelected.put(position,
holder.radio.isChecked());
} else {
FoodChooseAdapter.isSelected.put(i, false);
}
}
}
});
擦~主要就是这样了,效果就是点击list的一项,这项被选中,有checkbox的状态,其他选项都被置为未选中状态。
大概就是这个效果了