话不多说,先上效果图。带选择框和全选按钮的列表,因一些特殊要求还加上些计算功能,这并不是难就是稍微繁琐了些,如 “统计订单功能”

不要说,就是干,那就直接上代码
- 1,主页面的布局文件activity_test7.xml
<?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"
android:background="@color/colorWhite"
android:orientation="vertical">
<ListView
android:id="@+id/lv_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="none"
android:dividerHeight="0.1dp"
android:clickable="true" />
<LinearLayout android:id="@+id/ll_bottom"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_select"
android:layout_width="23dp"
android:layout_height="23dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="10dp"
android:src="@mipmap/gc2_04" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
android:layout_marginRight="10dp"
android:textColor="#7d7d80"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="总金额:"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_allMoney"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="¥ 0.00"
android:textColor="#ff0000"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_apply"
android:layout_width="110dp"
android:layout_height="match_parent"
android:text="提 交"
android:textColor="@color/colorWhite"
android:gravity="center"
android:background="#ff0000"
android:textSize="17sp" />
</LinearLayout>
</LinearLayout>
- 2,接下来到Activity代码,基本上都有注释的不多说了
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.administrator.pagertest.adapter.InvoiceApplyAdapter;
import com.example.administrator.pagertest.model.InvoiceApplyBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class TestCheckActivity extends Activity implements AdapterView.OnItemClickListener,View.OnClickListener {
private String resultJosn = "[\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"name\": \"购买门票\",\n" +
" \"money\": \"124.00\",\n" +
" \"ordernum\": \"1526979933006\",\n" +
" \"time\": \"2018年6月01日\"\n" +
" },\n" +
" {\n" +
" \"id\": \"2\",\n" +
" \"name\": \"购买食物\",\n" +
" \"money\": \"102.00\",\n" +
" \"ordernum\": \"1526979435086\",\n" +
" \"time\": \"2018年6月01日\"\n" +
" },\n" +
" {\n" +
" \"id\": \"3\",\n" +
" \"name\": \"购买特产\",\n" +
" \"money\": \"1100.00\",\n" +
" \"ordernum\": \"1526979935036\",\n" +
" \"time\": \"2018年6月01日\"\n" +
" }\n" +
"]";
private List<InvoiceApplyBean> mListData = new ArrayList<>();
private List<InvoiceApplyBean> mList;
private LinearLayout ll_bottom;
private ImageView iv_select;
private TextView tv_allMoney;
private TextView tv_apply;
private ListView lv_ListView;
private InvoiceApplyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test7);
initView();
}
private void initView() {
ll_bottom = (LinearLayout)findViewById(R.id.ll_bottom);
iv_select = (ImageView)findViewById(R.id.iv_select);
tv_allMoney = (TextView)findViewById(R.id.tv_allMoney);
tv_apply = (TextView)findViewById(R.id.tv_apply);
lv_ListView = (ListView)findViewById(R.id.lv_list);
adapter = new InvoiceApplyAdapter(this, mListData);
lv_ListView.setAdapter(adapter);
lv_ListView.setOnItemClickListener(this);
ll_bottom.setOnClickListener(this);
tv_apply.setOnClickListener(this);
initDate();
}
private void initDate() {
Gson gson = new Gson();
mList = gson.fromJson(resultJosn, new TypeToken<List<InvoiceApplyBean>>() {
}.getType());
mListData.addAll(mList);
adapter.notifyDataSetChanged();
}
private boolean isAllCheck = false;
private BigDecimal bignum1 = new BigDecimal("0.00");
private BigDecimal bignum2;
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
if (position >= 0 && position <= mListData.size()){
if (mListData.get(position).getFlag()==0){
mListData.get(position).setFlag(1);
bignum2 = new BigDecimal(mListData.get(position).getMoney());
bignum1 = bignum1.add(bignum2);
}else if (mListData.get(position).getFlag()==1){
mListData.get(position).setFlag(0);
bignum2 = new BigDecimal(mListData.get(position).getMoney());
bignum1 = bignum1.subtract(bignum2);
}
tv_allMoney.setText("¥ "+bignum1);
for (int i=0; i < mListData.size(); i++){
if (mListData.get(i).getFlag()==1){
isAllCheck = true;
iv_select.setImageResource(R.mipmap.check2_15);
}
if (mListData.get(i).getFlag()==0){
isAllCheck = false;
iv_select.setImageResource(R.mipmap.gc2_04);
break;
}
}
adapter.notifyDataSetChanged();
}
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.ll_bottom:
bignum1 = new BigDecimal("0.00");
if (!isAllCheck) {
isAllCheck = true;
iv_select.setImageResource(R.mipmap.check2_15);
for (int i = 0; i < mListData.size(); i++) {
mListData.get(i).setFlag(1);
bignum2 = new BigDecimal(mListData.get(i).getMoney());
bignum1 = bignum1.add(bignum2);
}
tv_allMoney.setText("¥ " + bignum1);
adapter.notifyDataSetChanged();
} else {
isAllCheck = false;
iv_select.setImageResource(R.mipmap.gc2_04);
for (int i = 0; i < mListData.size(); i++) {
mListData.get(i).setFlag(0);
bignum2 = new BigDecimal(mListData.get(i).getMoney());
bignum1 = bignum1.subtract(bignum2);
}
if (bignum1.doubleValue() >= 0) {
tv_allMoney.setText("¥ " + bignum1);
} else {
bignum1 = new BigDecimal("0.00");
tv_allMoney.setText("¥ " + bignum1);
}
adapter.notifyDataSetChanged();
}
break;
case R.id.tv_apply:
Toast.makeText(getApplicationContext(),""+bignum1,Toast.LENGTH_SHORT).show();
break;
}
}
}
- 3,里面用到的Javabean类, InvoiceApplyBean.java
public class InvoiceApplyBean {
private String id;
private String name;
private String money;
private String ordernum;
private String time;
private int flag = 0;
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
public String getOrdernum() {
return ordernum;
}
public void setOrdernum(String ordernum) {
this.ordernum = ordernum;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
- 4,适配器InvoiceApplyAdapter.java
public class InvoiceApplyAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater lInflater;
private List<InvoiceApplyBean> list = new ArrayList<>();
public InvoiceApplyAdapter(Context mContext, List<InvoiceApplyBean> list) {
this.mContext = mContext;
this.list = list;
if (mContext != null) {
lInflater = LayoutInflater.from(mContext);
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = lInflater.inflate(R.layout.invoice_apply_item, null);
holder.iv_select = (ImageView) view.findViewById(R.id.iv_select);
holder.tv_title = (TextView) view.findViewById(R.id.tv_title);
holder.tv_money = (TextView) view.findViewById(R.id.tv_money);
holder.tv_number = (TextView) view.findViewById(R.id.tv_number);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
final InvoiceApplyBean bean = list.get(i);
holder.tv_title.setText(bean.getName());
holder.tv_money.setText("订单金额:"+bean.getMoney()+"元 | 订单时间:"+bean.getTime());
holder.tv_number.setText("订单号:"+bean.getOrdernum());
if (bean.getFlag() == 0){
holder.iv_select.setImageResource(R.mipmap.gc2_04);
}else if (bean.getFlag() == 1){
holder.iv_select.setImageResource(R.mipmap.check2_15);
}
return view;
}
static class ViewHolder {
private ImageView iv_select;
public TextView tv_title, tv_money, tv_number;
}
}
- 5,子项的布局文件 invoice_apply_item.xml
<?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="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<ImageView
android:id="@+id/iv_select"
android:layout_width="23dp"
android:layout_height="23dp"
android:layout_marginRight="10dp"
android:src="@mipmap/gc2_04" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#000000"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_money"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text=""
android:textColor="#000000"
android:textSize="12sp" />
<TextView
android:id="@+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#7d7d80"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
compile 'com.google.code.gson:gson:2.7'
compile 'com.google.code.gson:gson-parent:2.7'
- 7,各位老铁们,到这里就是全部代码了,最主要就是每次操作就遍历数据,做相应的改变即可。用到的gc2_04 、 check2_15着两个小图标就不传,这次的源码也不传了