1.布局效果:
2.对应Acivity代码实现:
package me.wcy.slidingclose;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Iterator;
public class CheckListActivity extends BaseActivity implements View.OnClickListener {
private Button btn_sure, btn_cancel, btn_all, btn_reverse, btn_del;
private ListView lv;
private TextView sum_price;
private String[] name = new String[]{"苹果", "梨", "香蕉", "菠萝", "草莓", "蓝莓",
"桔子", "橙", "木瓜", "西瓜", "哈密瓜", "香瓜", "甘蔗", "马蹄", "人参果","海棠果", "火龙果","柠檬",
"提子", "葡萄", "芒果", "枇杷", "桃","杨桃", "樱桃", "弥猴桃",
"李", "番石榴", "榴莲", "柿子", "杏子", "菠萝蜜", "柚子", "龙眼",
"荔枝", "椰子", "小番茄", "蔷薇果",
"无花果", "梅子", "红枣", "山竹", "桑椹", "山渣", "红毛丹"};
private float[] price = new float[]{100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100};
private Fruits fruits;
private ArrayList<Fruits> list_fruits;
private MyListAdapter adapter;
float sum = 0;//(价格的总价)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_normal);
initUI();
initData();
}
private void initData() {
// 创建集合对象
list_fruits = new ArrayList<Fruits>();
for (int i = 0; i < name.length; i++) {
fruits = new Fruits(name[i], price[i], false);
// 将对象添加到集合中
list_fruits.add(fruits);
}
// 为listview设置适配器
adapter = new MyListAdapter(CheckListActivity.this, list_fruits, sum_price);
lv.setAdapter(adapter);
}
private void initUI() {
lv = (ListView) findViewById(R.id.lv_view);
sum_price = (TextView) findViewById(R.id.sum_price);
btn_sure = (Button) findViewById(R.id.btn_sure);
btn_cancel = (Button) findViewById(R.id.btn_cancel);
btn_all = (Button) findViewById(R.id.btn_all);
btn_reverse = (Button) findViewById(R.id.btn_reverse);
btn_del = (Button) findViewById(R.id.btn_del);
btn_sure.setOnClickListener(this);
btn_cancel.setOnClickListener(this);
btn_all.setOnClickListener(this);
btn_reverse.setOnClickListener(this);
btn_del.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_sure:
int count = 0;
for (int i = 0; i < list_fruits.size(); i++) {
if (list_fruits.get(i).isIscheck()) {
count += 1;
}
}
adapter.notifyDataSetChanged();
Toast.makeText(this, "已选择:" + count + "个", Toast.LENGTH_LONG).show();
break;
case R.id.btn_cancel:
for (int i = 0; i < list_fruits.size(); i++) {
if (list_fruits.get(i).isIscheck()) {
//为选中的状态就使其为未选中的状态
list_fruits.get(i).setIscheck(false);
//计算总价
sum -= list_fruits.get(i).getPrice();
}
}
adapter.notifyDataSetChanged();
break;
case R.id.btn_all:
sum = 0;//每次点击是都现将总价赋值为0,否则会出现累加的效果
// 将复选框设置为true
for (int i = 0; i < list_fruits.size(); i++) {
list_fruits.get(i).setIscheck(true);
//计算总价
sum += list_fruits.get(i).getPrice();
}
//为总价textview赋值
sum_price.setText(sum + "");
// 重新刷新适配器(实现页面重新展示执行后展示最后的界面)
adapter.notifyDataSetChanged();
break;
case R.id.btn_reverse:
//反选
for (int i = 0; i < list_fruits.size(); i++) {
//判断复选框的选中状态
boolean ischeck = list_fruits.get(i).isIscheck();
if (ischeck) {
//为选中的状态就使其为未选中的状态
list_fruits.get(i).setIscheck(false);
//计算总价
sum -= list_fruits.get(i).getPrice();
} else {
//为未选中的状态就使其为选中的状态
list_fruits.get(i).setIscheck(true);
sum += list_fruits.get(i).getPrice();
}
}
sum_price.setText(sum + "");
adapter.notifyDataSetChanged();
break;
case R.id.btn_del:
//迭代遍历集合对象
Iterator<Fruits> iterator = list_fruits.iterator();
while (iterator.hasNext()) {
Fruits next = iterator.next();//获取每次迭代的对象
boolean ischeck = next.isIscheck();//遍历选中的状态,若是选中的状态,就将选中的 条目删除
if (ischeck) {
iterator.remove();
}
}
adapter.notifyDataSetChanged();
break;
}
}
}
3.对应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 java.util.List;
public class MyListAdapter extends BaseAdapter {
private Context context;
private ViewHolder holder;
private List<Fruits> list;
private float sum;
private TextView sum_price;
public MyListAdapter(Context context, List<Fruits> list, TextView sum_price) {
this.context = context;
this.list = list;
this.sum_price = sum_price;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
if(convertView == null){
convertView = View.inflate(context,R.layout.item_view_check,null);
holder = new ViewHolder();
holder.box = (CheckBox) convertView.findViewById(R.id.cb);
holder.name = (TextView) convertView.findViewById(R.id.tv_name);
holder.price = (TextView) convertView.findViewById(R.id.tv_price);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(list.get(position).getName());
holder.price.setText(list.get(position).getPrice() + "");
// 滑动时,checkbox状态改变了(convertView复用导致的),就调用这个方法
holder.box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
list.get(position).setIscheck(isChecked);
sum = 0;
for (int i = 0; i < list.size(); i++) {
boolean ischeck = list.get(i).isIscheck();
if (ischeck) {
float price = list.get(i).getPrice();
sum += price;
}
}
sum_price.setText(sum + "");
}
});
//为复选框赋值必须写在状态变换监听事件的后面(避免错乱)
holder.box.setChecked(list.get(position).isIscheck());
return convertView;
}
class ViewHolder {
TextView name;
TextView price;
CheckBox box;
}
}
4.Bean类:
public class Fruits {
private String name;
private float price;
private boolean ischeck;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public boolean isIscheck() {
return ischeck;
}
public void setIscheck(boolean ischeck) {
this.ischeck = ischeck;
}
public Fruits(String name, float price, boolean ischeck) {
this.name = name;
this.price = price;
this.ischeck = ischeck;
}
}