<RelativeLayout 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/main_activity"
tools:context="com.pinpaimap.listviewcheckbox.MainActivity">
<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:background="@drawable/selector"
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:background="@drawable/selector"
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:background="@drawable/selector"
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:background="@drawable/selector"
android:paddingTop="5dp"
android:paddingBottom="5dp"/>
LinearLayout>
RelativeLayout>
2.item.xml
<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>
3.实体类ItemBean:
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;
}
}
4.自定义Adapter:
public class Adapter extends BaseAdapter {
private LayoutInflater inflater;
private List list;
private OnShowItemClickListener onShowItemClickListener;
public Adapter(List 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());
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;
}
}
5.MainActivity:
public class MainActivity extends AppCompatActivity implements Adapter.OnShowItemClickListener {
private ListView listView;
private List dataList;
private List selectList;
private Adapter adapter;
private RelativeLayout rootView;
private LinearLayout opreateView;
private static boolean isShow; // 是否显示CheckBox标识
private LinearLayout lay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.main_listview);
lay= (LinearLayout) findViewById(R.id.lay);
dataList = new ArrayList();
selectList = new ArrayList();
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);
Animation anim = AnimationUtils.loadAnimation(this, R.anim.operate_in);
lay.setAnimation(anim);
// 返回、删除、全选和反选按钮初始化及点击监听
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() {
Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.operate_out);
lay.setVisibility(View.GONE);
lay.setAnimation(anim);
}
@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();
}
}
}
6.在res下新建文件夹anim
(1)新建动画operate_in.xml:
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="100%"
android:toYDelta="0"
android:duration="500">
translate>
(2)新建动画operate_out.xml:
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0"
android:toYDelta="100%"
android:duration="500">
translate>
声明:本文转自http://m.blog.csdn.net/article/details?id=52102839