ListView和CheckBox实现全选,全不选和反选

时间:2021-08-05 10:01:22

 

ListView和CheckBox实现全选,全不选和反选

MainActivity

package com.example.checkboxtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

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

public class MainActivity extends AppCompatActivity {

    private List<Person> mPersonList = new ArrayList<>();
    private ListView mListView;
    private PersonAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initPerson();
        mAdapter = new PersonAdapter(MainActivity.this, mPersonList);

        //将构建好的适配器对象,完成ListView与数据之间的关联。
        mListView = (ListView) findViewById(R.id.list_view);
        mListView.setAdapter(mAdapter);

        Button button1 = (Button) findViewById(R.id.selected_all);
        Button button2 = (Button) findViewById(R.id.unselected_all);
        Button button3 = (Button) findViewById(R.id.reverse_select);

        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                selectAll(v);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unSelectAll(v);
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reverseSelect(v);
            }
        });
    }

    public void selectAll(View view){

        // 遍历list的长度,将MyAdapter中的map值全部设为true
        for(Person person : mPersonList){
            person.setCheck(true);
        }
        // 刷新listview和TextView的显示
        mAdapter.notifyDataSetChanged();

    }

    public void unSelectAll(View view){

        for(Person person : mPersonList){
            person.setCheck(false);
        }
        mAdapter.notifyDataSetChanged();

    }

    public void reverseSelect(View view) {

        for(Person person : mPersonList) {
            if (person.getIsCheck() == false) {
                person.setCheck(true);
            } else {
                person.setCheck(false);
            }
        }
        mAdapter.notifyDataSetChanged();

    }

    private void initPerson() {
        for(int i=0; i<5; i++) {
            Person tom = new Person("Tom", R.drawable.image1);
            mPersonList.add(tom);
            Person kate = new Person("Kate", R.drawable.image2);
            mPersonList.add(kate);
            Person ross = new Person("Ross", R.drawable.image3);
            mPersonList.add(ross);
            Person kris = new Person("Kris", R.drawable.image1);
            mPersonList.add(kris);
            Person ava = new Person("Ava", R.drawable.image2);
            mPersonList.add(ava);
            Person helen = new Person("Helen", R.drawable.image3);
            mPersonList.add(helen);
        }
    }
}

PersonAdapter

package com.example.checkboxtest;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;

public class PersonAdapter extends BaseAdapter {

    private Context mContext;
    private List<Person> mList;
    private ViewHolder mViewHolder;

    public PersonAdapter(Context mContext, List<Person> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        final Person person = mList.get(i);

        if(view == null) {
            //用LayouInflater加载布局,传给布局对象view
            //用view找到三个控件,存放在viewHolder中,再把viewHolder储存到View中
            //完成了把控件展示在ListView的步骤
            view = LayoutInflater.from(mContext).inflate(R.layout.listview_item,
                    viewGroup, false);
            mViewHolder = new ViewHolder();
            mViewHolder.checkBox = view.findViewById(R.id.check_Box);
            mViewHolder.personImage = view.findViewById(R.id.person_image);
            mViewHolder.personName = view.findViewById(R.id.person_name);
            view.setTag(mViewHolder);
        }else {
            mViewHolder = (ViewHolder) view.getTag();
        }

        mViewHolder.checkBox.setOnCheckedChangeListener
                (new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                person.setCheck(b);
            }
        });

        mViewHolder.personName.setText(person.getName());
        mViewHolder.personImage.setImageResource(person.getImageId());
        mViewHolder.checkBox.setChecked(person.getIsCheck());

        return view;
    }

    class ViewHolder {
        ImageView personImage;
        TextView personName;
        CheckBox checkBox;
    }
}

实体类Person

package com.example.checkboxtest;

public class Person {

    private String name;

    private int imageId;
    private boolean isCheck;

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }

    public boolean getIsCheck() {
        return isCheck;
    }

    public void setCheck(boolean check) {
        isCheck = check;
    }
}


主布局activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/selected_all"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="全选"
            android:textSize="24sp"/>

        <Button
            android:id="@+id/unselected_all"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="全不选"
            android:textSize="24sp"/>

        <Button
            android:id="@+id/reverse_select"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="反选"
            android:textSize="24sp"/>

    </LinearLayout>

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListVew>

</LinearLayout>

子项布局listview_itew

 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/person_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
<TextView android:id="@+id/person_name" android:textSize="29sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_toRightOf="@id/person_image"/> <CheckBox android:id="@+id/check_Box" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:buttonTint="#8B8878"/></RelativeLayout>