首页 > 代码库 > ListView+CheckBox实现全选和反选

ListView+CheckBox实现全选和反选

实现的效果图如下:(在《第一行代码的例子上加以修改的》),当点击全选的时候,效果如下:

技术分享


当只选中下面的几个:

技术分享


此时按下,反选,如图:

技术分享


首先是布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f2f0eb"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="50" >

        <ListView
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:cacheColorHint="#00000000"
            android:divider="#D4D4D4"
            android:dividerHeight="1px"
            android:fastScrollEnabled="true"
            android:overScrollMode="never" />
    </FrameLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dip"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dip"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:background="@drawable/button_register_bg"
            android:gravity="center"
            android:onClick="selectAll"
            android:paddingBottom="7dp"
            android:paddingTop="7dp"
            android:text="全选"
            android:textColor="@android:color/white"
            android:textSize="18sp" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dip"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dip"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:background="@drawable/button_login_bg"
            android:gravity="center"
            android:onClick="selectOppo"
            android:paddingBottom="7dp"
            android:paddingTop="7dp"
            android:text="反选"
            android:textColor="@android:color/white"
            android:textSize="18sp" />
    </LinearLayout>

</LinearLayout>

注意的是ListView是嵌套在FrameLayout下的,而且指定layout_weight属性(否则下面的两个按钮出不来。)

在MainActivity中:

public void selectAll(View view){
		
		for(Fruit fruit : fruitList){
			fruit.setChecked(true);
		}
		adapter.notifyDataSetChanged();
		
	}
	
	public void selectOppo(View view){
		for(Fruit fruit : fruitList){
			fruit.setChecked(!fruit.isChecked());
		}
		adapter.notifyDataSetChanged();
	}

这样就搞定了。

完整的代码:

MainActivity:

package com.example.uilistviewtest;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.example.uilistviewtest.FruitAdapter.ViewHolder;

public class MainActivity extends Activity {

	private List<Fruit> fruitList = new ArrayList<Fruit>();
	private FruitAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initFruits();
		adapter = new FruitAdapter(MainActivity.this,
				R.layout.fruit_item, fruitList);
		ListView listView = (ListView) findViewById(R.id.list_view);
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Fruit fruit = fruitList.get(position);
				ViewHolder holder = (ViewHolder) view.getTag();
				if (fruit.isChecked()) {
					fruit.setChecked(false);

					holder.cb.setChecked(false);

				} else {
					fruit.setChecked(true);

					holder.cb.setChecked(true);

				}

				Toast.makeText(MainActivity.this, fruit.getName(),
						Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	public void selectAll(View view){
		
		for(Fruit fruit : fruitList){
			fruit.setChecked(true);
		}
		adapter.notifyDataSetChanged();
		
	}
	
	public void selectOppo(View view){
		for(Fruit fruit : fruitList){
			fruit.setChecked(!fruit.isChecked());
		}
		adapter.notifyDataSetChanged();
	}
	

	private void initFruits() {
		Fruit apple = new Fruit("Apple", R.drawable.apple_pic);
		fruitList.add(apple);
		Fruit banana = new Fruit("Banana", R.drawable.banana_pic);
		fruitList.add(banana);
		Fruit orange = new Fruit("Orange", R.drawable.orange_pic);
		fruitList.add(orange);
		Fruit watermelon = new Fruit("Watermelon", R.drawable.watermelon_pic);
		fruitList.add(watermelon);
		Fruit pear = new Fruit("Pear", R.drawable.pear_pic);
		fruitList.add(pear);
		Fruit grape = new Fruit("Grape", R.drawable.grape_pic);
		fruitList.add(grape);
		Fruit pineapple = new Fruit("Pineapple", R.drawable.pineapple_pic);
		fruitList.add(pineapple);
		Fruit strawberry = new Fruit("Strawberry", R.drawable.strawberry_pic);
		fruitList.add(strawberry);
		Fruit cherry = new Fruit("Cherry", R.drawable.cherry_pic);
		fruitList.add(cherry);
		for (int i = 0; i < 10; i++) {
			Fruit mango = new Fruit("Mango", R.drawable.mango_pic);
			fruitList.add(mango);
		}
	}

}

Fruit业务bean类:

package com.example.uilistviewtest;

public class Fruit {

	private String name;

	private int imageId;
	
	private boolean checked;

	public Fruit(String name, int imageId) {
		this.name = name;
		this.imageId = imageId;
	}

	public String getName() {
		return name;
	}

	public int getImageId() {
		return imageId;
	}

	public boolean isChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
	}
	
	

}

adapter适配器:

package com.example.uilistviewtest;

import java.util.List;

import android.content.Context;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class FruitAdapter extends ArrayAdapter<Fruit> {

	private int resourceId;

	public FruitAdapter(Context context, int textViewResourceId,
			List<Fruit> objects) {
		super(context, textViewResourceId, objects);
		resourceId = textViewResourceId;
	}

	
	
	CursorAdapter ca;
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Fruit fruit = getItem(position);
		View view;
		ViewHolder viewHolder;
		if (convertView == null) {
			view = LayoutInflater.from(getContext()).inflate(resourceId, null);
			viewHolder = new ViewHolder();
			viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
			viewHolder.fruitName = (TextView) view.findViewById(R.id.fruit_name);
			viewHolder.cb = (CheckBox) view.findViewById(R.id.cb);
			view.setTag(viewHolder);
		} else {
			view = convertView;
			viewHolder = (ViewHolder) view.getTag();
		}
		viewHolder.fruitImage.setImageResource(fruit.getImageId());
		viewHolder.fruitName.setText(fruit.getName());
		viewHolder.cb.setChecked(fruit.isChecked());
		return view;
	}
	
	class ViewHolder {
		
		ImageView fruitImage;
		
		TextView fruitName;
		
		CheckBox cb;
		
	}

}

ListView+CheckBox实现全选和反选