1:是否全选
2:选中的商家是否全选
4 计算商品总的数量
5 计算总价
6 当商家得checkbox被点击得时候调用,设置当前商家得所有商品得状态--------商家组其所有的商品
7 当商品得checkbox被点击得时候调用,改变当前商品状态---------商品子条目
8 设置所有商品得状态
9 当加减器被点击得时候调用,改变当前商品得数量
package com.example.shop;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
public class Myadapter extends BaseExpandableListAdapter {
List<CartInfo.DataBean> data;
public Myadapter(List<CartInfo.DataBean> data) {
this.data = data;
}
@Override
public int getGroupCount() {
return data.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return data.get(groupPosition).getList().size();
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
final ParentViewHolder parentViewHolder;
CartInfo.DataBean dataBean = data.get(groupPosition);
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.item_cart_parent, null);
parentViewHolder = new ParentViewHolder(convertView);
} else {
parentViewHolder = (ParentViewHolder) convertView.getTag();
}
parentViewHolder.seller_name_tv.setText(dataBean.getSellerName());
//根据当前商家的所有商品,确定该商家得checkbox是否被选中
boolean shangjiaQuanxuan = isShangjiaQuanxuan(groupPosition);
parentViewHolder.seller_cb.setChecked(shangjiaQuanxuan);
parentViewHolder.seller_cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (monCartListChangeListener != null){
monCartListChangeListener.onSellerCheckedChange(groupPosition);
}
}
});
return convertView;
}
public static class ParentViewHolder {
public CheckBox seller_cb;
public TextView seller_name_tv;
public ParentViewHolder(View rootView) {
this.seller_cb = (CheckBox) rootView.findViewById(R.id.seller_cb);
this.seller_name_tv = (TextView) rootView.findViewById(R.id.seller_name_tv);
rootView.setTag(this);
}
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder childViewHolder;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.item_cart_child, null);
childViewHolder = new ChildViewHolder(convertView);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
//设置商品名字
childViewHolder.product_title_name_tv.setText(data.get(groupPosition).getList().get(childPosition).getTitle());
//设置商品单价
childViewHolder.product_price_tv.setText(data.get(groupPosition).getList().get(childPosition).getPrice() + "");
//设置复选框是否选中
childViewHolder.child_cb.setChecked(data.get(groupPosition).getList().get(childPosition).getSelected() == 1);
childViewHolder.child_cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (monCartListChangeListener != null)
monCartListChangeListener.onProductCheckedChange(groupPosition,childPosition);
}
});
String images = data.get(groupPosition).getList().get(childPosition).getImages();
String[] split = images.split("!");
ImageLoader.getInstance().displayImage(split[0], childViewHolder.product_icon_iv);
//设置组合式自定义控件内部的数量
childViewHolder.add_remove_view.setNumber(data.get(groupPosition).getList().get(childPosition).getNum());
childViewHolder.add_remove_view.setOnNumberChangeListener(new MyAddSubView.OnNumberChangeListener() {
@Override
public void onNumberChange(int num) {
monCartListChangeListener.onProductNumberChange(groupPosition,childPosition,num);
}
});
return convertView;
}
public static class ChildViewHolder {
public CheckBox child_cb;
public ImageView product_icon_iv;
public TextView product_title_name_tv;
public TextView product_price_tv;
public MyAddSubView add_remove_view;
public ChildViewHolder(View rootView) {
this.child_cb = (CheckBox) rootView.findViewById(R.id.child_cb);
this.product_icon_iv = (ImageView) rootView.findViewById(R.id.product_icon_iv);
this.product_title_name_tv = (TextView) rootView.findViewById(R.id.product_title_name_tv);
this.product_price_tv = (TextView) rootView.findViewById(R.id.product_price_tv);
this.add_remove_view = (MyAddSubView) rootView.findViewById(R.id.add_remove_view);
rootView.setTag(this);
}
}
//是否全选
public boolean isQuanxuan() {
for (int i = 0; i < data.size(); i++) {
List<CartInfo.DataBean.ListBean> list = data.get(i).getList();
for (int j = 0; j < list.size(); j++) {
if (list.get(j).getSelected() == 0) {
return false;
}
}
}
return true;
}
//选中的商家是否全选
public boolean isShangjiaQuanxuan(int groupPosition) {
for (int i = 0; i < data.get(groupPosition).getList().size(); i++) {
if (data.get(groupPosition).getList().get(i).getSelected() == 0)
return false;
}
return true;
}
//计算商品总的数量
public int isQuanNum() {
int SumNum = 0;
for (int i = 0; i < data.size(); i++) {
List<CartInfo.DataBean.ListBean> list = data.get(i).getList();
for (int j = 0; j < list.size(); j++) {
if (list.get(j).getSelected() == 1) {
SumNum += list.get(j).getNum();
}
}
}
return SumNum;
}
//计算总价
public double isQuanPrice() {
double SumPrice = 0;
for (int i = 0; i < data.size(); i++) {
List<CartInfo.DataBean.ListBean> list = data.get(i).getList();
for (int j = 0; j < list.size(); j++) {
if (list.get(j).getSelected() == 1) {
SumPrice += list.get(j).getNum()*list.get(j).getPrice();
}
}
}
return SumPrice;
}
//当商家得checkbox被点击得时候调用,设置当前商家得所有商品得状态--------商家组其所有的商品
public void ChageShangJiaCheck(int groupPosition , Boolean isSelected){
for (int j = 0; j < data.get(groupPosition).getList().size(); j++) {
data.get(groupPosition).getList().get(j).setSelected(isSelected ? 1 : 0);
}
}
//当商品得checkbox被点击得时候调用,改变当前商品状态---------商品子条目
public void ChageShangPinCheck(int groupPosition, int childPosition){
int selected = data.get(groupPosition).getList().get(childPosition).getSelected();
data.get(groupPosition).getList().get(childPosition).setSelected(selected==0 ?1:0 );
}
//设置所有商品得状态
public void ChageALLShangPinCheck(Boolean isSelected){
for (int i = 0; i < data.size(); i++) {
for (int j = 0; j < data.get(i).getList().size(); j++) {
data.get(i).getList().get(j).setSelected(isSelected ? 1 : 0);
}
}
}
//当加减器被点击得时候调用,改变当前商品得数量
public void changeCurrentProductNumber(int groupPosition, int childPosition, int number){
data.get(groupPosition).getList().get(childPosition).setNum(number);
}
public interface onCartListChangeListener {
void onSellerCheckedChange(int groupPosition);
void onProductCheckedChange(int groupPosition, int childPosition);
void onProductNumberChange(int groupPosition, int childPosition, int number);
}
onCartListChangeListener monCartListChangeListener;
public void setMonCartListChangeListener(onCartListChangeListener onCartListChangeListener){
monCartListChangeListener = onCartListChangeListener;
}
}
2自定义View的加减
package com.example.shop;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MyAddSubView extends LinearLayout implements View.OnClickListener {
private int number = 1;
private TextView sub_tv;
private TextView product_number_tv;
private TextView add_tv;
public MyAddSubView(Context context) {
this(context, null);
}
public MyAddSubView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyAddSubView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = View.inflate(context, R.layout.add_remove_view_layout, this);
sub_tv = (TextView) view.findViewById(R.id.sub_tv);
product_number_tv = (TextView) view.findViewById(R.id.product_number_tv);
add_tv = (TextView) view.findViewById(R.id.add_tv);
sub_tv.setOnClickListener(this);
add_tv.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.sub_tv:
if (number > 1) {
--number;
product_number_tv.setText(number + "");
if (onNumberChangeListener != null) {
onNumberChangeListener.onNumberChange(number);
}
} else {
Toast.makeText(getContext(), "不能再少了", Toast.LENGTH_SHORT).show();
}
break;
case R.id.add_tv:
++number;
product_number_tv.setText(number + "");
if (onNumberChangeListener != null) {
onNumberChangeListener.onNumberChange(number);
}
break;
}
}
public interface OnNumberChangeListener {
void onNumberChange(int num);
}
private OnNumberChangeListener onNumberChangeListener;
public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener) {
this.onNumberChangeListener = onNumberChangeListener;
}
public int getNumber(){
return number;
}
public void setNumber(int number1){
this.number = number1;
product_number_tv.setText(number+"");
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="30dp">
<TextView
android:background="#ffffff"
android:layout_weight="1"
android:id="@+id/sub_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="-"
android:textSize="16sp" />
<TextView
android:text="1"
android:layout_marginLeft="2dp"
android:background="#ffffff"
android:layout_weight="1"
android:id="@+id/product_number_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
/>
<TextView
android:layout_marginLeft="2dp"
android:background="#ffffff"
android:layout_weight="1"
android:id="@+id/add_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="+"
android:textSize="16sp" />
</LinearLayout>
package com.example.shop;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import com.google.gson.Gson;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private MyAddSubView myAddSubView;
String url = "http://www.zhaoapi.cn/product/getCarts";
private ExpandableListView el_cart;
private CheckBox cb_cart_all_select;
private TextView tv_cart_total_price;
private Button btn_cart_pay;
private Myadapter myadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
cb_cart_all_select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean quanxuan = myadapter.isQuanxuan();
myadapter.ChageALLShangPinCheck(!quanxuan);
myadapter.notifyDataSetChanged();
refreshSelectedAndTotalPriceAndTotalNumber();
}
});
}
private void initData() {
final HashMap<String, String> map = new HashMap<>();
map.put("uid", "71");
HttpUtil.getHtttpUtil().doPost(url, map, new HttpUtil.OKCallback() {
@Override
public void onFailed(Exception e) {
}
@Override
public void onSuccess(String string) {
CartInfo cartInfo = new Gson().fromJson(string, CartInfo.class);
List<CartInfo.DataBean> data = cartInfo.getData();
myadapter = new Myadapter(data);
el_cart.setAdapter(myadapter);
refreshSelectedAndTotalPriceAndTotalNumber();
myadapter.setMonCartListChangeListener(new Myadapter.onCartListChangeListener() {
@Override//点击商家的CheckBox的回调
public void onSellerCheckedChange(int groupPosition) {
boolean shangjiaQuanxuan = myadapter.isShangjiaQuanxuan(groupPosition);
myadapter.ChageShangJiaCheck(groupPosition,!shangjiaQuanxuan);
myadapter.notifyDataSetChanged();
refreshSelectedAndTotalPriceAndTotalNumber();
}
//点击商品的CheckBox的回调
@Override
public void onProductCheckedChange(int groupPosition, int childPosition) {
myadapter.ChageShangPinCheck(groupPosition,childPosition);
myadapter.notifyDataSetChanged();
refreshSelectedAndTotalPriceAndTotalNumber();
}
//点击加减按钮的回调
@Override
public void onProductNumberChange(int groupPosition, int childPosition, int number) {
myadapter.changeCurrentProductNumber(groupPosition,childPosition,number);
myadapter.notifyDataSetChanged();
refreshSelectedAndTotalPriceAndTotalNumber();
}
});
for (int x= 0 ;x <data.size();x++ ){
el_cart.expandGroup(x);
}
}
});
}
private void initView() {
el_cart = (ExpandableListView) findViewById(R.id.el_cart);
cb_cart_all_select = (CheckBox) findViewById(R.id.cb_cart_all_select);
tv_cart_total_price = (TextView) findViewById(R.id.tv_cart_total_price);
btn_cart_pay = (Button) findViewById(R.id.btn_cart_pay);
}
public void refreshSelectedAndTotalPriceAndTotalNumber(){
boolean quanxuan = myadapter.isQuanxuan();
cb_cart_all_select.setChecked(quanxuan);
int quanNum = myadapter.isQuanNum();
btn_cart_pay.setText(quanNum+"");
double quanPrice = myadapter.isQuanPrice();
//保存小数点后两位
String format = new DecimalFormat("#0.00").format(quanPrice);
tv_cart_total_price.setText(format+"");
}
}