在一个ListView中,我尝试放置一个扩展自base Adapter的适配器,其中包含复选框。
列表视图布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/mainLayout"
>
<Button
android:id="@+id/close_cat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonClick"
android:text="@string/back" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
列表元素的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TableLayout
android:id="@+id/blocCheck"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25px"
android:layout_marginTop="25px"
android:background="@color/black" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:paddingLeft="4sp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:orientation="horizontal"
android:paddingLeft="20sp" >
<ImageView
android:id="@+id/iv_cat"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="15dip"
android:text="tfgldkjhglf"
android:textSize="20sp" />
</LinearLayout>
<CheckBox
android:id="@+id/check_cat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10sp"
android:onClick="MyHandler" />
</TableRow>
</TableLayout>
</LinearLayout>
我在我的视图中设置了适配器:
list = getListView();
list.setAdapter(new CatAdapter(this, listCat));
我的适配器:
public class CatAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<CatAdapterObject> mListAppInfo;
private Context mContext;
/**
* Constructor
* @param context
* @param data
* @param resource
* @param from
* @param to
*/
public CatAdapter (Context context, List<CatAdapterObject> list){
mContext = context;
mListAppInfo = list;
mInflater = LayoutInflater.from(mContext);
}
/**
* number of elements
*/
@Override
public int getCount() {
return mListAppInfo.size();
}
/**
* get an item in the list
*/
@Override
public Object getItem (int position){
return mListAppInfo.get(position).getId();
}
/**
* get id of the selected item
*/
@Override
public long getItemId(int position) {
return mListAppInfo.get(position).getId();
}
/**
* get the view
*/
@Override
public View getView (int position, View convertView, ViewGroup parent){
LinearLayout layoutItem;
if (convertView == null) {
layoutItem = (LinearLayout) mInflater.inflate(R.layout.category, parent, false);
} else {
layoutItem = (LinearLayout) convertView;
}
// get the current category
CatAdapterObject entry = mListAppInfo.get(position);
//view setting
TextView category = (TextView) layoutItem.findViewById(R.id.category);
CheckBox cb = (CheckBox) layoutItem.findViewById(R.id.check_cat);
ImageView iv_cat = (ImageView) layoutItem.findViewById(R.id.iv_cat);
//elements setting
category.setText(entry.getName());
//checkbox
cb.setChecked(entry.isChecked());
cb.setTag(position);
//picture
Bitmap icon = Utils.getResizedBitmap(BitmapFactory.decodeResource(mContext.getResources(),entry.getPict()),45,45,true);
iv_cat.setImageBitmap(icon);
return layoutItem;
}
}
当活动启动时,列表会完美地显示出来。我可以轻松地上下滚动。然而,当我选中一个复选框并向下滚动时,被选中的框将“失去”其勾选标记,并且列表中最后一个框的背景颜色也会改变。
在选中框时,会调用下面的方法(在我的活动文件中):
public void MyHandler(View v) {
//クリックされたcheckbox
cb = (CheckBox) v;
Log.d("CHECKBOX","before :"+Utils.implode(",",this.cat_id_list));
//get position in the list
Integer position = Integer.parseInt(cb.getTag().toString());
//instance of the view (list's child)
View o = list.getChildAt(position).findViewById(R.id.blocCheck);
// check if box is checked
if (cb.isChecked()) {
o.setBackgroundResource(R.color.pink);
this.cat_id_list.add(position.toString());
// 背景色を替え、リストから削除
} else {
o.setBackgroundResource(R.color.black);
Collections.sort(this.cat_id_list);
int index = Collections.binarySearch(this.cat_id_list,position.toString());
this.cat_id_list.remove(index);
}
Log.d("CHECKBOX","after :"+Utils.implode(",",this.cat_id_list));
}
我的列表中有8个元素,但是当我检查列表的“mChildrenCount”时,只有5个元素。
我只想在“this.cat_id_list”中保存已选元素的id,并更改所选项的背景颜色。
我将非常感谢您的帮助!