项目中有用到复选框的例子,啊啊......在网上查找有关资料,大多都是过于繁琐,所以自己决定写个这个方面的demo。。。
先给个效果图:
在ListView中添加复选框主要注意以下几个问题:
1、ListView item与item中的控件抢焦点的问题(必须设置CheckBox不可点击和不能获取焦点,让ListView得到焦点)
2、ListView的setChoicMode(int choiceMode)选择模式():有choiceMode : CHOICE_MODE_NONE, CHOICE_MODE_SINGLE, or CHOICE_MODE_MULTIPLE分别是不可选、单选、多选
3、点击ListView item时,反转CheckBox的状态,mCheckBox.toggle();
说到这,可以知道ListView实现复选框的逻辑还是比较简单的,针对第一个问题,为什么需要让CheckBox失去焦点呢?因为像系统默认可点击的控件如Button、CheckBox、ImageButton等或者它们的子控件,相对其它默认不可点击的控件具有更高的优先级,所以在和其它控件一起使用中它们会优先获得焦点,所以如果不设置CheckBox失去焦点,那么ListView就不可点击了,只能点击CheckBox这个控件。
好了,直接贴一下代码吧:
这个方法就是显示一个ListView复选框:
private void showAllAppInfo() { View view = LayoutInflater.from(this).inflate(R.layout.show_appinfo, null); ListView mListView = (ListView) view .findViewById(R.id.show_appinfo_listview); Button mBtnCancel = (Button) view .findViewById(R.id.show_appinfo_cancel); Button mBtnSubmit = (Button) view .findViewById(R.id.show_appinfo_submit); final MyAdapter adapter = new MyAdapter(); mListView.setAdapter(adapter); mListView.setItemsCanFocus(true);// 让item得到焦点,【注意】必须将ListView的item布局中的Checkbox控件失去焦点 mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);// 设置多选模式 mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ViewHolder holder = (ViewHolder) view.getTag();// 得到当前点击的View的ViewHolder对象 holder.mCheckBox.toggle();// 当点击时设置相反的状态 adapter.mCheckBoxSelected.put(position, holder.mCheckBox.isChecked());//修改Hashmap中对应位置Checkbox的选中情况 } }); Display display = getWindowManager().getDefaultDisplay(); Point outSize = new Point(); display.getSize(outSize); int width = outSize.x; int height = outSize.y; final PopupWindow pop = new PopupWindow(view, width * 3 / 4, height * 4 / 5, true); WindowManager.LayoutParams params = getWindow().getAttributes(); params.alpha = 0.6f; getWindow().setAttributes(params); mBtnSubmit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < adapter.appInfoList.size(); i++) { // 循环遍历集合,得到选择item的信息 if (adapter.mCheckBoxSelected.get(i)) { String selectedAppName = adapter.appInfoList.get(i) .getAppName(); // Drawable selectedAppIcon = adapter.appInfoList.get(i).getAppIcon(); sb.append(selectedAppName+","); } } pop.dismiss(); Toast.makeText(getApplicationContext(), sb.toString(), Toast.LENGTH_SHORT).show(); } }); mBtnCancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { pop.dismiss(); } }); pop.setOnDismissListener(new OnDismissListener() { @Override public void onDismiss() { WindowManager.LayoutParams params = getWindow() .getAttributes(); params.alpha = 1.0f; getWindow().setAttributes(params); } }); pop.setBackgroundDrawable(new BitmapDrawable()); pop.setOutsideTouchable(true); pop.setAnimationStyle(R.style.showPopupAnimation); pop.showAtLocation(view, Gravity.CENTER, 0, 0); }
重写适配器,Myadapter.java
public class MyAdapter extends BaseAdapter { public List<AppInfo> appInfoList; public HashMap<Integer, Boolean> mCheckBoxSelected; public MyAdapter() { appInfoList = getSystemAppInfo(); mCheckBoxSelected = new HashMap<Integer, Boolean>(); for (int i = 0; i < appInfoList.size(); i++) { mCheckBoxSelected.put(i, false);//初始化, 默认不选中 } } @Override public int getCount() { return appInfoList.size(); } @Override public Object getItem(int position) { return appInfoList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView == null) { viewHolder = new ViewHolder(); convertView = LayoutInflater.from(MainActivity.this).inflate( R.layout.listview_item, null, true); viewHolder.mImgAppIcon = (ImageView) convertView .findViewById(R.id.listview_item_img); viewHolder.mTvAppName = (TextView) convertView .findViewById(R.id.listview_item_tv); viewHolder.mCheckBox = (CheckBox) convertView .findViewById(R.id.listview_item_cbx); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.mImgAppIcon.setImageDrawable(appInfoList.get(position) .getAppIcon()); viewHolder.mTvAppName.setText(appInfoList.get(position) .getAppName()); viewHolder.mCheckBox.setChecked(mCheckBoxSelected.get(position)); return convertView; } }
ViewHolder类:
public final static class ViewHolder { ImageView mImgAppIcon; TextView mTvAppName; CheckBox mCheckBox; }
得到系统内非系统app:
// 得到非系统应用列表 private List<AppInfo> getSystemAppInfo() { List<AppInfo> list = new ArrayList<AppInfo>(); List<PackageInfo> packages = this.getPackageManager() .getInstalledPackages(0); AppInfo appInfo = null; for (PackageInfo temp : packages) { if ((temp.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { // 非系统应用 String appName = (String) temp.applicationInfo .loadLabel(getPackageManager()); Drawable appIcon = temp.applicationInfo .loadIcon(getPackageManager()); appInfo = new AppInfo(appIcon, appName); list.add(appInfo); appInfo = null; } else { // 系统应用 } } return list; }
当然还有一个JavaBean类:AppInfo.java,这个类的代码就不贴了。
源码下载请戳:http://download.csdn.net/detail/u010687392/8702343
时间: 2024-11-06 13:22:12