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> groupList;
		private Map<Integer, Boolean> selectedMap;//保存checkbox是否被选中的状态
		private Map<Integer, Integer> visibleMap ;
		public GroupListAdapter(Context context, List<Group> groupList) {
			this.context = context;
			this.groupList = groupList;
			selectedMap = new HashMap<Integer, Boolean>();
			visibleMap = new HashMap<Integer, Integer>();
			for (Group group : groupList) {
				selectedMap.put(group.id, false);
				visibleMap.put(group.id, View.GONE);
			}
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return groupList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(R.layout.group_list2_item, null);
				holder.groupNameCTV = (TextView) convertView.findViewById(R.id.groupNameTV);
				holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			Group group = groupList.get(position);
			holder.groupNameCTV.setText(group.m_strGroupName);
			holder.checkBox.setVisibility(visibleMap.get(group.id));
			holder.checkBox.setChecked(selectedMap.get(group.id));
			return convertView;
		}
		
		private class ViewHolder{
			TextView groupNameCTV;
			CheckBox checkBox;
		}
	}


group_list2.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">
  	  <LinearLayout android:layout_height="0dip"
	  	android:layout_width="match_parent"
	  	android:layout_weight="1">
	  	<ListView android:id="@+id/groupListView"
	  		android:layout_width="match_parent"
	  		android:layout_height="match_parent"
	  		android:divider="#999999"
	  		android:dividerHeight="1sp"
	  		/>
  		</LinearLayout>
  		
  		<LinearLayout android:id="@+id/bottomBar"
  			android:layout_height="wrap_content"
		  	android:layout_width="match_parent" android:gravity="center_horizontal"
		  	android:padding="5dip" android:background="@drawable/buttonbar"
		  	android:visibility="gone">
		  	<Button android:id="@+id/add2ScanListBtn"
			    android:layout_width="wrap_content"
			    android:layout_height="wrap_content" 
			    android:text="@string/add2ScanList"/>
		    <Button android:id="@+id/checkBtn"
			    android:layout_width="wrap_content"
			    android:layout_height="wrap_content"
			    android:text="@string/checkAll" />
			<Button android:id="@+id/closeCheckBtn"
			    android:layout_width="wrap_content"
			    android:layout_height="wrap_content"
			    android:text="@string/cancel" />
  		</LinearLayout>
</LinearLayout>


group_list2_item.xml

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  >
  
  		<TextView android:id="@+id/groupNameTV"
		  	android:layout_width="wrap_content"
		  	android:layout_height="wrap_content"
		  	android:textSize="20dip"
		  	android:layout_alignParentLeft="true"
		  	android:textColor="@android:color/white"
		  	android:layout_marginTop="12dip"
		  	android:layout_marginLeft="5dip"
		  	android:paddingBottom="12dip"
		  	/>

		<CheckBox android:id="@+id/checkBox"
			android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	  		android:layout_alignParentRight="true"
	  		android:layout_marginRight="5dip"
	  		android:clickable="false"
		  	android:focusable="false"
		  	android:focusableInTouchMode="false"
	    />
  
</RelativeLayout>


 

 

 


相关文章
|
1月前
|
XML 缓存 Android开发
Android开发,使用kotlin学习多媒体功能(详细)
Android开发,使用kotlin学习多媒体功能(详细)
101 0
|
3月前
|
API Android开发 开发者
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
31 2
|
3月前
|
安全 Linux Android开发
Android 安全功能
Android 安全功能
37 0
|
1天前
|
Java Android开发
Android Mediatek 应用层重置USB设备功能
Android Mediatek 应用层重置USB设备功能
5 0
|
4月前
|
XML 前端开发 Java
Android App实战项目之实现手写签名APP功能(附源码,简单易懂 可直接实用)
Android App实战项目之实现手写签名APP功能(附源码,简单易懂 可直接实用)
45 0
|
1月前
|
XML Java Android开发
[Android]CheckBox复选框
[Android]CheckBox复选框
50 0
|
4月前
|
传感器 物联网 Android开发
【Android App】物联网中查看手机支持的传感器及实现摇一摇功能-加速度传感器(附源码和演示 超详细)
【Android App】物联网中查看手机支持的传感器及实现摇一摇功能-加速度传感器(附源码和演示 超详细)
67 1
|
4月前
|
XML Java 定位技术
【Android App】定位导航GPS中开启手机定位功能讲解及实战(附源码和演示 超详细)
【Android App】定位导航GPS中开启手机定位功能讲解及实战(附源码和演示 超详细)
115 0
|
4月前
|
XML 前端开发 Java
【Android App】三维处理中三维投影OpenGL功能的讲解及实战(附源码和演示 超详细必看)
【Android App】三维处理中三维投影OpenGL功能的讲解及实战(附源码和演示 超详细必看)
33 1
|
4月前
|
JSON 语音技术 Android开发
【Android App】在线语音识别功能实现(使用云知声平台与WebSocket 超详细 附源码)
【Android App】在线语音识别功能实现(使用云知声平台与WebSocket 超详细 附源码)
34 0