Listview中实现隐藏和显示多选框 实现全选,取消全选,反选,删除
1.封装一个实体类存放listview中需要添加的内容
package com.example.choose;
public class Bean {
private String num;
private boolean ischecked;
public Bean(String num, boolean ischecked) {
this.num = num;
this.ischecked = ischecked;
}
public Bean() {
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public boolean isIschecked() {
return ischecked;
}
public void setIschecked(boolean ischecked) {
this.ischecked = ischecked;
}
}
2.listview中适配器的代码
package com.example.choose;
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.TextView;
import java.util.List;
public class Myadpater extends BaseAdapter {
private Context context;
private List<Bean> list;
private boolean flag;
public Myadpater(Context context, List<Bean> list, boolean flag) {
this.context = context;
this.list = list;
this.flag = flag;
}
@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;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Viewholder viewholder;
if (convertView==null){
convertView = LayoutInflater.from(context).inflate(R.layout.layout, null);
viewholder=new Viewholder();
viewholder.textView=convertView.findViewById(R.id.textid);
viewholder.checkBox=convertView.findViewById(R.id.checked);
convertView.setTag(viewholder);
}else {
viewholder = (Viewholder) convertView.getTag();
}
viewholder.textView.setText(list.get(position).getNum());
if (flag){
viewholder.checkBox.setVisibility(View.VISIBLE);
}else {
viewholder.checkBox.setVisibility(View.GONE);
}
viewholder.checkBox.setChecked(list.get(position).isIschecked());
return convertView;
}
class Viewholder{
TextView textView;
CheckBox checkBox;
}
}
3.适配器中的布局文件
<?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="match_parent">
<TextView
android:id="@+id/textid"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
/>
<CheckBox
android:layout_weight="0.1"
android:id="@+id/checked"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</LinearLayout>
4.Activity中的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:id="@+id/listid"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
/>
<Button
android:id="@+id/noall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消全选"
/>
<Button
android:id="@+id/nochoose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="反选"
/>
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
/>
</LinearLayout>
</LinearLayout>
5.Activity中的代码
package com.example.choose;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listid;
private Button all;
private Button noall;
private Button nochoose;
private Button delete;
private LinearLayout layout;
private List<Bean> list = new ArrayList<>();
private Myadpater myadpater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (LinearLayout) findViewById(R.id.layout);
listid = (ListView) findViewById(R.id.listid);
all = (Button) findViewById(R.id.all);
noall = (Button) findViewById(R.id.noall);
nochoose = (Button) findViewById(R.id.nochoose);
delete = (Button) findViewById(R.id.delete);
list.add(new Bean("123",false));
list.add(new Bean("456",false));
list.add(new Bean("789",false));
list.add(new Bean("147",false));
myadpater = new Myadpater(MainActivity.this, list,false);
listid.setAdapter(myadpater);
listid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Bean bean = list.get(position);
if (bean.isIschecked()){
bean.setIschecked(false);
list.set(position,bean);
}else {
bean.setIschecked(true);
list.set(position,bean);
}
myadpater.notifyDataSetChanged();
}
});
listid.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
myadpater = new Myadpater(MainActivity.this, list,true);
listid.setAdapter(myadpater);
myadpater.notifyDataSetChanged();
return false;
}
});
all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < list.size(); i++) {
list.get(i).setIschecked(true);
}
myadpater.notifyDataSetChanged();
}
});
noall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < list.size(); i++) {
list.get(i).setIschecked(false);
}
myadpater.notifyDataSetChanged();
}
});
nochoose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < list.size(); i++) {
boolean ischecked = list.get(i).isIschecked();
if (ischecked){
list.get(i).setIschecked(false);
}else {
list.get(i).setIschecked(true);
}
}
myadpater.notifyDataSetChanged();
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).isIschecked()){
list.remove(list.get(i));
}
}
myadpater.notifyDataSetChanged();
}
});
}
}