大佬教程收集整理的这篇文章主要介绍了android:ListView中的复选框(所选元素的奇怪行为),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在ListView中,我尝试放置一个适配器(从基本适配器扩展)内容复选框.
列表视图布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://scheR_322_11845@as.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://scheR_322_11845@as.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,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并更改所选项目的背景颜色.
非常感谢你的帮助!
ListView lv = ((ListActivity)context).getListView(); // Containing all check states SparseBooleanArray sba = lv.getcheckedItemPositions(); checkBox cb = (checkBox) convertView.findViewById(R.id.yourcheckBox); cb.setchecked(false); if(sba != null) if(sba.get(position)) cb.setchecked(true);
这就是你在活动方面所需要的.
你已经通过android:在xml中使用SELEctMode或使用setChoiceMode将ListView设置为多选模式.复选框应该是不可点击的,不可聚焦的.
您必须删除按钮上的onClick侦听器.无论你在onClick按钮上做什么,你都必须将该逻辑添加到ListActivtiy的onListItemClick.
以上是大佬教程为你收集整理的android:ListView中的复选框(所选元素的奇怪行为)全部内容,希望文章能够帮你解决android:ListView中的复选框(所选元素的奇怪行为)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。