activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_activity"
tools:context="com.example.myapplication3.MainActivity">
<androidx.appcompat.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#7F7F7F">
<ImageButton android:id="@+id/ib_fileList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_arrow_back_ios_24"
android:background="@null"
android:layout_gravity="right"/>
</androidx.appcompat.widget.Toolbar>
<ListView
android:id="@+id/main_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/lay"/>
<LinearLayout
android:id="@+id/lay"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0f0"
android:layout_alignParentBottom="true"
android:weightSum="4"
android:orientation="horizontal">
<TextView
android:id="@+id/operate_back"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="返回"
android:textSize="22sp"
android:paddingTop="5dp"
android:paddingBottom="5dp"/>
<TextView
android:id="@+id/operate_select"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="全选"
android:textSize="22sp"
android:paddingTop="5dp"
android:paddingBottom="5dp"/>
<TextView
android:id="@+id/invert_select"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="反选"
android:textSize="22sp"
android:paddingTop="5dp"
android:paddingBottom="5dp"/>
<TextView
android:id="@+id/operate_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="刪除"
android:textSize="22sp"
android:paddingTop="5dp"
android:paddingBottom="5dp"/>
</LinearLayout>
</RelativeLayout>
item.xml
<?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"
android:descendantFocusability="blocksDescendants"
android:padding="5dp">
<TextView
android:id="@+id/listview_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ListView批量删除"
android:textSize="22sp"
android:layout_toLeftOf="@+id/listview_select_cb"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<CheckBox
android:id="@+id/listview_select_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/listview_tv"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
Adapter.java
package com.example.myapplication3;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import java.util.List;
public class Adapter extends BaseAdapter {
private LayoutInflater inflater;
private List<ItemBean> list;
private OnShowItemClickListener onShowItemClickListener;
public Adapter(List<ItemBean> list, Context context) {
this.list = list;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.item, null);
holder.msg = (TextView) convertView.findViewById(R.id.listview_tv);
holder.cb = (CheckBox) convertView.findViewById(R.id.listview_select_cb);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final ItemBean bean = list.get(position);
// 是否是多选状态
if (bean.isShow()) {
holder.cb.setVisibility(View.VISIBLE);
} else {
holder.cb.setVisibility(View.GONE);
}
holder.msg.setText(bean.getMsg());
/* Android中,CompoundButton(勾选状态变化监听器),为抽象的复合按钮,因为是抽象类,所以不能直接使用,它的派生类有CheckBox(复选框)、RadioButton(单选框)、Switch(开关按钮),这些派生类都能使用CompoundButton的属性和方法。(来自:欧阳燊)**/
holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
bean.setChecked(true);
} else {
bean.setChecked(false);
}
// 回调方法,将Item加入已选
onShowItemClickListener.onShowItemClick(bean);
}
});
// 必须放在监听后面
holder.cb.setChecked(bean.isChecked());
return convertView;
}
static class ViewHolder {
TextView msg;
CheckBox cb;
}
public interface OnShowItemClickListener {
public void onShowItemClick(ItemBean bean);
}
public void setOnShowItemClickListener(OnShowItemClickListener onShowItemClickListener) {
this.onShowItemClickListener = onShowItemClickListener;
}
}
ItemBean.java
package com.example.myapplication3;
public class ItemBean {
private String msg;
private boolean isShow; // 是否显示CheckBox
private boolean isChecked; // 是否选中CheckBox
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public boolean isShow() {
return isShow;
}
public void setShow(boolean isShow) {
this.isShow = isShow;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
public ItemBean(String msg, boolean isShow, boolean isChecked) {
super();
this.msg = msg;
this.isShow = isShow;
this.isChecked = isChecked;
}
}
MainActivity.java
package com.example.myapplication3;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements Adapter.OnShowItemClickListener {
private ListView listView;
private List<ItemBean> dataList;
private List<ItemBean> selectList;
private Adapter adapter;
private RelativeLayout rootView;
private LinearLayout opreateView;
private static boolean isShow;
private Toolbar toolbar;
private LinearLayout lay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
//设置主标题,默认为 app_label
toolbar.setTitle("test");
//取代原本的 Actionbar
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.baseline_arrow_back_ios_24);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
listView = findViewById(R.id.main_listview);
lay = findViewById(R.id.lay);
dataList = new ArrayList<ItemBean>();
selectList = new ArrayList<ItemBean>();
for (int i = 0; i < 20; i++) {
dataList.add(new ItemBean("item " + i, false, false));
}
adapter = new Adapter(dataList, this);
listView.setAdapter(adapter);
adapter.setOnShowItemClickListener(this);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
if (isShow) {
return false;
} else {
isShow = true;
for (ItemBean bean : dataList) {
bean.setShow(true);
}
adapter.notifyDataSetChanged();
showOpervate();
listView.setLongClickable(false);
}
return true;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (isShow) {
ItemBean bean = dataList.get(position);
boolean isChecked = bean.isChecked();
if (isChecked) {
bean.setChecked(false);
} else {
bean.setChecked(true);
}
adapter.notifyDataSetChanged();
} else {
Toast.makeText(MainActivity.this, dataList.get(position).getMsg(), Toast.LENGTH_SHORT).show();
}
}
});
}
public void onShowItemClick(ItemBean bean) {
if (bean.isChecked() && !selectList.contains(bean)) {
selectList.add(bean);
} else if (!bean.isChecked() && selectList.contains(bean)) {
selectList.remove(bean);
}
}
/**
* 显示操作界面
*/
private void showOpervate() {
lay.setVisibility(View.VISIBLE);
// 返回、删除、全选和反选按钮初始化及点击监听
TextView tvBack =(TextView) findViewById(R.id.operate_back);
TextView tvDelete = (TextView) findViewById(R.id.operate_delete);
TextView tvSelect = (TextView) findViewById(R.id.operate_select);
TextView tvInvertSelect = (TextView) findViewById(R.id.invert_select);
tvBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isShow) {
selectList.clear();
for (ItemBean bean : dataList) {
bean.setChecked(false);
bean.setShow(false);
}
adapter.notifyDataSetChanged();
isShow = false;
listView.setLongClickable(true);
dismissOperate();
}
}
});
tvSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (ItemBean bean : dataList) {
if (!bean.isChecked()) {
bean.setChecked(true);
if (!selectList.contains(bean)) {
selectList.add(bean);
}
}
}
adapter.notifyDataSetChanged();
}
});
tvInvertSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (ItemBean bean : dataList){
if (!bean.isChecked()){
bean.setChecked(true);
if (!selectList.contains(bean)) {
selectList.add(bean);
}
}else {
bean.setChecked(false);
if (!selectList.contains(bean)) {
selectList.remove(bean);
}
}
}
adapter.notifyDataSetChanged();
}
});
tvDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (selectList != null && selectList.size() > 0) {
dataList.removeAll(selectList);
adapter.notifyDataSetChanged();
selectList.clear();
} else {
Toast.makeText(MainActivity.this, "请选择条目", Toast.LENGTH_SHORT).show();
}
}
});
}
private void dismissOperate() {
lay.setVisibility(View.GONE);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
public void onBackPressed() {
if (isShow) {
selectList.clear();
for (ItemBean bean : dataList) {
bean.setChecked(false);
bean.setShow(false);
}
adapter.notifyDataSetChanged();
isShow = false;
listView.setLongClickable(true);
dismissOperate();
} else {
super.onBackPressed();
}
}
}