洞察企业如何通过模块化APP集成工具高效管理多平台小程序
1297
2022-11-07
android ListView中Checkbox实现单选,全选,全不选功能
@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.group_list2); initBottomBar(); listView = (ListView) findViewById(R.id.groupListView); GroupService service = new GroupService(this); groupList = service.getGroupList(null); adapter = new GroupListAdapter(this, groupList); listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView> adapterView, View view, int position, long arg3) { // TODO Auto-generated method stub Group group = groupList.get(position); CheckBox ctb = (CheckBox) view.findViewById(R.id.checkBox); ctb.toggle(); adapter.selectedMap.put(group.id, ctb.isChecked()); adapter.notifyDataSetChanged(); if(adapter.selectedMap.containsValue(true)){ add2ScanListBtn.setEnabled(true); }else{ add2ScanListBtn.setEnabled(false); } } }); } private void initBottomBar(){ bottomBarView = (LinearLayout) findViewById(R.id.bottomBar); add2ScanListBtn = (Button) bottomBarView.findViewById(R.id.add2ScanListBtn); add2ScanListBtn.setEnabled(false); checkBtn = (Button) bottomBarView.findViewById(R.id.checkBtn); closeCheckBtn = (Button) bottomBarView.findViewById(R.id.closeCheckBtn); add2ScanListBtn.setOnClickListener(this); checkBtn.setOnClickListener(this); closeCheckBtn.setOnClickListener(this); }
@Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, MENU_EDITOR, 0, "编辑"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_EDITOR: setCheckBoxVisible(View.VISIBLE); adapter.notifyDataSetChanged(); break; default: break; } return super.onOptionsItemSelected(item); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.add2ScanListBtn: GroupService service = new GroupService(this); service.add2ScanList(adapter.selectedMap); for (Group group : groupList) { adapter.selectedMap.put(group.id, false); } setCheckBoxVisible(View.GONE); adapter.notifyDataSetChanged(); break; case R.id.checkBtn: CharSequence text = checkBtn.getText(); if (getText(R.string.checkAll).equals(text)) { for (int i = 0; i < adapter.getCount(); i++) { Group group = groupList.get(i); adapter.selectedMap.put(group.id, true); } checkBtn.setText(getText(R.string.checkNeither)); add2ScanListBtn.setEnabled(true); }else if(getText(R.string.checkNeither).equals(text)){ for (int i = 0; i < adapter.getCount(); i++) { Group group = groupList.get(i); adapter.selectedMap.put(group.id, false); } checkBtn.setText(getText(R.string.checkAll)); add2ScanListBtn.setEnabled(false); } adapter.notifyDataSetChanged(); break; case R.id.closeCheckBtn: setCheckBoxVisible(View.GONE); adapter.notifyDataSetChanged(); break; default: break; } }
//设置checkbox是否可见 private void setCheckBoxVisible(int state){ bottomBarView.setVisibility(state); for (int i = 0; i < adapter.getCount(); i++) { Group group = groupList.get(i); adapter.visibleMap.put(group.id, state); } }
class GroupListAdapter extends BaseAdapter{ private Context context; private List
group_list2.xml
group_list2_item.xml
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~