ListView的全选反选

package bwie.day02_listview;

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

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

import bwie.day02_listview.adapter.MyAdapter;
import bwie.day02_listview.bean.MyData;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ListView lv;
    private Button all;
    private Button no;
    private Button hide;
    private boolean isHide = false;
    List list = new ArrayList<>();
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initData();
        adapter = new MyAdapter(list, this);
        lv.setAdapter(adapter);
    }

    private void initData() {
        for (int i = 0; i < 50; i++) {
            MyData data = new MyData();
            data.setContent("内容" + i);
            data.setIscheck(false);
            data.setPosition(i);
            list.add(data);
        }

    }

    /**
     * 初始化控件
     */
    private void init() {
        lv = (ListView) findViewById(R.id.lv);
        all = (Button) findViewById(R.id.all);
        no = (Button) findViewById(R.id.no);
        hide = (Button) findViewById(R.id.hide);
        ScrollView scrollView= (ScrollView) findViewById(R.id.sv);
        scrollView.smoothScrollBy(0,0);
        all.setOnClickListener(this);
        no.setOnClickListener(this);
        hide.setOnClickListener(this);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "aa", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.all:
                check(true);
                break;
            case R.id.no:
                check(false);
                break;
            case R.id.hide:
                isHide = isHide == true ? false : true;
                Hide(isHide);
                break;
        }
    }

    public void check(boolean ischeck) {
        for (MyData data : list) {
            data.setIscheck(ischeck);
            adapter.notifyDataSetChanged();
        }
    }

    public void Hide(boolean ishide) {
        if (ishide) {
            hide.setText("取消隐藏");
        } else {
            hide.setText("隐藏已选");
        }
        adapter.setHide(ishide);
        adapter.notifyDataSetChanged();
    }
}

适配器

package bwie.day02_listview.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

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

import bwie.day02_listview.R;
import bwie.day02_listview.bean.MyData;

/**
 * Created by Gaojian on 2016/11/1.
 */
public class MyAdapter extends BaseAdapter {
    private List list = new ArrayList<>();
    private List bflist;
    private Context context;
    private boolean ishide;

    public MyAdapter(List list, Context context) {
        this.bflist = list;
        this.list.addAll(bflist);
        this.context = context;
    }

    @Override
    public int getCount() {

        return list.size();
    }

    @Override
    public Object getItem(int position) {

        return list.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    /**
     * 设置是否隐藏
     *
     * @param ishide
     */
    public void setHide(boolean ishide) {
        this.ishide = ishide;
    }

    @Override
    public void notifyDataSetChanged() {
        list.clear();
        list.addAll(bflist);
        if (ishide) {
            List dellist = new ArrayList<>();
            for (MyData data : list) {
                if (data.ischeck()) {
                    dellist.add(data);
                }
            }
            for (MyData data : dellist) {
                list.remove(data);
            }
        }
        super.notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = View.inflate(context, R.layout.lv_item, null);
            vh.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
            vh.content = (TextView) convertView.findViewById(R.id.content);
            vh.index = (TextView) convertView.findViewById(R.id.index);
            vh.position = (TextView) convertView.findViewById(R.id.position);
            vh.index.setText(position + "");
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        vh.content.setText(list.get(position).getContent());
        vh.position.setText(list.get(position).getPosition() + "");
        vh.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                list.get(position).setIscheck(isChecked);
                System.out.println("position"+position);
                Toast.makeText(context, "ischeck" + isChecked, Toast.LENGTH_SHORT).show();
                notifyDataSetChanged();
            }
        });
        vh.checkBox.setChecked(list.get(position).ischeck());

        return convertView;
    }

    class ViewHolder {
        CheckBox checkBox;
        TextView content;
        TextView index;
        TextView position;
    }
}

//自定义listview 避免高度不能计算的问题
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

/**
* Created by Gaojian on 2016/11/2.
*/
public class CustomListview extends ListView {
public CustomListview(Context context) {
super(context);
}

public CustomListview(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomListview(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, expandSpec);
}

}
//布局文件


<ScrollView 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:id="@+id/sv"
    tools:context="bwie.day02_listview.MainActivity">

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

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

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

            <Button
                android:id="@+id/hide"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="隐藏已选" />

        LinearLayout>

        <bwie.day02_listview.bean.CustomListview
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    LinearLayout>
ScrollView>

你可能感兴趣的:(ListView的全选反选)