ListView中有Checkbox時的點擊和選擇問題

之前項目中有用到這塊的東西,現在在拿來用,覺得還是寫下來比較好。

言簡意賅,主界面有個listview:

<ListView
        android:id="@+id/list_food"
        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)

<CheckBox
        android:id="@+id/food_add"
        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<FoodBean> mData;
/*the context*/
Context context;
int k=0;
public static Map<Integer, Boolean> isSelected= new HashMap<Integer, Boolean>();
public FoodChooseAdapter(Context context, List<FoodBean> list) {
mInflater = LayoutInflater.from(context);
this.context = context;
mData = list;
init();
}
// 初始化
private void init() {
// mData=new ArrayList<Map<String, Object>>();
// 這兒定義isSelected這個map是記錄每個listitem的狀態,初始狀態全部爲false。

for (int i = 0; i < mData.size(); i++) {
isSelected.put(i, false);
}
}
public View getView(int position, View convertView, ViewGroup parent) {
HistoryWeightHolder holder = null;
if (convertView == null) {
// init convertView if convertView is null
convertView = mInflater.inflate(R.layout.food_choose,null);
holder = new HistoryWeightHolder();
holder.radio=(CheckBox) convertView.findViewById(R.id.food_add);
holder.name = (TextView) convertView
.findViewById(R.id.name);
holder.unit = (TextView) convertView
.findViewById(R.id.food_unit);
holder.energy = (TextView) convertView
.findViewById(R.id.energy);
convertView.setTag(holder);

} else {
holder = (HistoryWeightHolder) convertView.getTag();
}
holder.name.setText(mData.get(position).getName());
holder.unit.setText(String.valueOf(mData.get(position).getUnit()));
holder.energy.setText(String.valueOf(mData.get(position).getEnergy())+context.getResources().getText(R.string.unit_1).toString());
holder.radio.setChecked(isSelected.get(position));
return convertView;
}


public static final class HistoryWeightHolder {
public CheckBox radio;
public TextView name;
public TextView unit;
public TextView energy;
}


public int getCount() {
return mData.size();
}


public Object getItem(int position) {
return null;
}


public long getItemId(int position) {
return 0;
}

最後是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的狀態,其他選項都被置爲未選中狀態。

大概就是這個效果了

發佈了29 篇原創文章 · 獲贊 18 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章